`
qilixiang012
  • 浏览: 203848 次
文章分类
社区版块
存档分类
最新评论

spring 使用外部属性文件

 
阅读更多

概要:



使用外部属性文件

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


注册PropertyPlaceholderConfigurer
  • Spring 2.0:

  • Spring2.5之后:可通过<context:property-placeholder>元素简化:
    • <beans>中增加centext Schema定义(context命名空间 )
    • 在配置文件中加入如下配置:



实例代码详解:
目录结构

因为要连接数据源:所以要加入mysql和c3p0的jar包


Main.java
package com.coslay.beans.properties;

import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args) throws SQLException {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"beans-properties.xml");

		DataSource dataSource = (DataSource) ctx.getBean("dataSource");

		System.out.println(dataSource.getConnection());
	}
}


beans-properties.xml
<?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="classpath:db.properties"/>


	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 使用外部属性文件的属性 -->
		<property name="user" value="root"></property>
		<property name="password" value="root"></property>
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///test"></property>
	</bean>
</beans>

db.properties

user=root
password=root
driverclass=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql:///test


分享到:
评论

相关推荐

    JAVA Spring使用外部属性文件

    JAVA Spring使用外部属性文件,分享给需要的人,0.0。。

    在Spring中使用加密外部属性文件

    在Spring的开发中,我们在很多情况下会使用占位符引用属性文件的属性值来简化我们的系统及使我们的系统具有更高的灵活性和通用性。这种配置方式有两个明显的好处: ?- 减少维护的工作量:资源的配置信息可以多应用...

    SSH笔记-通过property-placeholder使用外部属性文件

    SSH笔记-通过property-placeholder使用外部属性文件的demo

    说说在Spring中如何引用外部属性文件的方法

    主要介绍了说说在Spring中如何引用外部属性文件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    尚硅谷佟刚Spring4代码及PPT.rar

    代码及ppt涵盖 Spring4.0 的所有核心内容:在 Eclipse 中安装 SpringIDE 插件、IOC & DI、在 Spring 中配置 Bean、自动装配、Bean 之间的关系(依赖、继承)、Bean 的作用域、使用外部属性文件、SpEL、管理 Bean 的...

    ybl-oastatusreport-spring-boot:使用Spring Boot框架处理文本文件以及使用外部配置属性文件

    这两个文件的路径可以作为命令行提供,也可以在外部属性文件中提供。 由于我在Window的PC上使用WSL,因此我想在cmd.exe和Linux Shell上同时运行via CLI参数和外部配置道具文件。 这使我探索了Windows与Linux上文件...

    spring4示例代码

    spring的生命周期及BeanPostProcessor的使用,注解方式创建bean 及使用Autowried标签关联类的属性 ,泛型依赖注入的使用 spring-3 演示使用动态代理模式实现面向切面编程 使用注解方式进行AOP编程及使用配置xml方式...

    Spring相关测试1全部

    Spring中注入属性值细节、自动装配、bean之间的关系、bean的作用域、使用外部属性文件、SpEL、IOC容器中Bean 的生命周期、泛型依赖注入。

    18 Spring IoC容器如何读取应用外部的xml,txt,图形或者属性文件?慕课专栏(1)1

    背景我们使用 Spring IoC 容器的时候,配置文件一般是放到到应用内部,如下所示:我们往往使用 ClassPathXmlApplicationContex

    Spring Boot 把配置文件和日志文件放到jar外部

    如果不想使用默认的application.properties,而想将属性文件放到jar包外面,怎么做呢?下面小编给大家带来了两种方法解决Spring Boot 把配置文件和日志文件放到jar外部问题,感兴趣的朋友一起看看吧

    Spring.3.x企业应用开发实战(完整版).part2

    5.3.1 使用外部属性文件 5.3.2 使用加密的属性文件 5.3.3 属性文件自身的引用 5.4 引用Bean的属性值 5.5 国际化信息 5.5.1 基础知识 5.5.2 MessageSource 5.5.3 容器级的国际化信息资源 5.6 容器事件 5.6.1 Spring...

    引用外部文件配置C3P0连接池.zip

    引用外部属性文件配置C3P0连接池。本博客https://blog.csdn.net/qq_40634846有从零基础入门spring,有兴趣可关注本博客。希望对大家有用

    Spring攻略(第二版 中文高清版).part1

    1.7 使用Spring的FactoryBean创建Bean 27 1.7.1 问题 27 1.7.2 解决方案 27 1.7.3 工作原理 27 1.8 使用工厂Bean和Utility Schema定义集合 29 1.8.1 问题 29 1.8.2 解决方案 29 1.8.3 工作原理 29 ...

    Spring.net框架

    Assembly.LoadFile()用于将外部文件装载进来;assembly.CreateInstance()根据装载进来的程序集创建一指定类型的对象;t.InvokeMember(prop.propertyName, ........BindingFlags.SetProperty, null, o, new Object[] ...

    Spring3.x企业应用开发实战(完整版) part1

    5.3.1 使用外部属性文件 5.3.2 使用加密的属性文件 5.3.3 属性文件自身的引用 5.4 引用Bean的属性值 5.5 国际化信息 5.5.1 基础知识 5.5.2 MessageSource 5.5.3 容器级的国际化信息资源 5.6 容器事件 5.6.1 Spring...

    Spring Boot中文文档.rar

    Spring Boot文件 1.关于文档 2.获得帮助 3.第一步 4.使用Spring Boot 5.了解Spring Boot功能 6.转向生产 7.高级主题 II。入门 8.介绍Spring Boot 9.系统要求 9.1.Servlet容器 10....

    Spring攻略(第二版 中文高清版).part2

    1.7 使用Spring的FactoryBean创建Bean 27 1.7.1 问题 27 1.7.2 解决方案 27 1.7.3 工作原理 27 1.8 使用工厂Bean和Utility Schema定义集合 29 1.8.1 问题 29 1.8.2 解决方案 29 1.8.3 工作原理 29 ...

    spring学习过程

    spring基础,主要讲解了 spring的autowire:自动装配 collections:属性配置的细节 properties:外部属性文件的使用 relati:bean之间的关系 scope:bean的作用域 spel:spel的使用

    spring-nats:用于 NATS 的 Spring Cloud 流绑定器

    如果您希望应用程序文件包含连接信息,它应该是一个属性文件而不是 YAML。 换句话说: spring.cloud.stream.bindings.input.destination=dataIn spring.cloud.stream.bindings.input.binder=nats1 spring.cloud....

    Spring in Action(第2版)中文版

    5.3.3使用spring对jdbc的dao支持类 5.4在spring里集成hibernate 5.4.1选择hibernate的版本 5.4.2使用hibernate模板 5.4.3建立基于hibernate的dao 5.4.4使用hibernate3上下文会话 5.5spring和java持久api ...

Global site tag (gtag.js) - Google Analytics