@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;
}