最近開發的項目使用了SpringMVC的架構,用下來感覺SpringMVC的代碼實作的非常優雅,功能也非常強大,
網上介紹Controller參數綁定、URL映射的文章都很多了,寫這篇部落客要總結一下SpringMVC加載配置Properties檔案的幾種方式
1.通過context:property-placeholde實作配置檔案加載
1.1、在spring.xml中加入context相關引用
<b>[html]</b> view plain copy
<?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 name="username">
<value>${jdbc_username}</value>
<property name="password">
<value>${jdbc_password}</value>
<!-- 連接配接池最大使用連接配接數 -->
<property name="maxActive">
<value>20</value>
<!-- 初始化連接配接大小 -->
<property name="initialSize">
<value>1</value>
<!-- 擷取連接配接最大等待時間 -->
<property name="maxWait">
<value>60000</value>
<!-- 連接配接池最大空閑 -->
<property name="maxIdle">
<!-- 連接配接池最小空閑 -->
<property name="minIdle">
<value>3</value>
<!-- 自動清除無用連接配接 -->
<property name="removeAbandoned">
<value>true</value>
<!-- 清除無用連接配接的等待時間 -->
<property name="removeAbandonedTimeout">
<value>180</value>
<!-- 連接配接屬性 -->
<property name="connectionProperties">
<value>clientEncoding=UTF-8</value>
</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中調用
@RequestMapping("/service/**")
@Controller
public class JdbcController{
@Autowired
private JdbcConfig Config; //引用統一的參數配置類
@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、測試
在ie中輸入http://localhost:8080/testWeb/service/test 或http://localhost:8080/testWeb/service/test2
傳回如下結果:
<b>[java]</b> view plain copy
{
jdbcUrl:"jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8"
注:通過context:property-placeholde加載多個配置檔案
隻需在第1.2步中将多個配置檔案以逗号分隔即可
<context:property-placeholder location="classpath:jdbc.properties,classpath:XXX.properties"/>
2、通過util:properties實作配置檔案加載
2.1、在spring.xml中加入util相關引用
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
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.stereotype.Component;
@Component
public class Config {
@Value("#{settings['gnss.server.url']}")
public String GNSS_SERVER_URL;
2.5、在controller中調用
@RequestMapping("/service2/**")
public class ConfigController{
private Config Config; //引用統一的參數配置類
public ModelMap test(ModelMap modelMap) {
modelMap.put("gnss.service.url",Config.GNSS_SERVER_URL);
return modelMap;
}
2.6、測試
在ie中輸入http://localhost:8080/testWeb/service2/test
"gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"
3.直接在Java類中通過注解實作配置檔案加載
3.1、在java類中引入配置檔案
import org.springframework.context.annotation.PropertySource;
@PropertySource(value="classpath:config.properties")
@Value("${gnss.server.url}")
public String GNSS_SERVER_URL;
public String jdbcUrl;
3.2、在controller中調用
@RequestMapping(value={"/test"})
modelMap.put("gnss.service.url", Config.GNSS_SERVER_URL);
3.3、測試
}
最後附上spring.xml的完整配置:
<!-- 引入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" /> -->
<!-- 掃描檔案(自動将servicec層注入) -->
<context:component-scan base-package="修改成你的Config類所在的package"/></beans>