Spring使用外部属性文件
2017-03-22 10:14
309 查看
一、在 Spring Config 文件中配置 Bean 时,有时候需要在 Bean 的配置里添加 系统部署的细节信息, 如文件路径,数据源配置信息。而这些部署细节实际上需要在配置文件外部来定义。
二、Spring 提供了一个 PropertyPlaceholderConfigurer 的 BeanFactory 后置处理器。这个处理器允许用户将 Bean 的配置部分内容外移到属性文件中,然后可以在 Bean 的配置文件
里使用形式为 ${var}的变量,PropertyPlaceholderConfigurer 从属性文件里加载属性,并使用这些属性来替换变量。
三、Spring 还允许在属性文件中使用 ${key},以属性间的互相引用。
四、使用:需要注册 PropertyPlaceholderConfigurer 。通过 <context:property-placeholder location="props.properties"/> 这种方式来指定属性文件。
db.properties
在XML中我们需要使用&来转译;
而在properties中则不需要。
测试:
二、Spring 提供了一个 PropertyPlaceholderConfigurer 的 BeanFactory 后置处理器。这个处理器允许用户将 Bean 的配置部分内容外移到属性文件中,然后可以在 Bean 的配置文件
里使用形式为 ${var}的变量,PropertyPlaceholderConfigurer 从属性文件里加载属性,并使用这些属性来替换变量。
三、Spring 还允许在属性文件中使用 ${key},以属性间的互相引用。
四、使用:需要注册 PropertyPlaceholderConfigurer 。通过 <context:property-placeholder location="props.properties"/> 这种方式来指定属性文件。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="root"/> <property name="password" value="lgh123"/> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?user=root&password=lgh123&useUnicode=true&characterEncoding=UTF8&useSSL=true"/> </bean>--> <!--导入属性文件--> <context:property-placeholder location="classpath:db.properties"/> <!--使用外部的属性文件的属性配置--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${user}"/> <property name="password" value="${password}"/> <property name="driverClass" value="${driverClass}"/> <property name="jdbcUrl" value="${jdbcUrl}"/> </bean> </beans>
db.properties
user=root password=lgh123 driverClass=com.mysql.jdbc.Driver jdbcUrl=jdbc:mysql://localhost:3306/test?user=root&password=lgh123&useUnicode=true&characterEncoding=UTF8&useSSL=true
在XML中我们需要使用&来转译;
而在properties中则不需要。
测试:
package com.xiya.spring.beans.properties; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.sql.DataSource; import java.sql.SQLException; /** * Created by N3verL4nd on 2017/3/22. */ public class Main { public static void main(String[] args) throws SQLException { ApplicationContext context = new ClassPathXmlApplicationContext("beans-properties.xml"); DataSource dataSource = (DataSource) context.getBean("dataSource"); System.out.println(dataSource.getConnection()); } }
相关文章推荐
- 【跟我学spring 4.0 】之第七节-spring使用外部属性文件-spring配置连接oracle数据库
- Spring基础—— 在 Spring Config 中使用外部属性文件
- Spring_使用外部属性文件
- Spring 使用外部属性文件
- spring4学习:使用外部属性文件
- Spring 学习笔记 8. 尚硅谷_佟刚_Spring_使用外部属性文件
- Spring-6 使用外部属性文件
- Spring---Bean使用外部属性文件
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
- Spring4 学习系列之——使用外部属性文件
- 在Spring中如何使用加密外部属性文件
- (八)spring_使用外部属性文件
- Spring学习笔记之使用外部属性文件
- Spring-Bean配置-使用外部属性文件
- Spring-7:使用外部属性文件
- Spring IOC使用外部属性文件
- spring_scope作用域和使用外部属性文件和spEL表达式
- Spring学习历程---使用外部属性文件
- spring 使用外部属性文件