Spring---Bean使用外部属性文件

在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean 配置相分离
Spring 提供了一个 PropertyPlaceholderConfigurer 的 BeanFactory 后置处理器, 这个处理器允许用户将 Bean 配置的部分内容外移到属性文件中. 可以在 Bean 配置文件里使用形式为 ${var} 为变量赋值, PropertyPlaceholderConfigurer 从属性文件里加载属性, 并使用这些属性来替换变量.
Spring 还允许在属性文件中使用 ${propName},以实现属性之间的相互引用。

Bean使用外部属性文件实现步骤(以配置c3p0数据源为例)

  1创建c3p0外部属性文件db.properties(采用键值对的方式)

user=root
password=1230
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/test

 2Spring4.0后是使用Context命名空间来配置的,所以需要导入Context命名空间:

  
  

3在配置文件中配置外部文件,和c3p0的Bean实例,代码如下:

<?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-4.0.xsd">

    <context:property-placeholder location="com/jeremy/spring/properties/c3po.properties"/>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <property name="driverClass" value="${driverClass}"></property>
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
    </bean>

</beans>
时间: 2024-08-11 05:45:22

Spring---Bean使用外部属性文件的相关文章

spring bean 读取外部properties文件

方法一:利用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> &l

spring 使用外部属性文件

概要: 使用外部属性文件 在配置文件中配置Bean时.有时须要在Bean的配置里混入系统部署的细节信息(比如:文件路径.数据源配置信息等)而这些部署细节实际上须要和Bean配置相分离 Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,整个处理器同意用户将Bean配置的部分内容外移到属性文件中.能够在Bean配置文件中使用形式为${var}的变量,PropertyPlaceholderConfigurer从属性文件中载入属性,并使用

Spring4学习笔记 - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

1 Autowire自动装配 1.1 使用:只需在<bean>中使用autowire元素 <bean id="student" class="com.kejian.spring.bean.autowire.Student" p:name="Tony" autowire="byName"></bean> 1.2 类型 byName 目标bean的id与属性名一置,若不匹配置为null byTy

使用外部属性文件和spring的事件

一.使用外部属性 使用PropertyPlaceholderConfigurer引用属性文件 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    <property name="location" value="classpath:com/smart/place/jdbc.properties">&

Spring中Bean的作用域、Spring的自动注入、在spring配置文件中引入属性文件

1. Bean的作用域 Bean的作用域默认为单例模式. 2. 自动注入 3. 在spring配置文件中引入属性文件 Bean的作用域默认为单例模式. 原文地址:https://www.cnblogs.com/mcl2238973568/p/11478426.html

十八 Spring的JDBC模板:引入外部属性文件

配置外部属性文件 配置文件里引入属性文件,两种方式 第一种: 第二种: 引入属性文件的值: 测试: 原文地址:https://www.cnblogs.com/ltfxy/p/9888591.html

spring4学习:使用外部属性文件

在配置文件里配置Bean时,有时需要在Bean的配置里混入系统部署的细节信息(例如:文件路径,数据源配置信息等)而这些部署细节实际上需要和Bean配置相分离: spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean配置的内容外移到属性文件中,可以在Bean配置文件里使用形式为${var}的变量,PropertyPlaceholderConfigurer从属性文件里加载属性,并使用这些属性来替换属性变量: S

使用外部属性文件

1.在配置文件里配置Bean时,有时需要在Bean的配置里混入系统部署的细节信息(如:文件路径,数据源配置信息等).而这些部署细节实际上需要和Bean配置相分离. 2.Spring提供了一个PropertyPlacehoiderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean配置的部分内容外移到属性文件中.可以在Bean配置文件里使用形式为${var}的变量,PropertyPlacehoiderConfigurer从属性文件里加载属性,并使用这些属性来替换变

java spring中对properties属性文件加密及其解密

原创整理不易,转载请注明出处:java spring中对properties属性文件加密及其解密 代码下载地址:http://www.zuidaima.com/share/1781588957400064.htm 加密类: package com.zuidaima.commons.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import