詳解SpringMVC加載配置Properties文件的幾種方式
最近開發(fā)的項(xiàng)目使用了SpringMVC的框架,用下來感覺SpringMVC的代碼實(shí)現(xiàn)的非常優(yōu)雅,功能也非常強(qiáng)大,
網(wǎng)上介紹Controller參數(shù)綁定、URL映射的文章都很多了,寫這篇博客主要總結(jié)一下SpringMVC加載配置Properties文件的幾種方式
1.通過context:property-placeholde實(shí)現(xiàn)配置文件加載
1.1、在spring.xml中加入context相關(guān)引用
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
1.2、引入jdbc配置文件
<context:property-placeholder location="classpath:jdbc.properties"/>
1.3、jdbc.properties的配置如下
jdbc_driverClassName=com.mysql.jdbc.Driver jdbc_url=jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8 jdbc_username=root jdbc_password=123456
1.4、在spring-mybatis.xml中引用jdbc中的配置
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close" > <property name="driverClassName"> <value>${jdbc_driverClassName}</value> </property> <property name="url"> <value>${jdbc_url}</value> </property> <property name="username"> <value>${jdbc_username}</value> </property> <property name="password"> <value>${jdbc_password}</value> </property> <!-- 連接池最大使用連接數(shù) --> <property name="maxActive"> <value>20</value> </property> <!-- 初始化連接大小 --> <property name="initialSize"> <value>1</value> </property> <!-- 獲取連接最大等待時(shí)間 --> <property name="maxWait"> <value>60000</value> </property> <!-- 連接池最大空閑 --> <property name="maxIdle"> <value>20</value> </property> <!-- 連接池最小空閑 --> <property name="minIdle"> <value>3</value> </property> <!-- 自動(dòng)清除無用連接 --> <property name="removeAbandoned"> <value>true</value> </property> <!-- 清除無用連接的等待時(shí)間 --> <property name="removeAbandonedTimeout"> <value>180</value> </property> <!-- 連接屬性 --> <property name="connectionProperties"> <value>clientEncoding=UTF-8</value> </property> </bean>
1.5、在Java類中引用jdbc.properties中的配置
import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; @Configuration public class JdbcConfig{ @Value("${jdbc_url}") public String jdbcUrl; //這里變量不能定義成static @Value("${jdbc_username}") public String username; @Value("${jdbc_password}") public String password; }
1.6、在controller中調(diào)用
@RequestMapping("/service/**") @Controller public class JdbcController{ @Autowired private JdbcConfig Config; //引用統(tǒng)一的參數(shù)配置類 @Value("${jdbc_url}") private String jdbcUrl; //直接在Controller引用 @RequestMapping(value={"/test"}) public ModelMap test(ModelMap modelMap) { modelMap.put("jdbcUrl", Config.jdbcUrl); return modelMap; } @RequestMapping(value={"/test2"}) public ModelMap test2(ModelMap modelMap) { modelMap.put("jdbcUrl", this.jdbcUrl); return modelMap; } }
1.7、測(cè)試
在ie中輸入http://localhost:8080/testWeb/service/test 或http://localhost:8080/testWeb/service/test2
返回如下結(jié)果:
{ jdbcUrl:"jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8" }
注:通過context:property-placeholde加載多個(gè)配置文件
只需在第1.2步中將多個(gè)配置文件以逗號(hào)分隔即可
<context:property-placeholder location="classpath:jdbc.properties,classpath:XXX.properties"/>
2、通過util:properties實(shí)現(xiàn)配置文件加載
2.1、在spring.xml中加入util相關(guān)引用
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
2.2、 引入config配置文件
<util:properties id="settings" location="classpath:config.properties"/>
2.3、config.properties的配置如下
gnss.server.url=http://127.0.0.1:8080/gnss/services/data-world/rest
2.4、在java類中引用config中的配置
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Config { @Value("#{settings['gnss.server.url']}") public String GNSS_SERVER_URL; }
2.5、在controller中調(diào)用
@RequestMapping("/service2/**") @Controller public class ConfigController{ @Autowired private Config Config; //引用統(tǒng)一的參數(shù)配置類 @RequestMapping(value={"/test"}) public ModelMap test(ModelMap modelMap) { modelMap.put("gnss.service.url",Config.GNSS_SERVER_URL); return modelMap; } }
2.6、測(cè)試
在ie中輸入http://localhost:8080/testWeb/service2/test
返回如下結(jié)果:
{ "gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest" }
3.直接在Java類中通過注解實(shí)現(xiàn)配置文件加載
3.1、在java類中引入配置文件
import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource(value="classpath:config.properties") public class Config { @Value("${gnss.server.url}") public String GNSS_SERVER_URL; @Value("${gnss.server.url}") public String jdbcUrl; } 3.2、在controller中調(diào)用 @RequestMapping("/service2/**") @Controller public class ConfigController{ @Autowired private Config Config; //引用統(tǒng)一的參數(shù)配置類 @RequestMapping(value={"/test"}) public ModelMap test(ModelMap modelMap) { modelMap.put("gnss.service.url", Config.GNSS_SERVER_URL); return modelMap; } }
3.3、測(cè)試
在ie中輸入http://localhost:8080/testWeb/service2/test
返回如下結(jié)果:
{ "gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest" }
最后附上spring.xml的完整配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 引入jdbc配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 引入多配置文件 --> <context:property-placeholder location="classpath:jdbc.properties,classpath:XXX.properties"/> <!-- 通過util引入config配置文件 --> <!-- <util:properties id="settings" location="classpath:config.properties" /> --> <!-- 掃描文件(自動(dòng)將servicec層注入) --> <context:component-scan base-package="修改成你的Config類所在的package"/></beans>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java基于裝飾者模式實(shí)現(xiàn)的圖片工具類實(shí)例【附demo源碼下載】
這篇文章主要介紹了Java基于裝飾者模式實(shí)現(xiàn)的圖片工具類,結(jié)合完整實(shí)例形式分析了裝飾者模式實(shí)現(xiàn)圖片的判斷、水印、縮放、復(fù)制等功能,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-09-09java 序列化對(duì)象 serializable 讀寫數(shù)據(jù)的實(shí)例
java 序列化對(duì)象 serializable 讀寫數(shù)據(jù)的實(shí)例,需要的朋友可以參考一下2013-03-03java開發(fā)validate方法中校驗(yàn)工具類詳解
這篇文章主要為大家介紹了java開發(fā)validate方法中校驗(yàn)工具類詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09基于Spring BeanUtils的copyProperties方法使用及注意事項(xiàng)
這篇文章主要介紹了基于Spring BeanUtils的copyProperties方法使用及注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06一文探究ArrayBlockQueue函數(shù)及應(yīng)用場(chǎng)景
這篇文章主要為大家介紹了一文探究ArrayBlockQueue函數(shù)及應(yīng)用場(chǎng)景,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Spring中@Value讀取properties作為map或list的操作
這篇文章主要介紹了Spring中@Value讀取properties作為map或list的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Java實(shí)現(xiàn)單向鏈表反轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)單向鏈表反轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03