SpringMVC中如何读取配置文件中的值

青旅半醒 2022-08-06 13:24 281阅读 0赞

第一种情况是在测试类中读取配置文件中的值,实现如下:

1、在spring-mybatis配置文件中引入配置文件,代码如下:

  1. <span style="font-family:Comic Sans MS;"> <!-- 引入配置文件 -->
  2. <bean id="prpertyConfigurer"
  3. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  4. <property name="locations">
  5. <list>
  6. <value>classpath:jdbc.properties</value>
  7. </list>
  8. </property>
  9. </bean></span>

2、为了让controller读取到配置文件,仍需要在spring.xml文件中引入配置文件

  1. <span style="font-family:Comic Sans MS;"><context:property-placeholder location="classpath:jdbc.properties" /></span>

3、在测试类中写成如下形式:

  1. <span style="font-family:Comic Sans MS;">@RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations={"classpath:spring-mybatis.xml","classpath:spring.xml"})
  3. public class TestMyBatis{
  4. //这是对应配置文件中的变量,给变量注入值
  5. @Value("${picUrl}")
  6. private String picUrl;
  7. 。。。。。。</span>

这样便自动将配置文件中picUrl变量的值注入给了picUrl,可以测试打印输出其值,发现就是配置文件中的值。

第二种情况是在Controller中读取配置文件中的值

之前我写了两个配置文件,想同时加载,但是发现一直出现各种问题。原来在spring.xml文件中写两个这样的:

  1. <span style="font-family:Comic Sans MS;"> <context:property-placeholder location="classpath:jdbc.properties" />
  2. <context:property-placeholder location="classpath:picPath.properties"/></span>

这样是行不通的。

后来我将这两个配置文件的内容合并,保存在了jdbc.properties中,便可以在Controller利用@Value对属性值进行注入了。

看看我的Controller类的写法:

  1. <span style="font-family:Comic Sans MS;">@Controller
  2. @Configuration
  3. @ImportResource("classpath:spring.xml")
  4. @RequestMapping("/photo")
  5. public class FileUploadController {
  6. @RequestMapping(value="/tofile")
  7. public String toFileUpLoad(HttpServletRequest request,Model model){
  8. return "fileUpLoad";
  9. }
  10. //@Value("#{settings['picPath.picUrl']}")
  11. @Value("${picUrl}")
  12. private String picUrl;
  13. public void setPicUrl(String picUrl) {
  14. this.picUrl = picUrl;
  15. }</span>

注意一定要引入配置文件,在类名上加上,如下两个注解,不然给picUrl变量注入的值不是配置文件中的值,而是字符串“${picUrl}”

  1. <span style="font-family:Comic Sans MS;"><strong>@Configuration
  2. @ImportResource("classpath:spring.xml")</strong></span>

下面来分析下之前分散到两个配置文件中加载不成功的原因:

比如有A和B两个模块

A模块和B模块都分别拥有自己的Spring XML配置,并分别拥有自己的配置文件

A模块:

A模块的spring配置文件如下:

  1. <span style="font-family:Comic Sans MS;"><?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  8. <context:property-placeholder location="classpath*:conf/conf_a.properties"/>
  9. <bean class="com.xxx.<span style="color:#cc0000;">aaa.Bean1</span>"
  10. p:driverClassName="${modulea.jdbc.driverClassName}"
  11. p:url="${modulea.jdbc.url}"
  12. p:username="${modulea.jdbc.username}"
  13. p:password="${modulea.jdbc.password}"/>
  14. </beans> </span>

其配置文件位于类路径conf/conf_a.properties中:

  1. <span style="font-family:Comic Sans MS;">modulea.jdbc.driverClassName=com.mysql.jdbc.Driver
  2. modulea.jdbc.username=cartan
  3. modulea.jdbc.password=superman
  4. modulea.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8</span>

B模块:

B模块的Spring配置文件如下:

  1. <span style="font-family:Comic Sans MS;"><?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  8. <context:property-placeholder location="classpath*:conf/conf_b.properties"/>
  9. <bean class="com.xxx.<span style="color:#cc0000;">bbb.Bean1</span>"
  10. p:driverClassName="${moduleb.jdbc.driverClassName}"
  11. p:url="${moduleb.jdbc.url}"
  12. p:username="${moduleb.jdbc.username}"
  13. p:password="${moduleb.jdbc.password}"/>
  14. </beans> </span>

其配置文件位于类路径conf/conf_b.properties中:

  1. <span style="font-family:Comic Sans MS;">moduleb.jdbc.driverClassName=com.mysql.jdbc.Driver
  2. moduleb.jdbc.username=cartan
  3. moduleb.jdbc.password=superman
  4. moduleb.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8</span>

问题出现了,单独运行A模块或B模块都是正常,但是将A和B两个模块集成后运行,Spring容器就启动不了:

出现的问题:Could not resolve placeholder ‘moduleb.jdbc.driverClassName’ in string value “${moduleb.jdbc.driverClassName}“
问题的原因是:Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean
就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring3.1已经使用PropertySourcesPlaceholderConfigurer替代
PropertyPlaceholderConfigurer了)。
这个基于命名空间的配置,其实内部就是创建一个PropertyPlaceholderConfigurer Bean而已。换句话说,
即Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:property-placeholder),其余的会被Spring忽略掉。

