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

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM