`

spring配置中调用properties文件

阅读更多

. system.properties


database.url=jdbc:mysql://localhost/smaple
database.driver=com.mysql.jdbc.Driver
database.user=root
database.password=root

2.applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>>classpath:system.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>${database.url}</value>
</property>
<property name="driverClassName">
<value>${database.driver}</value>
</property>
<property name="username">
<value>${database.user}</value>
</property>
<property name="password">
<value>${database.password}</value>
</property>

</bean>
</beans>

当放入多个配置文件时
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>>classpath:system1.properties</value>
<value>>classpath:system2.properties</value>
</list>
</property>
</bean>

还可以这样
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:*.properties</value>
</list>
</property>
</bean>

 

 

在Spring中,使用PropertyPlaceholderConfigurer和PropertyOverrideConfigurer可以在XML配置文件中加入外部属性文件
但使用这种方式,有一些需要注意的地方

1.首先在主类中,需要使用ClassPathXmlApplicationContext来读取spring配置xml文件
如:

ApplicationContext context = new ClassPathXmlApplicationContext("example4/appcontext.xml");
HelloWorld hw = (HelloWorld)context.getBean("fileHelloWorld");
log.info(hw.getContent());

直接以beanFactory方式,是无法使用PropertyPlaceholderConfigurer或PropertyOverrideConfigurer的
如下方式不行:
Resource resource = new ClassPathResource("example4/appcontext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
HelloWorld hw = (HelloWorld) factory.getBean("fileHelloWorld");
log.info(hw.getContent());

2.PropertyOverrideConfigurer需要考虑bean的名称
如下是正确配置:
appcontext.xml:
<bean name="fileHelloWorld" class="example4.HelloWorld">
    <constructor-arg>
        <ref bean="fileHello"/>
    </constructor-arg>
    <property name="statusname">
        <value>${fileHelloWorld.statusname}</value>
    </property>
</bean>

<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
    <property name="location" value="classpath:example4/helloworld.properties"/>
</bean>

helloworld.properties:
fileHelloWorld.statusname=this is status Name;

如果少了${fileHelloWorld.statusname}中少了Bean名称fileHelloWorld,会导致错误发生
这应该是种强制某个配置是属性某个Bean

3.PropertyPlaceholderConfigurer的配置不需要考虑Bean的名称,直接配置就可以了
配置方式和PropertyOverrideConfigurer类似
分享到:
评论

相关推荐

    Spring-Reference_zh_CN(Spring中文参考手册)

    12.2.2. 在Spring的application context中创建 SessionFactory 12.2.3. HibernateTemplate 12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7. ...

    Struts课堂笔记.rar--struts2的struts.properties配置文件详解

    struts自动加载的一个配置文件列表 struts.configuration.xml.reload Whether to reload the XML configuration or not 是否加载xml配置(true,false) struts.continuations.package The package containing ...

    从零开始学Spring Boot

    1.32 Spring boot 文件上传(多文件上传) 1.33 导入时如何定制spring-boot依赖项的版本 1.34 Spring Boot导入XML配置 1.35 Spring Boot使用@SpringBootApplication注解 1.36 Spring Boot 监控和管理生产环境 1.37 ...

    Spring中文帮助文档

    3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @...

    spring.doc

    2.2 Spring配置文件 7 2.3 Spring API 8 3 Spring基本功能详解 8 3.1 SpringIOC 8 3.2别名Alias 11 别名拓展: 11 3.3 Spring容器内部对象的创建 12 Spring容器内部对象创建拓展: 12 3.3.1使用类构造器实例化(默认...

    SpringBoot入门(三) 之springboot的配置配置文件以及yaml的使用

    配置文件的作用就是来配置某些信息,修改某些默认值,在我们快速创建的springboot项目中就是Application.properties 这里引入一种新的配置文件的形式就是yaml配置文件,后缀名为yml 它以数据为中心,比json和xml更高效 ...

    Spring MVC 入门实例

    配置 SimpleUrlHandlerMapping, 在上面的配置文件中, /hello.do 的请求将被 helloController 处理. "/hello.do"和"helloController" 是变量, 你可以更改. 但是你注意到了吗, hello.do 以 .do 作为后缀名. 如果这里...

    spring杂谈 作者zhang KaiTao

    1.1 Spring事务处理时自我调用的解决方案及一些实现方式的风险 1.2 我对AOP的理解 1.3 Spring开闭原则的表现-BeanPostProcessor的扩展点-1 1.4 我对IoC/DI的理解 1.5 SpringMVC + spring3.1.1 + hibernate4.1.0 集成...

    Spring.net框架

    从中我们可以看出,我们实现了一个IocInCSharp.ConfigHandler类,用来处理配置文件中IocInCSharp\ objects结点中的内容。ConfigHandler类将根据该结点下的内容处理并创建一ConfigInfo对象(关于ConfigInfo、 ...

    Spring 2.0 开发参考手册

    6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 7.2.3. AspectJ切入点表达式 7.2.4. ...

    spring-boot-邮件发送

    添加依赖:在Spring Boot项目的pom.xml文件中加入spring-boot-starter-mail依赖,如果需要发送模板邮件,还可以加入Thymeleaf相关的依赖。 配置邮件:在application.properties或者application.yml中配置邮件发送的...

    ssh(structs,spring,hibernate)框架中的上传下载

     由于Spring通过代理Hibernate完成数据层的操作,所以原Hibernate的配置文件hibernate.cfg.xml的信息也转移到Spring的配置文件中:  代码 4 Spring中有关Hibernate的配置信息 1. 2. !-- 数据源的配置 //--> 3. ...

    Spring API

    3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @...

    tool-link-properties:读取项目配置文件

    将配置文件log4j2.xml和spring-logging.xml从项目目录转移至tool-logging的包目录。 更改“自定义变量”使用“远程配置读取”方式的规则,["dev","test","real"]远程读取,其他则配置文件读取。 更改“全局变量”jsp...

    spring chm文档

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    springboot学习思维笔记.xmind

    SpringEL和资源调用 注入普通字符 注入操作系统属性 注入表达式云算结果 注入其他Bean的属性 注入文件内容 注入网址内容 注入属性文件 Bean的初始化和销毁 Java配置方式 注解方式 ...

    JavaEE开发的颠覆者SpringBoot实战[完整版].part3

    在当今Java EE 开发中,Spring 框架是当之无愧的王者。而Spring Boot 是Spring 主推的基于“习惯优于配置”的原则,让你能够快速搭建应用的框架,从而使得Java EE 开发变得异常简单。 《JavaEE开发的颠覆者: Spring ...

    Spring boot整合Dubbo

    1、Spring Boot 她其实就是整合了Spring MVC和Spring减少了配置的xml用注解和application.properties代替了.xml,其次他还内嵌可tomcat可以用Main函数替代了tomcat。 2、Dubbo又阿里开发的典型的分布式处理框架 ,...

    drools5.2final与spring3集成测试

    此包主要测试drools5.2与spring整合,drools可调用数据库参数 环境准备,mysql/postgres 用到的表及数据见db.sql 修改hibernate.properties对应的参数 运行测试: com/jview/test/testMapping.java ...

    JavaEE开发的颠覆者SpringBoot实战[完整版].part2

    在当今Java EE 开发中,Spring 框架是当之无愧的王者。而Spring Boot 是Spring 主推的基于“习惯优于配置”的原则,让你能够快速搭建应用的框架,从而使得Java EE 开发变得异常简单。 《JavaEE开发的颠覆者: Spring ...

Global site tag (gtag.js) - Google Analytics