Spring4笔记----使用外部属性文件配置 bean
来源:互联网 发布:矩阵分解应用 编辑:程序博客网 时间:2024/05/21 10:17
在配置文件里配置 Bean 时, 有时需要在Bean的配置里混入系统部署的细节信息(例如:文件路径、 数据源配置信息等)。 而这些部署细节实际上需要和Bean配置相分离。
Spring 提供了一个 PropertyPlaceholderConfigurer的 BeanFactory后置处理器, 这个处理器允许用户将Bean配置的部分内容外移到属性文件中。可以在Bean配置文件里使用形式为 ${var} 的变量,PropertyPlaceholderConfigurer从属性文件里加载属性, 并使用这些属性来替换变量。
Spring 还允许在属性文件中使用 ${propName},以实现属性之间的相互引用。
属性文件:
user=root
password=123456
jdbcUrl=jdbc:mysql:///mybatis driver
Class=com.mysql.jdbc.Driver
配置文件:
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user"value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
</bean>
测试代码:
public void testDataSource() throws SQLException
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); DataSourceds = (DataSource) ctx.getBean("dataSource");
System.out.println(ds.getConnection());
}
- Spring4笔记----使用外部属性文件配置 bean
- Spring4.0学习笔记-bean之间的关系,作用域以及使用外部属性文件
- Spring4学习笔记(七):bean的作用域和使用外部属性文件
- Spring4深入理解IOC&DI02----Bean配置--自动装配、bean之间的继承与依赖、使用外部属性文件
- Spring-Bean配置-使用外部属性文件
- Spring中配置bean时使用外部属性文件
- Spring4----使用注解方式配置bean和bean的属性
- 配置bean之你使用外部属性
- Xml配置里的bean自动装配与使用外部属性文件
- 12.笔记JAVA Spring框架学习————Bean使用外部属性文件
- Spring4学习笔记-通过注解配置bean
- Spring4笔记----工厂方法配置bean
- Spring4笔记----实现 FactoryBean 接口配置 Bean
- Spring4.0学习笔记003——Bean的配置一(基于XML文件)
- Spring4.0学习笔记004——Bean的配置二(基于XML文件)
- Spring4.0学习笔记005——Bean的配置三(基于XML文件)
- Spring4-2 Bean配置
- spring学习总结(六):IOC & DI 配置Bean之作用域、加载外部属性文件、SPEL
- HDU 1874 (迪杰斯特拉 + 邻接表 + 优先队列 模板)
- [leetcode] 20. Valid Parentheses
- JavaScript中的上溢、下溢和被零整除
- poj 2388 堆排序
- 数独
- Spring4笔记----使用外部属性文件配置 bean
- Codeforces Round #363 (Div. 2) A. Launch of Collider
- 【OpenJudge】c语言_计算线段长度
- 物联网项目开发编程语言一览
- jQuery源码研究学习笔记(二)
- 【POJ】[3176]Cow Bowling
- 下滑隐藏导航栏 上滑出现
- 杭电acm1052解题代码(详细注释)
- HTTP网络超时