简介
一般来说,我们会在配置文件中自定义一些自己需要的值,比如 jwt 的密匙、数据库的连接信息或者一些 FTP 配置等信息。
Spring Boot获取文件总的来说有三种方式,分别是 @Value 注解,@ConfigurationProperties 注解和 Environment 接口。这三种注解可以配合着 @PropertySource 来使用。
1、@PropertySource 解析
@PropertySource 主要是用来指定具体的配置文件
。@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Repeatable(PropertySources.class) public @interface PropertySource { String name() default ""; String[] value(); boolean ignoreResourceNotFound() default false; String encoding() default ""; Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class; }
● value():
指定配置文件
,这样就不用再在 application.properties 中进行激活自定义的配置文件。
● encoding():指定编码
,因为properties文件的编码默认是ios8859-1,读取出来是乱码
● factory():自定义解析文件类型,因为该注解默认只会加载properties文件,如果想要指定yml等其他格式的文件需要自定义实现
。
2、PropertySources
Spring Boot 官网使用的是application.properties文件来实现文件的配置。但是实际情况下一个配置文件是不够用的,比如项目集成redis,mq,以及数据库比如mysql的时候,多个配置文件有利于开发及维护的管理。Spring Boot 可以通过 @PropertySources 来实现多配置文件。
@PropertySources 源码:
public @interface PropertySources {
PropertySource[] value();
}
我们可以看到
@PropertySources 其实就是PropertySource的数组
。
因此通过 @PropertySources 配置方式为:
@SpringBootApplication
@ComponentScan(basePackages = { "com.aron" }) // 通过扫描本路径可不需将ctl包和启动类放在同一目录下
@PropertySources({ @PropertySource("classpath:redis.properties"),
@PropertySource("classpath:database.properties") })
public class ProjectMainEntranceApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectMainEntranceApplication.class, args);
}
}
3、经验与坑
● properties文件默认使用的是iso8859-1,并且不可修改;
●yml文件的加载顺序高于properties,但是读取配置信息的时候会读取后加载的
;
● @PropertySource注解默认只会加载properties文件 @PropertySource注解可以与任何一种方式联合使用;
● 简单值推荐使用@Value,复杂对象推荐使用@ConfigurationProperties;
一、@Value 注解读取配置文件
此方式一般多用于读取较少数量或没有相同前缀的属性值。
使用事项:(1)
@Value 必须在注册类中使用,不能有构造方法,且类加载方式必须为注入方式
:这个类必须是注释有@Bean、@Component、@Service或者@Controller等注解或者其他方式注册为bean。并且使用方式不能是new一个对象,必须为注释有@Autowired、@Resource等类似注解或者使用其他方式注入,否则@Value不会生效。
(2)@Value 不能直接注释在 static 或者 final 的变量
(3) 如果使用想要配置static类型的变量为配置值,可以:@Component public class Student { private static String name; @Value("${student.name}") public void setName(String name){ Student.name = name; } }
1、在 resources 下新建两个配置文件 application-test.properties 和主配置文件 application.properties,内容分别如下:
application-test.properties
name=Java旅途
age=22
application.properties
<!-- 在application.yml主配置文件中激活test配置文件,否则获取不到 -->
spring.profiles.active = test
该种方式需要读取的配置文件必须要在主配置文件 application.yml 中进行激活,否则获取不到
。
也可以在类中使用注解 @PropertySource(value={“classpath:xxxx.properties”}) 进行指定配置文件。
2、新增一个类用来读取配置文件
@Configuration
public class GetProperties {
@Value("${name}")
private String name;
@Value("${age}")
private String age;
public void getConfig() {
System.out.println("name:" + name);
System.out.println("age:" + age);
}
}
读取配置文件的类必须为注册类,不能有构造方法
:这个类必须是注释有@Bean、@Configuration、@Component、@Service或者@Controller等注解或者其他方式注册为bean,否则不会生效。
3、Controller 中使用配置文件值
public class Controller {
@Autowired
GetProperties getProperties; // 只能用注入方式
@RequestMapping("/getProperties")
public void getProperties(){
System.out.println(getProperties.getConfig());
}
}
读取配置文件的类的类加载方式必须为注入方式
:必须为注释有@Autowired、@Resource等类似注解或者使用其他方式注入,使用方式不能是new一个对象,否则不会生效。
二、@ConfigurationProperties(prefix=“xxx”) + @Component(或@Configuration)
此种方式一般多用于当需要读取多个且前缀相同的属性值。
1、在 resources 下新建两个配置文件 application-user.properties 和主配置文件 application.properties,内容分别如下:
application-user.properties
user.name=lhj
user.age=25
application.properties
<!-- 在application.yml主配置文件中激活 user 配置文件,否则获取不到 -->
spring.profiles.active = user
该种方式需要读取的配置文件必须要在主配置文件 application.yml 中进行激活,否则获取不到
。
也可以在类中使用注解 @PropertySource(value={“classpath:xxxx.properties”}) 进行指定配置文件。
2、新增一个类用来读取配置文件
@Data // 设置 getter 和 setter
@Component
@ConfigurationProperties(prefix = "user") // 指定属性值的前缀
public class ReadValueConfig {
// 每个属性跟配置文件中的属性一一对应
private String age;
private String address;
}
@ConfigurationProperties 可以将配置文件直接映射成一个实体类,然后我们可以直接操作实体类来获取配置文件相关数据
。读取配置文件的类必须为注册类,不能有构造方法
:这个类必须是注释有@Bean、@Configuration、@Component、@Service或者@Controller等注解或者其他方式注册为bean,否则不会生效。
3、使用配置文件的变量值
@Component
public class ReadParamController2 {
@Autowired
ReadValueConfig readValueConfig;
public void readValue() {
String name = readValueConfig.getName();
String age = readValueConfig.getAge();
System.out.println("name:" + name);
System.out.println("age:" + age);
}
}
读取配置文件的类的类加载方式必须为注入方式
:必须为注释有@Autowired、@Resource等类似注解或者使用其他方式注入,使用方式不能是new一个对象,否则不会生效。
三、@Component(或@Configuration) + @PropertySource(value={“classpath:xxxx.properties”}) + @ConfigurationProperties(prefix=“xxxx”)
1、在 resources 下新建配置文件 application-user.properties,内容如下:
application-user.properties
user.name=lhj
user.age=25
该种方式读取的配置文件不需要要在主配置文件 application.yml 中进行激活,因为通过 @PropertySource(value={“classpath:xxxx.properties”}) 来指定配置文件所在的地址,程序就会去加载
。
2、新增一个类用来读取配置文件
@Data // 设置 getter 和 setter
@Component
@PropertySource(value = {"classpath:application-user.properties"}) //指定配置文件所在路径
@ConfigurationProperties(prefix = "user") // 指定属性值的前缀
public class ReadValueConfig {
// 每个属性跟配置文件中的属性一一对应
private String age;
private String address;
}
@ConfigurationProperties 可以将配置文件直接映射成一个实体类,然后我们可以直接操作实体类来获取配置文件相关数据
。读取配置文件的类必须为注册类,不能有构造方法
:这个类必须是注释有@Bean、@Configuration、@Component、@Service或者@Controller等注解或者其他方式注册为bean,否则不会生效。
3、使用配置文件的变量值
@Component
public class ReadParamController2 {
@Autowired
ReadValueConfig readValueConfig;
public void readValue() {
String name = readValueConfig.getName();
String age = readValueConfig.getAge();
System.out.println("name:" + name);
System.out.println("age:" + age);
}
}
读取配置文件的类的类加载方式必须为注入方式
:必须为注释有@Autowired、@Resource等类似注解或者使用其他方式注入,使用方式不能是new一个对象,否则不会生效。
四、@Component(或@Configuration) + @PropertySource(value={“classpath:xxxx.properties”}) + SpringBoot 自带的 Environment
1、在 resources 下新建配置文件 config.properties,内容如下:
config.properties
lhj.config.web-configs.name=Java旅途
lhj.config.web-configs.age=22
2、定义一个类去读取配置文件
@Configuration
@PropertySource(value = {"classpath:config.properties"},encoding="gbk")
public class GetProperties {
@Autowired
Environment environment;
public void getEnvConfig(){
String name = environment.getProperty("lhj.config.web-configs.name");
String age = environment.getProperty("lhj.config.web-configs.age");
System.out.println("name:" + name);
System.out.println("age:" + age);
}
}
该种方式需要读取的配置文件不需要在主配置文件 application.yml 中进行激活。而是通过 @PropertySource(value={“classpath:xxxx.properties”}) 来指定配置文件所在的地址,程序就会去加载
。读取配置文件的类必须为注册类,不能有构造方法
:这个类必须是注释有@Bean、@Configuration、@Component、@Service或者@Controller等注解或者其他方式注册为bean,否则不会生效。