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