详解SpringMVC加载配置Properties文件的几种方式
最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大,
网上介绍Controller参数绑定、URL映射的文章都很多了,写这篇博客主要总结一下SpringMVC加载配置Properties文件的几种方式
1.通过context:property-placeholde实现配置文件加载
1.1、在spring.xml中加入context相关引用
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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-placeholderlocation="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中的配置
<beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource"init-method="init"
destroy-method="close">
<propertyname="driverClassName">
<value>${jdbc_driverClassName}</value>
</property>
<propertyname="url">
<value>${jdbc_url}</value>
</property>
<propertyname="username">
<value>${jdbc_username}</value>
</property>
<propertyname="password">
<value>${jdbc_password}</value>
</property>
<!--连接池最大使用连接数-->
<propertyname="maxActive">
<value>20</value>
</property>
<!--初始化连接大小-->
<propertyname="initialSize">
<value>1</value>
</property>
<!--获取连接最大等待时间-->
<propertyname="maxWait">
<value>60000</value>
</property>
<!--连接池最大空闲-->
<propertyname="maxIdle">
<value>20</value>
</property>
<!--连接池最小空闲-->
<propertyname="minIdle">
<value>3</value>
</property>
<!--自动清除无用连接-->
<propertyname="removeAbandoned">
<value>true</value>
</property>
<!--清除无用连接的等待时间-->
<propertyname="removeAbandonedTimeout">
<value>180</value>
</property>
<!--连接属性-->
<propertyname="connectionProperties">
<value>clientEncoding=UTF-8</value>
</property>
</bean>
1.5、在Java类中引用jdbc.properties中的配置
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.context.annotation.Configuration;
@Configuration
publicclassJdbcConfig{
@Value("${jdbc_url}")
publicStringjdbcUrl;//这里变量不能定义成static
@Value("${jdbc_username}")
publicStringusername;
@Value("${jdbc_password}")
publicStringpassword;
}
1.6、在controller中调用
@RequestMapping("/service/**")
@Controller
publicclassJdbcController{
@Autowired
privateJdbcConfigConfig;//引用统一的参数配置类
@Value("${jdbc_url}")
privateStringjdbcUrl;//直接在Controller引用
@RequestMapping(value={"/test"})
publicModelMaptest(ModelMapmodelMap){
modelMap.put("jdbcUrl",Config.jdbcUrl);
returnmodelMap;
}
@RequestMapping(value={"/test2"})
publicModelMaptest2(ModelMapmodelMap){
modelMap.put("jdbcUrl",this.jdbcUrl);
returnmodelMap;
}
}
1.7、测试
在ie中输入http://localhost:8080/testWeb/service/test或http://localhost:8080/testWeb/service/test2
返回如下结果:
{
jdbcUrl:"jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8"
}
注:通过context:property-placeholde加载多个配置文件
只需在第1.2步中将多个配置文件以逗号分隔即可
<context:property-placeholderlocation="classpath:jdbc.properties,classpath:XXX.properties"/>
2、通过util:properties实现配置文件加载
2.1、在spring.xml中加入util相关引用
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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:propertiesid="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中的配置
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.stereotype.Component;
@Component
publicclassConfig{
@Value("#{settings['gnss.server.url']}")
publicStringGNSS_SERVER_URL;
}
2.5、在controller中调用
@RequestMapping("/service2/**")
@Controller
publicclassConfigController{
@Autowired
privateConfigConfig;//引用统一的参数配置类
@RequestMapping(value={"/test"})
publicModelMaptest(ModelMapmodelMap){
modelMap.put("gnss.service.url",Config.GNSS_SERVER_URL);
returnmodelMap;
}
}
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类中引入配置文件
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource(value="classpath:config.properties")
publicclassConfig{
@Value("${gnss.server.url}")
publicStringGNSS_SERVER_URL;
@Value("${gnss.server.url}")
publicStringjdbcUrl;
}
3.2、在controller中调用
@RequestMapping("/service2/**")
@Controller
publicclassConfigController{
@Autowired
privateConfigConfig;//引用统一的参数配置类
@RequestMapping(value={"/test"})
publicModelMaptest(ModelMapmodelMap){
modelMap.put("gnss.service.url",Config.GNSS_SERVER_URL);
returnmodelMap;
}
}
3.3、测试
在ie中输入http://localhost:8080/testWeb/service2/test
返回如下结果:
{
"gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"
}
最后附上spring.xml的完整配置:
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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-placeholderlocation="classpath:jdbc.properties"/> <!--引入多配置文件--> <context:property-placeholderlocation="classpath:jdbc.properties,classpath:XXX.properties"/> <!--通过util引入config配置文件--> <!--<util:propertiesid="settings"location="classpath:config.properties"/>--> <!--扫描文件(自动将servicec层注入)--> <context:component-scanbase-package="修改成你的Config类所在的package"/></beans>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。