拿上来的例子来说,如果A和B模块是单独运行的,由于Spring容器都只有一个PropertyPlaceholderConfigurer,因此属性文件会被正常加载并替换掉。如果A和B两模块集成后运行,Spring容器中就有两个PropertyPlaceholderConfigurer Bean了,这时就看谁先谁后了,
先的保留,后的忽略!因此,只加载到了一个属性文件,因而造成无法正确进行属性替换的问题。

解决办法:
属性文件加载在统一的地方做,不需要分模块加载

A模块a.xml:

  1. <span style="font-family:Comic Sans MS;"><?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  8. <!--<context:property-placeholder location="classpath*:conf/conf_a.properties"/>-->
  9. <bean class="com.xxx.<strong><span style="color:#cc0000;">aaa.Bean1</span></strong>"
  10. p:driverClassName="${modulea.jdbc.driverClassName}"
  11. p:url="${modulea.jdbc.url}"
  12. p:username="${modulea.jdbc.username}"
  13. p:password="${modulea.jdbc.password}"/>
  14. </beans> </span>

B模块b.xml:

  1. <span style="font-family:Comic Sans MS;"><?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  8. <!--<context:property-placeholder location="classpath*:conf/conf_b.properties"/>-->
  9. <bean class="com.xxx.<strong><span style="color:#ff0000;">bbb.Bean1</span></strong>"
  10. p:driverClassName="${moduleb.jdbc.driverClassName}"
  11. p:url="${moduleb.jdbc.url}"
  12. p:username="${moduleb.jdbc.username}"
  13. p:password="${moduleb.jdbc.password}"/>
  14. </beans></span>

集成:

  1. <span style="font-family:Comic Sans MS;"><?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  8. <context:property-placeholder location="classpath*:conf/conf*.properties"/>
  9. <import resource="a.xml"/>
  10. <import resource="b.xml"/>
  11. </beans> </span>

Spring为什么要这样呢?细想想是有道理的,一个项目或一个系统的配置应该放在一起,不宜分散。
这样才可以做到统一管控,否则到处都有配置,到底是加载哪个配置文件呢?有时你还会不小心让JAR中的Spring配置文件加载一个位于JAR中的属性文件,
而外面又更改不了。如果Spring使用了这种机制,即使JAR包中的Spring配置文件使用引用到JAR中的属性文件,只要Spring配置文件中显示提供一个指定另一个属性文件 ,就可以覆盖JAR中的默认配置了。

发表评论

表情:
评论列表 (有 0 条评论,281人围观)

还没有评论,来说两句吧...

相关阅读

    相关 spring-boot读取配置文件

    前言 任何一个项目不可能少得了配置文件, 所以配置文件在spring-boot中有很多呈现方式,本篇主要介绍一下最常用的配置文件的方式,代码依附于第一篇开发的简单Hello