Spring中PropertyPlaceholderConfigurer的使用
Spring 利用PropertyPlaceholderConfigurer佔位符
1. PropertyPlaceholderConfigurer是個bean工廠後置處理器的實現,也就是BeanFactoryPostProcessor介面的一個實現。PropertyPlaceholderConfigurer可以將上下文(配置檔案)中的屬性值放在另一個單獨的標準java Properties檔案中去。在XML檔案中用${key}替換指定的properties檔案中的值。這樣的話,只需要對properties檔案進行修改,而不用對xml配置檔案進行修改。
2.在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置檔案中加入外部屬性檔案,當然也可以指定外部檔案的編碼,如:
<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>conf/sqlmap/jdbc.properties</value>
</property>
<property name="fileEncoding">
<value>UTF-8</value>
</property>
</bean>
當然也可以引入多個屬性檔案,如:
<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/mail.properties</value>
<value>classpath: conf/sqlmap/jdbc.properties</value>//注意這兩種value值的寫法
</list>
</property>
</bean>
3.譬如,jdbc.properties的內容為:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mysqldb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=round;
jdbc.username=root
jdbc.password=123456
4.那麼在spring配置檔案中,我們就可以這樣寫:
<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath: conf/sqlmap/jdbc.properties </value>
</list>
</property>
</bean>
<bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
<property name="driverClassName"value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}"/>
<property name="password"value="${jdbc.password}" />
</bean>
5.這樣,一個簡單的資料來源就設定完畢了。可以看出:PropertyPlaceholderConfigurer起的作用就是將佔位符指向的資料庫配置資訊放在bean中定義的工具。
6.檢視原始碼,可以發現,locations屬性定義在PropertyPlaceholderConfigurer的祖父類PropertiesLoaderSupport中,而location只有 setter方法。類似於這樣的配置,在spring的源程式中很常見的。
PropertyPlaceholderConfigurer如果在指定的Properties檔案中找不到你想使用的屬性,它還會在Java的System類屬性中查詢。
我們可以通過System.setProperty(key, value)或者java中通過-Dnamevalue來給Spring配置檔案傳遞引數。
相關文章
- Spring中Enable*功能的使用Spring
- AspectJ 在 Spring 中的使用Spring
- Spring Boot(十一):Spring Boot 中 MongoDB 的使用Spring BootMongoDB
- Spring Boot(三):Spring Boot 中 Redis 的使用Spring BootRedis
- Spring中@Import註解的使用SpringImport
- Mybatis在Spring中的使用(三)MyBatisSpring
- JMS 在 Spring Boot 中的使用Spring Boot
- spring中定時器的使用Spring定時器
- Spring中Value註解的使用Spring
- Spring MVC 中 HandlerInterceptorAdapter的使用SpringMVCAPT
- 如果使用spring中的jdni配置Spring
- spring boot中zookeeper使用Spring Boot
- spring boot中redis使用Spring BootRedis
- Spring Boot中Dockerfile使用Spring BootDocker
- Spring 5 MVC 中的 Router Function 使用SpringMVCFunction
- Spring Boot中攔截器的使用Spring Boot
- Spring中Quartz排程器的使用Springquartz
- spring中異常的使用--事務Spring
- springboot(十一):Spring boot中mongodb的使用Spring BootMongoDB
- springboot(三):Spring boot中Redis的使用Spring BootRedis
- 使用Intellij中的Spring Initializr來快速構建Spring Boot工程IntelliJSpring Boot
- Spring中如何使用設計模式Spring設計模式
- Spring之RestTemplate中級使用篇SpringREST
- Spring中@Transactional事務使用陷阱Spring
- Spring中@Transactional與@Async共同使用Spring
- 不用注入方式使用Spring管理的物件中的方法,神奇Spring物件
- 8、使用 Spring Boot 搭建的一個 Spring MVC 示例(持續更新中)Spring BootMVC
- Spring中如何優雅的使用監聽器模式Spring模式
- Spring中多執行緒的使用及問題Spring執行緒
- Spring Boot中@Import三種使用方式!Spring BootImport
- Spring中的ResourceHandlerSpring
- Spring中的BeanSpringBean
- Spring 中的AOPSpring
- 使用Intellij中的Spring Initializr來快速構建SpriIntelliJSpring
- Spring的Filter使用SpringFilter
- Spring Boot(三):Spring Boot中的事件的使用 與Spring Boot啟動流程(Event 事件 和 Listeners監聽器)Spring Boot事件
- redis的安裝並在java中初步使用(spring配置redis)RedisJavaSpring
- spring中autowire的用法Spring