spring中context:property-placeholder/元素

http://blog.sina.com.cn/s/blog_4550f3ca0100ubmt.html

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}等类似信息。



<bean id="propertyPlaceholderConfigurer"
        class="org.springframework.beans.factory.config.
PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>userinfo.properties</value>
        </list>
    </property>
</bean>  

<bean name="userInfo" class="test.UserInfo">
  <property name="username" value="${db.username}"/>
  <property name="password" value="${db.password}"/>
</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属性,开发者能够控制这一行为。

时间: 2024-08-18 05:49:53

spring中context:property-placeholder/元素的相关文章

spring中context:property-placeholder/元素 转载

spring中context:property-placeholder/元素  转载 1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,password,driverClass等 b.分布式应用中client端访问server端所用的server地址,port,service等 c.配置文件的位置 2.而这些参数在不同阶段之间又往往需要改变 比如:在项目开发阶段和交付阶段数据库的连接信息往往是不同的,分布式应用也是同样的情况. 期望:能不能有一

spring中context:property-placeholder解析

1.替代常量 应用场所:1在开发阶段我们连接数据库时的连接url,username,password,driverClass等 2分布式应用中client端访问server端所用的server地址,port,service等 2.参数需经常变动 在项目开发阶段和交付阶段数据库的连接信息往往是不同的,分布式应用也是同样的情况.  能不能有一种解决方案可以方便我们在一个阶段内不需要频繁书写一个参数的值,而在不同阶段间又可以方便的切换参数配置信息 spring3中提供了一种简便的方式就是context

spring中 context:property-placeholder 导入多个独立的 .properties配置文件

spring中 context:property-placeholder 导入多个独立的 .properties配置文件? Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的 Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceh

spring中 context:property-placeholder 导入多个独立的配置文件

spring中 context:property-placeholder 导入多个独立的 .properties配置文件? Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的 Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceh

关于spring中&lt;context:annotation-config/&gt;配置

在spring的配置文件中,如果我们一个一个地配置bean会非常麻烦,因此我们可以使用注解的方式 使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean: 使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean 类似地,使用@Resource.@PostConstruct.@PreDestroy等注解就必须声明 CommonAnnotatio

深入分析JavaWeb Item54 -- Spring中的AOP面向切面编程2

一.在Advice方法中获取目标方法的参数 1.获取目标方法的信息 访问目标方法最简单的做法是定义增强处理方法时,将第一个参数定义为JoinPoint类型,当该增强处理方法被调用时,该JoinPoint参数就代表了织入增强处理的连接点.JoinPoint里包含了如下几个常用的方法: Object[] getArgs:返回目标方法的参数 Signature getSignature:返回目标方法的签名 Object getTarget:返回被织入增强处理的目标对象 Object getThis:返

通过Spring配置文件中bean中的property赋值

基本数据类型赋值-通过spring配置文件中bean中的property 扩展-以此方式可以通过配置为连接数据的属性赋值 1.如果是基本数据类型,可以通过setter方法为对象中的属性设置初始值,应用:可以把以前写dbc的东西写进去 2.如果属性的类型不是基本类型或String ,可以使用引用的方式为对象赋值(bean中property中的ref) 扩展-以此方式可以把数据库的连接值给实现类赋值 3.集合属性的赋值,注意要集合要初始化.基本数据类型不用初始化的原因就是它默认初始化(不常用) 4.

在完全由Spring管理的环境中使用Spring的Context获取Bean实例

在大型的应用中,常常会有很灵活的需求,而在使用了框架之后,虽然可以大大提高开发的效率,但同时,也把我们框到一个架子中了. 下面先说一下我遇到的问题,事情大概是这样的: @Component @Scope("prototype") public class Action1 implements Action{ ..... } @Component @Scope("prototype") public class Action2 implements Action{ .

Spring中使用注解时启用&lt;context:component-scan/&gt;

在spring中使用注解方式时需要在spring配置文件中配置组件扫描器:http://blog.csdn.net/j080624/article/details/56277315 <context:component-scan>详解:http://outofmemory.cn/java/spring/spring-DI-with-annotation-context-component-scan 原文地址:https://www.cnblogs.com/jeryM/p/8427366.htm