PropertyPlaceholderConfigurer

Spring中两种常用的容器后处理器

我与影子孤独终老i 提交于 2019-12-05 03:49:44
容器后处理器是一种特殊的Bean,这种Bean并不对外提供服务,它甚至可以无需id属性,它主要负责对容器本身进行某些特殊的处理。 PropertyPlaceholderConfigurer后处理器 PropertyPlaceholderConfigurer是Spring提供的一个容器后处理器,负责读取properties属性文件里的属性值,并将这些属性值设置成Spring配置文件的元数据。通过使用 PropertyPlaceholderConfigurer,可以将Spring配置文件中的部分元数据放在属性文件中设置,这种配置方式当然有其优势:可以将部分相似的配置(比如数据库的URL,用户名和密码等)放在特定的属性文件中,如果只需要修改这部分配置,则无需修改Spring配置文件。例如: <bean class="org.springframework.beans.factory.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <!-- 如果有多个属性文件,一并列在这里 --> <value>db.properties</value> <!-- <value>xxx.properties</value> --> </list> </property> </bean> <bean id=

Spring里PropertyPlaceholderConfigurer类的使用

南笙酒味 提交于 2019-12-04 02:57:46
这个类是用来解析Java Properties属性文件值,并提供在spring配置期间替换使用属性值。以下是它的三种常见使用方法。 (1).基本的使用方法: spring-config.xml <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:/spring/include/dbQuery.properties</value> </property> </bean> 其中classpath是引用src目录下的文件写法。 (2).当存在多个Properties文件时,配置就需使用locations了: spring-config.xml <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:/spring/include/jdbc-parms.properties<

PropertyPlaceholderConfigurer 无效的问题

房东的猫 提交于 2019-11-28 14:27:59
问题描述: 这两天自己配置SPring+MyBatis遇到了个问题,搞了一天才搞定。就是PropertyPlaceholderConfigurer加载配置之后在DatasSource中的使用无效的问题。 以下是配置 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:properties/database.properties</value> </list> </property> <property name="fileEncoding" value="utf-8"></property> </bean> <!-- 数据源1 --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <!-- 数据库基本信息配置 --> <property name="url" value="${jdbc.url}"/> <property name="driverClassName" value="${jdbc.driver}"/>