使用properties文件(两种:1.读取数据库信息 2.将其注入到类中)


  1. 1.在spring框架中,使用properties文件读取里面的数据库配置信息

  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.         http://www.springframework.org/schema/aop  
  7.         http://www.springframework.org/schema/aop/spring-aop.xsd  
  8.         http://www.springframework.org/schema/context  
  9.         http://www.springframework.org/schema/context/spring-context.xsd">  
  10.   
  11.     <context:property-placeholder location="classpath:db.properties" />  
  12.   
  13.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  14.         <property name="user" value="${jdbc.user}"></property>  
  15.         <property name="password" value="${jdbc.password}"></property>  
  16.         <property name="driverClass" value="${jdbc.driverClass}"></property>  
  17.         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>  
  18.         <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>  
  19.         <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>  
  20.     </bean>  
  21.   
  22.   
  23. </beans>  
[html]  view plain  copy
  1. db.properties文件信息  
[plain]  view plain  copy
  1. jdbc.user=root  
  2. jdbc.password=root  
  3. jdbc.driverClass=com.mysql.jdbc.Driver  
  4. jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring  
  5.   
  6.   
  7. jdbc.initPoolSize=5  
  8. jdbc.maxPoolSize=10  



2.将其在springmvc框架中使用value注入到controller里面


我的项目目录结构

test
----- java
--------- com.test
---------------Proporties.java
---------------Test.java
-----resource
--------- applicationContext.xml
--------- application.properties

-------------------------------------------------------------------

application.properties内容

[plain]  view plain  copy
  1. jdbc.code = "this is a code"  

-------------------------------------------------------------------
Proporties.java 源码

[java]  view plain  copy
  1. package com.test;  
  2. import org.springframework.beans.factory.annotation.Value;  
  3. import org.springframework.stereotype.Component;  
  4.   
  5. @Component  
  6. public class Proporties {  
  7.   
  8.     @Value("#{configProperties['jdbc.code']}")  
  9.     private String code;  
  10.   
  11.     public String getCode() {  
  12.         return code;  
  13.     }  
  14.   
  15.     public void setCode(String code) {  
  16.         this.code = code;  
  17.     }  
  18.   
  19.       
  20. }  

-------------------------------------------------------------------

applicationContext.xml 配置

[html]  view plain  copy
  1. <!-- 获取properties中的值 -->  
  2. <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
  3.   <property name="locations">  
  4.     <list>  
  5.     <value>classpath:application.properties</value>  
  6.     </list>  
  7.   </property>  
  8. </bean>  
  9.       
  10. <!-- Spring的动态变量,能在bean中直接调用 -->   
  11. <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  12.     <property name="properties" ref="configProperties" />  
  13. </bean>  
  14.   
  15. <!-- 使用Annotation自动注册Bean ,扫描 Component-->  
  16. <context:component-scan base-package="com.test" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->  
  17.     <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>  
  18. </context:component-scan>  
-------------------------------------------------------------------

Test.java 源码


