spring如何引用properties文件里的配置

1.PropertyPlaceholderConfigurer类
它是把属性中的定义的变量(var)替代,spring的配置文件中使用${var}的占位符

<beans>
<bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
           <property name="location"><value>db.properties</value></property>
</bean> 
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
    <property name="url"><value>${jdbc.url}</value></property>
    <property name="username"><value>${jdbc.username}</value></property>
    <property name="password"><value>${jdbc.password}</value></property>
</bean>
</beans>

db.properties文件

jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root

2.PropertyOverrideConfigurer类
跟PropertyPlaceholderConfigurer功能一样,不过用法不一样.不用占位符,在属性文件中
直接定义属性的值,这样就允许有默认值

<beans>
<bean id="configBean" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
           <property name="location"><value>db.properties</value></property>
</bean> 
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property>
    <property name="url"><value>jdbc:hsqldb:hsql://production:9002</value></property>
    <property name="username"><value>test</value></property>
    <property name="password"><value>123456</value></property>
</bean>
</beans>

db.properties文件

dataSource.username=admin
dataSource.password=9527

在bean实例时,admin,9527将替代test,123456
3其他
1)如果需要引用多个属性,将configBean属性改为

<property name="locations">
<list>
<value>db.properties</value>
<value>db1.properties</value>
</list>
</property>

2)在ApplactionContext中是自动调用BeanFactoryPostProcessor接口的,如果要在BeanFactory中使用,必须手动添加:

XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource("jdbc.properties"));
cfg.postProcessBeanFactory(factory);

spring如何引用properties文件里的配置

时间: 2024-08-28 06:48:21

spring如何引用properties文件里的配置的相关文章

spring boot application.properties文件外部配置

spring boot application.properties文件外部配置 官方文档 原文地址:https://www.cnblogs.com/lwmp/p/9836791.html

Spring 中使用Properties文件

Spring提供了加载Properties文件的工具类:org.springframework.beans.factory.config.PropertyPlaceholderConfigurer. 在Spring容器启动时,使用内置bean对属性文件信息进行加载,在bean.xml中添加如下: <!-- spring的属性加载器,加载properties文件中的属性 --> <bean id="propertyConfigurer" class="org.

曹工说Spring Boot源码(6)-- Spring怎么从xml文件里解析bean的

写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean Definition到底是什么,咱们对着接口,逐个方法讲解 曹工说Spring Boot源码(3)-- 手动注册Bean Definition不比游戏好玩吗,我们来试一下 曹工说Spring Boot源码(4)-- 我是怎么自定义ApplicationContext,从json文件读取bean de

Spring自动注入properties文件

实现spring 自动注入属性文件中的key-value. 1.在applicationContext.xml配置文件中,引入<util />命名空间. xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/util http://www.springframework.org/schema

Spring下读取properties文件

由于在spring的xml文件中配置了 <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/&g

Spring加载properties文件的两种方式

在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可,不需要修改源代码,这样更加方便.在Spring中也可以这么做,而且Spring有两种加载properties文件的方式:基于xml方式和基于注解方式.下面分别讨论下这两种方式. 1. 通过xml方式加载properties文件 我们以Spring实例化dataSource为例,我们一般会在beans

spring无法读取properties文件数据

只讲述异常点,关于怎么配置文件,这里不做说明. 1. controller中无法读取config.properties文件 controller中注入的@Value配置是从servlet-context.xml配置文件中获取的:service中注入的@Value配置可以从applicationContext.xml中获取的.所以,如果要在controller中注入属性配置,需要在相应servlet文件中添加配置,同applicationContext.xml中一样. <bean class=&quo

文献引用 .bib文件里有公式符号

问题: 有时候文献标题带有特殊公式符号,进行BibTex编译时,会识别不出来公式符号. 例如: .bib文件里的文献: @article{XIE201892, title = "?∞ control for systems based on the  model", journal = "Top Journal", volume = "xx", pages = "xx - xx", year = "xx",

Spring通过.properties文件引入属性配置

1.在spring的beans中引入context名字空间 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <span style="color:#ff0000;"> xmlns:context="http://www.springframew