1:文件位置
2:文件内容
3:加载配置文件的工具类
@Component
public class SystemConfig {
@Bean("syscnf")
private static PropertiesFactoryBean getPropertiesFactoryBean() {
try {
ClassPathResource res = new ClassPathResource("sys.cnf");
PropertiesFactoryBean result = new PropertiesFactoryBean();
result.setLocations(res);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
4:使用方法
@Service
public class LytPassRecordServerImpl implements LytPassRecordServer{
@Value("#{syscnf['lyt.passrecord.url']}")
private String lytPassrecordUrl;
}
注意:有时候在修改完这个配置文件后,重启tomcat这个文件不会重新编译,就会导致取到的值为空,需要注意一下。