spring和redis整合总结

1、spring中context:property-placeholder/元素

1.有些参数在某些阶段中是常量

    比如 :a、在开发阶段我们连接数据库时的连接url,username,password,driverClass等 

                 b、分布式应用中client端访问server端所用的server地址,port,service等  

                  c、配置文件的位置

2.而这些参数在不同阶段之间又往往需要改变

    比如:在项目开发阶段和交付阶段数据库的连接信息往往是不同的,分布式应用也是同样的情况。

期望:能不能有一种解决方案可以方便我们在一个阶段内不需要频繁书写一个参数的值,而在不同阶段间又可以方便的切换参数配置信息

解决:spring3中提供了一种简便的方式就是context:property-placeholder/元素

只需要在spring的配置文件里添加一句:<context:property-placeholder location="classpath:jdbc.properties"/> 即可,这里location值为参数配置文件的位置,参数配置文件通常放在src目录下,而参数配置文件的格式跟java通用的参数配置文件相同,即键值对的形式,例如:

#jdbc配置

test.jdbc.driverClassName=com.mysql.jdbc.Driver
test.jdbc.url=jdbc:mysql://localhost:3306/test
test.jdbc.username=root
test.jdbc.password=root

行内#号后面部分为注释

应用:

1.这样一来就可以为spring配置的bean的属性设置值了,比如spring有一个jdbc数据源的类DriverManagerDataSource

在配置文件里这么定义bean:

<bean id="testDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${test.jdbc.driverClassName}"/>
    <property name="url" value="${test.jdbc.url}"/>
    <property name="username" value="${test.jdbc.username}"/>
    <property name="password" value="${test.jdbc.password}"/>
</bean>

2.甚至可以将${ }这种形式的变量用在spring提供的注解当中,为注解的属性提供值

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

外在化应用参数的配置

在开发企业应用期间,或者在将企业应用部署到生产环境时,应用依赖的很多参数信息往往需要调整,比如LDAP连接、RDBMS JDBC连接信息。对这类信息进行外在化管理显得格外重要。PropertyPlaceholderConfigurer和PropertyOverrideConfigurer对象,它们正是担负着外在化配置应用参数的重任。

  <context:property-placeholder/>元素

PropertyPlaceholderConfigurer实现了BeanFactoryPostProcessor接口,它能够对<bean/>中的属性值进行外在化管理。开发者可以提供单独的属性文件来管理相关属性。比如,存在如下属性文件,摘自userinfo.properties。
db.username=scott
db.password=tiger

如下内容摘自propertyplaceholderconfigurer.xml。正常情况下,在userInfo的定义中不会出现${db.username}、${db.password}等类似信息,这里采用PropertyPlaceholderConfigurer管理username和password属性的取值。DI容器实例化userInfo前,PropertyPlaceholderConfigurer会修改userInfo的元数据信息(<bean/>定义),它会用userinfo.properties中db.username对应的scott值替换${db.username}、db.password对应的tiger值替换${db.password}。最终,DI容器在实例化userInfo时,UserInfo便会得到新的属性值,而不是${db.username}、${db.password}等类似信息。

 
  1. <bean id="propertyPlaceholderConfigurer"   
  2.         class="org.springframework.beans.factory.config.  
  3. PropertyPlaceholderConfigurer">  
  4.     <property name="locations">  
  5.         <list>  
  6.             <value>userinfo.properties</value>  
  7.         </list>  
  8.     </property>  
  9. </bean>  
  10.  
  11. <bean name="userInfo" class="test.UserInfo">  
  12.   <property name="username" value="${db.username}"/>  
  13.   <property name="password" value="${db.password}"/>  
  14. </bean> 

通过运行并分析PropertyPlaceholderConfigurerDemo示例应用,开发者能够深入理解PropertyPlaceholderConfigurer。为简化PropertyPlaceholderConfigurer的使用,Spring提供了<context:property-placeholder/>元素。下面给出了配置示例,启用它后,开发者便不用配置PropertyPlaceholderConfigurer对象了。

 
  1. <context:property-placeholder location="userinfo.properties"/> 

PropertyPlaceholderConfigurer内置的功能非常丰富,如果它未找到${xxx}中定义的xxx键,它还会去JVM系统属性(System.getProperty())和环境变量(System.getenv())中寻找。通过启用systemPropertiesMode和searchSystemEnvironment属性,开发者能够控制这一行为。

****注意*****:如果配置发现报"Could not resolve placeholder" 错,那么 下面就是解决方案

除去properites文件路径错误、拼写错误外,出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlaceholderConfigurer或者多个<context:property-placeholder>的原因。

 

  比如我有一个dao.xml读取dbConnect.properties,还有一个dfs.xml读取dfsManager.properties,然后web.xml统一load这两个xml文件

  1. <context-param>  
  2.         <param-name>contextConfigLocation</param-name>  
  3.         <param-value>  
  4.                 WEB-INF/config/spring/dao.xml,   
  5.                 WEB-INF/config/spring/dfs.xml  
  6.         </param-value>  
  7. </context-param>  

如果这两个xml文件中分别有

  1. <!-- dao.xml -->  
  2. <context:property-placeholder location="WEB-INF/config/db/dbConnect.properties" />  
  3.   
  4. <!-- dfs.xml -->  
  5. <context:property-placeholder location="WEB-INF/config/dfs/dfsManager.properties" />  

那么,一定会出"Could not resolve placeholder"。

 

  一定要记住,不管是在一个Spring文件还是在多个Spring文件被统一load的情况下,直接写

  1. <context:property-placeholder location="" />  
  2. <context:property-placeholder location="" />   

是不允许的。

 

解决方案:

  (1) 在Spring 3.0中,可以写:

  1. <context:property-placeholder location="xxx.properties" ignore-unresolvable="true"  
  2. />  
  3. <context:property-placeholder location="yyy.properties" ignore-unresolvable="true"  
  4. />  

注意两个都要加上ignore-unresolvable="true",一个加另一个不加也是不行的

 

  (2) 在Spring 2.5中,<context:property-placeholder>没有ignore-unresolvable属性,此时可以改用PropertyPlaceholderConfigurer。其实<context:property-placeholder location="xxx.properties" ignore-unresolvable="true" />与下面的配置是等价的

  1. <bean id="随便" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  2.     <property name="location" value="xxx.properties" />  
  3.     <property name="ignoreUnresolvablePlaceholders" value="true" />   
  4. </bean>  

  正因为如此,写多个PropertyPlaceholderConfigurer不加ignoreUnresolvablePlaceholders属性也是一样会出"Could not resolve placeholder"。

 

  虽然两者是的等价的,但估计大家还是喜欢写<context:property-placeholder>多一些,毕竟简单一些嘛。所以如果是Spring 3.0,直接用解决方案(1)再简单不过了;如果是Spring 2.5,需要费点力气改写成PropertyPlaceholderConfigurer

 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值