Spring使用外部属性文件
2023-09-27 14:26:12 时间
一、在 Spring Config 文件中配置 Bean 时,有时候需要在 Bean 的配置里添加 系统部署的细节信息, 如文件路径,数据源配置信息。而这些部署细节实际上需要在配置文件外部来定义。
二、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
测试:
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的bug,领导带我出去一起爬山玩水了
- spring使用@Value标签读取*.properties文件的中文乱码问题的解决
- Spring Boot 项目上传日志到 Azure Application Insights
- Spring Websocket实现文本、图片、声音、文件下载及推送、接收及显示(集群模式)
- spring mvc 3.0 实现文件上传功能
- 二、Spring——AoP
- @Autowired的使用--Spring规范解释,推荐对构造函数进行注释
- 使用Spring替换properties文件定义属性的方法
- spring入门编程问题集锦
- Spring MVC实现文件下载
- 快速失败(Fail Fast) Spring Validation
- Spring MVC异常处理SimpleMappingExceptionResolver
- webservice 服务端例子+客户端例子+CXF整合spring服务端测试+生成wsdl文件 +cxf客户端代码自动生成
- spring aop开发常见错误
- spring的循环依赖
- Spring mvc-文件上传与JSON-学习笔记
- Spring下用Java导出CSV文件(java直接导出和第三方jar导出)
- sharding jdbc(sphere) 3.1.0 spring boot配置
- Spring+Mybatis多数据源配置(三)——Spring如何获取Properties文件的信息