[java]  view plain  copy
  1. @ContextConfiguration(locations = { "classpath:applicationContext.xml" })    
  2. @RunWith(SpringJUnit4ClassRunner.class  
  3. public class Test{  
  4.   
  5.       
  6.     @Antowird  
  7.     private Proporties proporties;  
  8.   
  9.     @Test  
  10.     public void testProperties(){  
  11.       
  12.     System.out.println("获取的值为:" + proporties.getCode());  
  13.     }  
  14. }  

applicationcontext.xml 中的:


我的项目目录结构

test
----- java
--------- com.test
---------------Proporties.java
---------------Test.java
-----resource
--------- applicationContext.xml
--------- application.properties

-------------------------------------------------------------------

application.properties内容

[plain]  view plain  copy
  1. jdbc.code = "this is a code"  

-------------------------------------------------------------------
Proporties.java 源码

[java]  view plain  copy
  1. package com.test;  
  2. import org.springframework.beans.factory.annotation.Value;  
  3. import org.springframework.stereotype.Component;  
  4.   
  5. @Component  
  6. public class Proporties {  
  7.   
  8.     @Value("#{configProperties['jdbc.code']}")  
  9.     private String code;  
  10.   
  11.     public String getCode() {  
  12.         return code;  
  13.     }  
  14.   
  15.     public void setCode(String code) {  
  16.         this.code = code;  
  17.     }  
  18.   
  19.       
  20. }  

-------------------------------------------------------------------

applicationContext.xml 配置

[html]  view plain  copy
  1. <!-- 获取properties中的值 -->  
  2. <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
  3.   <property name="locations">  
  4.     <list>  
  5.     <value>classpath:application.properties</value>  
  6.     </list>  
  7.   </property>  
  8. </bean>  
  9.       
  10. <!-- Spring的动态变量,能在bean中直接调用 -->   
  11. <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  12.     <property name="properties" ref="configProperties" />  
  13. </bean>  
  14.   
  15. <!-- 使用Annotation自动注册Bean ,扫描 Component-->  
  16. <context:component-scan base-package="com.test" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->  
  17.     <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>  
  18. </context:component-scan>  
-------------------------------------------------------------------

Test.java 源码


[java]  view plain  copy
  1. @ContextConfiguration(locations = { "classpath:applicationContext.xml" })    
  2. @RunWith(SpringJUnit4ClassRunner.class  
  3. public class Test{  
  4.   
  5.       
  6.     @Antowird  
  7.     private Proporties proporties;  
  8.   
  9.     @Test  
  10.     public void testProperties(){  
  11.       
  12.     System.out.println("获取的值为:" + proporties.getCode());  
  13.     }  
  14. }  

1)在Spring-MVC.xml中有以下配置:

 

 

 

<!-- 扫描@Controller注解 -->
<context:component-scan base-package="com.fq.controller">
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

可以看出要把最终的包写上,而不能这样写base-package=”com.fq”。这种写法对于include-filter来讲它都会扫描,而不是仅仅扫描@Controller。哈哈哈,这点需要注意。他一般会导致一个常见的错误,那就是事务不起作用,补救的方法是添加use-default-filters=”false”。

(2)在Spring-common.xml中有如下配置:

<!-- 配置扫描注解,不扫描@Controller注解 -->
<context:component-scan base-package="com.fq">
    <context:exclude-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

可以看到,他是要扫描com.fq包下的所有子类,不包含@Controller。对于exculude-filter不存在包不精确后都进行扫描的问题。







我的项目目录结构

test
----- java
--------- com.test
---------------Proporties.java
---------------Test.java
-----resource
--------- applicationContext.xml
--------- application.properties

-------------------------------------------------------------------

application.properties内容

[plain]  view plain  copy
  1. jdbc.code = "this is a code"  

-------------------------------------------------------------------
Proporties.java 源码

[java]  view plain  copy
  1. package com.test;  
  2. import org.springframework.beans.factory.annotation.Value;  
  3. import org.springframework.stereotype.Component;  
  4.   
  5. @Component  
  6. public class Proporties {  
  7.   
  8.     @Value("#{configProperties['jdbc.code']}")  
  9.     private String code;  
  10.   
  11.     public String getCode() {  
  12.         return code;  
  13.     }  
  14.   
  15.     public void setCode(String code) {  
  16.         this.code = code;  
  17.     }  
  18.   
  19.       
  20. }  

-------------------------------------------------------------------

applicationContext.xml 配置

[html]  view plain  copy
  1. <!-- 获取properties中的值 -->  
  2. <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
  3.   <property name="locations">  
  4.     <list>  
  5.     <value>classpath:application.properties</value>  
  6.     </list>  
  7.   </property>  
  8. </bean>  
  9.       
  10. <!-- Spring的动态变量,能在bean中直接调用 -->   
  11. <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  12.     <property name="properties" ref="configProperties" />  
  13. </bean>  
  14.   
  15. <!-- 使用Annotation自动注册Bean ,扫描 Component-->  
  16. <context:component-scan base-package="com.test" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->  
  17.     <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>  
  18. </context:component-scan>  
-------------------------------------------------------------------

Test.java 源码


[java]  view plain  copy
  1. @ContextConfiguration(locations = { "classpath:applicationContext.xml" })    
  2. @RunWith(SpringJUnit4ClassRunner.class  
  3. public class Test{  
  4.   
  5.       
  6.     @Antowird  
  7.     private Proporties proporties;  
  8.   
  9.     @Test  
  10.     public void testProperties(){  
  11.       
  12.     System.out.println("获取的值为:" + proporties.getCode());  
  13.     }  
  14. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值