@PropertySource
@PropertySource 是spring的注解
加载指定的属性文件的配置到 Spring 的 Environment 中。可以配合 @Value 和 @ConfigurationProperties 使用。
用法:
@Configuration+ @PropertySource+Environment
@Configuration+ @PropertySource+@Value
@Configuration+ @PropertySource+@ConfigurationProperties
@Configuration本质也是告诉spring这是一个bean.
my.name=helloworld
my.age=8
@Configuration+ @PropertySource+Environment
@Configuration
@PropertySource("classpath:hellword.properties")
public class HelloWorldConfig {
@Autowired
private Environment env;
}
@Configuration+ @PropertySource+@Value
@Configuration
@PropertySource("classpath:hellword.properties")
public class HelloWorldConfig {
@Value(${my.name})
private String name;
}
@Configuration+ @PropertySource+@ConfigurationProperties
@PropertySource指定加载哪个文件,@ConfigurationProperties指定加载文件中的哪一类属性。
@PropertySource+@ConfigurationProperties在一起解决了@ConfigurationProperties只能加载主文件内属性问题。
@Configuration
@PropertySource("classpath:hellword.properties")
@ConfigurationProperties(prefix = "my")
public class HelloWorldConfig {
private String name;
}