Spring Boot 读取配置文件到静态工具类

Spring的自动注入是无法注入到静态类的

并且一个对象如果手动new的话,这个对象里面的自动注入也是不起作用的

所以如果需要读取配置文件到静态的工具类中

就要使用 @Configuration 和 @Bean注解来调用set方法读取

代码如下:

@Configuration
public class SearchAroundConfigration_jdbc {
	@Autowired
	private Environment env;

	@Bean
	public int readConf() {
		SearchAroundUtil_jdbc.setUsername(env.getProperty("spring.data.neo4j.username"));
		SearchAroundUtil_jdbc.setPassword(env.getProperty("spring.data.neo4j.password"));
		SearchAroundUtil_jdbc.setUri(env.getProperty("spring.data.neo4j.uri").replaceAll("http:", "jdbc:neo4j:"));
		return 1;
	}
}

  

posted @ 2017-06-21 15:17  一只韭菜  阅读(7465)  评论(0编辑  收藏  举报