spring注入配置文件屬性到j(luò)ava類
引言
在許多時(shí)候,我們需要把一些全局的參數(shù)配置到配置文件里面,提供給java程序使用,為了減少代碼量及高閱讀性,理想的是把我們所需要的全局屬性注入到類里面,由程序代碼直接引用.
普通引入properties方法
在spring的配置文件applicationContext.xml配置
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:application.properties</value>
</list>
</property>
</bean>改進(jìn)后的properties引入方法
在spring的配置文件applicationContext.xml配置
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:application.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties"></property>
</bean>application.properties文件配置內(nèi)容
# 默認(rèn)頭像 userDefaultHeaderUrl=http://www.maoyupeng.com/Male.png
java類的使用示例
@Controller
@RequestMapping(value = "/userController")
public class userController {
private static final Logger logger = Logger.getLogger(UserProjectController.class);
@Value("#{configProperties['userDefaultHeaderUrl']}")
private String userDefaultHeaderUrl;
}以上就是spring注入配置文件屬性到j(luò)ava類的詳細(xì)內(nèi)容,更多關(guān)于spring注入配置文件屬性到j(luò)ava類的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于數(shù)據(jù)庫 + JWT 的 Spring Boot Secu
該示例展示了如何使用Spring Boot Security和JWT實(shí)現(xiàn)用戶數(shù)據(jù)庫存儲(chǔ)、無狀態(tài)認(rèn)證和角色權(quán)限控制,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2026-01-01
PowerJob的AliOssService工作流程源碼解讀
這篇文章主要介紹了PowerJob的AliOssServiceg工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Java連接MySQL數(shù)據(jù)庫并實(shí)現(xiàn)數(shù)據(jù)交互功能
在現(xiàn)代應(yīng)用中,數(shù)據(jù)庫是不可或缺的一部分,Java 作為一種廣泛使用的編程語言,提供了豐富的 API 來與各種數(shù)據(jù)庫進(jìn)行交互,本文將詳細(xì)介紹如何在 Java 中連接 MySQL 數(shù)據(jù)庫,并實(shí)現(xiàn)基本的數(shù)據(jù)交互功能,需要的朋友可以參考下2024-10-10

