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()); } }
相关文章
- STS创建spring boot项目,pom.xml文件第一行报错
- 《Spring 5 官方文档》5. 验证、数据绑定和类型转换(一)
- spring boot admin抛出"status":401,"error":"Unauthorized"异常
- Spring官方发布新成员:Spring GraphQL
- 《Spring MVC学习指南(第2版)》——导读
- spring.xml从外部文件引入数据库配置信息
- Spring配置文件头及xsd文件版本
- spring属性注入
- springboot初体验-不知道怎么创建spring-boot项目?
- 是时候为Spring Boot 3.0做准备了
- Spring Security 实战干货:分布式对象SharedObject
- Spring 官方发起Spring Authorization Server 项目
- 通过Spring来读取文件的各种方法
- Spring自带配置方式链接数据库(没有src新建文件,没有c3p0)
- Spring Boot—12URL映射
- Spring Boot—04文件上传
- Spring Cloud : Gateway 服务网关认识(一)
- Spring 源码分析-循环依赖
- 玩转spring boot——结合AngularJs和JDBC
- Spring中配置和读取多个Properties文件--转
- 曹工说Spring Boot源码(15)-- Spring从xml文件里到底得到了什么(context:load-time-weaver 完整解析)
- 曹工说Spring Boot源码(12)-- Spring解析xml文件,到底从中得到了什么(context:component-scan完整解析)
- 曹工说Spring Boot源码(8)-- Spring解析xml文件,到底从中得到了什么(util命名空间)
- 曹工说Spring Boot源码(7)-- Spring解析xml文件,到底从中得到了什么(上)
- 曹工说Spring Boot源码(6)-- Spring怎么从xml文件里解析bean的
- 曹工说Spring Boot源码(5)-- 怎么从properties文件读取bean
- spring boot +ajax上传文件前后端分离完整实现示例代码
- Spring Boot 2.X 文件上传与下载