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配置檔案傳遞引數。
相關文章
- AspectJ 在 Spring 中的使用Spring
- Spring中Enable*功能的使用Spring
- Spring中@Import註解的使用SpringImport
- Mybatis在Spring中的使用(三)MyBatisSpring
- springboot(十一):Spring boot中mongodb的使用Spring BootMongoDB
- Spring Boot中攔截器的使用Spring Boot
- Spring 5 MVC 中的 Router Function 使用SpringMVCFunction
- spring boot中zookeeper使用Spring Boot
- spring boot中redis使用Spring BootRedis
- 使用Intellij中的Spring Initializr來快速構建Spring Boot工程IntelliJSpring Boot
- Spring之RestTemplate中級使用篇SpringREST
- Spring中@Transactional事務使用陷阱Spring
- Spring中@Transactional與@Async共同使用Spring
- Spring中如何使用設計模式Spring設計模式
- 不用注入方式使用Spring管理的物件中的方法,神奇Spring物件
- Spring中如何優雅的使用監聽器模式Spring模式
- 使用Intellij中的Spring Initializr來快速構建SpriIntelliJSpring
- Spring中多執行緒的使用及問題Spring執行緒
- Spring Validation 的使用Spring
- Spring Boot中@Import三種使用方式!Spring BootImport
- Spring Boot(三):Spring Boot中的事件的使用 與Spring Boot啟動流程(Event 事件 和 Listeners監聽器)Spring Boot事件
- Spring中的ResourceHandlerSpring
- Intellij IDEA 中 的spring boot 專案使用了spring-boot-devtools要做的兩個設定IntelliJIdeaSpring Bootdev
- Spring Data JPA 的使用Spring
- Dozer的使用: 整合SpringSpring
- Spring Framework BeanUtils的使用SpringFrameworkBean
- 二、Spring Security的使用Spring
- Spring Boot2中Swagger3使用Spring BootSwagger
- Spring事務專題(四)Spring中事務的使用、抽象機制及模擬Spring事務實現Spring抽象
- Spring原始碼分析(三) -- Spring中的BeanFactoryPostProcessorSpring原始碼Bean
- Spring Security 中的 BCryptPasswordEncoderSpring
- Spring Boot中如何使用Ostara監控應用?Spring Boot
- Spring Data JPA中事務使用異常TransactionUsageExceptionSpringException
- Spring Cloud Netflix—示例在Ribbon中禁用Eureka使用SpringCloud
- 在spring boot3中使用native imageSpring Boot
- ActiveMQ的使用及整合spring的使用例項MQSpring
- 如何使用Spring的FactoryBean介面SpringBean
- 如何使用Spring Boot的ProfilesSpring Boot
- Mybatis 的使用(整合Spring、SpringBoot)MyBatisSpring Boot