情景,使用服务器环境文件,比如服务器地址数据等。
Spring 提供了一个叫做PropertyPlaceholderConfigured的BeanFactory处理器,允许将Bean配置文件 移植到外部文件中,可以在bean中使用 ${var} 形式调用
注册:Spring2.5 后简化的配置方式
- 在 中添加 定义
- 在配置文件中假如如下配置
实例
创建外部属性文件
price=3000
dis=long sitong
speed=250
导入命名空间
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
引用
<!--导入属性文件-->
<context:property-placeholder location="classpath:car.properties"/>
<!--使用属性文件-->
<bean id="casr" class="cn.nokia.pojo.Car" p:price="${price}" p:dis="${dis}" p:speed="${speed}" abstract="true"/>
<bean id="car" class="cn.nokia.pojo.Car" parent="casr"/>
<bean id="huan" autowire="byName" class="cn.nokia.pojo.Persons" p:age="23" p:name="erhuan" depends-on="car"
scope="prototype"></bean>