1.介绍:
@PropertySouce是spring3.1开始引入的基于java config的注解。
通过@PropertySource注解将properties配置文件中的值存储到Spring的 Environment中,Environment接口提供方法去读取配置文件中的值,参数是properties文件中定义的key值。
2.如下配置文件
3.获取配置文件中的属性值:
1.在类上面加上注解:@PropertySource,使用@Value注解获取值
@PropertySource(value = "classpath:wolong/wolong.properties", encoding = "UTF-8")
public class SysWlKeywordServiceImpl{
@Value("${wolong.username}")
private String username;
@Value("${wolong.password}")
private String password;
@Value("${wolong.token}")
private String token;
}
2.使用 Environment对象获取,使用对象getProperty()方法获取对应的属性值
@Configuration
@PropertySource(value = "classpath:wolong/wolong.properties", encoding = "UTF-8")
public class PropertiesWithJavaConfig {
@Autowired
private Environment env;
env.getProperty("wolong.username")
}