精通Spring 之 注解【<context:property-placeholder/>】

5.10  外在化应用参数的配置

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

本节将结合beanfactorypostprocessordemo项目展开对它们的讨论。

5.10.1  <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属性,开发者能够控制这一行为。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码是一个Spring MVC项目的配置文件,用于配置一些关键的组件和功能。让我一一解释一下: 1. `<mvc:annotation-driven/>`:启用Spring MVC注解驱动,使得控制器中的注解生效。 2. `<context:component-scan base-package="com.dgy"/>`:指定需要扫描的包,将被注解标记的类作为组件进行扫描。 3. `<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">`:配置视图解析器,用于解析并定位JSP视图文件。 4. `<context:property-placeholder location="classpath:jdbc.properties"/>`:加载属性文件jdbc.properties中的配置信息。 5. `<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">`:配置数据库连接池,使用阿里巴巴Druid连接池。 6. `<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">`:配置MyBatis的SqlSessionFactory,指定数据源。 7. `<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">`:配置MyBatis的Mapper接口扫描器,用于自动扫描并实例化Mapper接口的实现类。 8. `<tx:annotation-driven/>`:启用Spring事务注解驱动,使得注解标记的方法可以被事务管理。 9. `<aop:aspectj-autoproxy/>`:启用AOP自动代理,用于支持基于切面的编程。 10. `<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">`:配置事务管理器,使用Spring的DataSourceTransactionManager,将数据源注入事务管理器。 希望以上解释对您有所帮助!如果您还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值