通过注解读取配置文件中的值

配置中文件

spring:
  thymeleaf:
    cache: false # 关闭缓存,默认开启
  datasource:
    url: jdbc:mysql://localhost:3306/qcby_db?useUnicode=true&characterEncoding=utf-8
    username: root
    password: 12345678
    driver-class-name: com.mysql.cj.jdbc.Driver

 @Value("${spring.datasource.url}")
    private String dataUrl;

 

通过@Value("${}"),可以读取配置文件中的数值

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java,您可以使用注解读取配置文件的数据。使用注解的好处是可以将数据注入到类属性,从而方便地在代码使用这些数据。 以下是一个简单的例子,演示如何使用注解读取配置文件的数据: 首先,创建一个配置文件`config.properties`,并添加以下内容: ``` database=mysql dbuser=root dbpassword=123456 ``` 然后,创建一个类`Config.java`,并添加以下代码: ```java import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Field; import java.util.Properties; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @interface ConfigProperty { String value() default ""; } public class Config { @ConfigProperty("database") private String database; @ConfigProperty("dbuser") private String dbUser; @ConfigProperty("dbpassword") private String dbPassword; public Config() { readConfig(); } private void readConfig() { Properties prop = new Properties(); InputStream input = null; try { input = new FileInputStream("config.properties"); prop.load(input); for (Field field : this.getClass().getDeclaredFields()) { if (field.isAnnotationPresent(ConfigProperty.class)) { ConfigProperty annotation = field.getAnnotation(ConfigProperty.class); String propertyName = annotation.value().isEmpty() ? field.getName() : annotation.value(); String propertyValue = prop.getProperty(propertyName); field.setAccessible(true); field.set(this, propertyValue); } } } catch (IOException | IllegalAccessException ex) { ex.printStackTrace(); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } } public String getDatabase() { return database; } public String getDbUser() { return dbUser; } public String getDbPassword() { return dbPassword; } } ``` 在这个例子,我们定义了一个注解`@ConfigProperty`,它可以用于类属性上。注解有一个属性`value`,可以用来指定配置文件的属性名。如果未指定,则默认使用属性名作为配置文件的属性名。 我们还定义了一个`readConfig()`方法,用于读取配置文件的数据并将其注入到类属性。在这个方法,我们使用`Properties`类来加载配置文件。然后,我们遍历类的所有属性,如果属性上有`@ConfigProperty`注解,则使用反射机制将属性设置为配置文件。 最后,我们可以创建一个`Main.java`类,来测试`Config.java`类: ```java public class Main { public static void main(String[] args) { Config config = new Config(); System.out.println("Database: " + config.getDatabase()); System.out.println("DB User: " + config.getDbUser()); System.out.println("DB Password: " + config.getDbPassword()); } } ``` 在这个例子,我们创建了一个`Config`对象,并打印出从配置文件读取的数据库信息。 当我们运行`Main.java`时,输出如下: ``` Database: mysql DB User: root DB Password: 123456 ``` 这表明我们已成功从配置文件读取了数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值