spring 使用外部屬性文件
阿新 • • 發佈:2019-01-16
component ati lac pla ava gif sch processor 功能
PropertyPlaceholderConfigurer
spring提供的PropertyPlaceholderConfigurer實現類能夠使Bean在配置時引用外部屬性文件。
PropertyPlaceholderConfigurer實現了BeanFactoryPostProcessorBean接口,因而也是一個Bean工廠後處理器。
PropertyPlaceholderConfigurer的使用
xml配置及註解方式:
<dependency> <groupId>org.springframework</groupIdpom.xml> <artifactId>spring-core</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.16.RELEASE</version> </dependency>
name=zhangsan age=23my.properties
package test; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Driver { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("my.xml"); User user = context.getBean("user", User.class); System.out.println(user.getName()); System.out.println(user.getAge()); } } class User { @Value("${name}") private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }java 類
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- spring 引入屬性文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="classpath:my.properties" p:fileEncoding="utf-8"/> <!-- 開啟包掃描,因為User類使用到了@Value註解;包掃描不支持裸體類上的註解 --> <context:component-scan base-package="test"/> <!-- 使用外部屬性文件值賦值 --> <bean id="user" class="test.User" p:age="${age}"/> </beans>spring my.xml
PropertyPlaceholderConfigurer的其他屬性:
locations:可以引入多個屬性文件
fileEncoding:屬性文件的編碼格式
order:指定多個屬性的優先級
placeholderPrefix:默認值為“${”,可以修改
placeholderSuffix:默認為“}”
除了使用<bean>聲明PropertyPlaceholderConfigurer引入屬性文件這種方式外,還可以使用另一種簡潔的方式;但如果希望自定義一些額外的高級功能,如屬性加密、使用數據庫表來保存配置信息時,就必須使用<bean>聲明PropertyPlaceholderConfigurer的方式
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- spring 引入屬性文件 --> <context:property-placeholder location="my.properties"/> <!-- 開啟包掃描,因為User類使用到了@Value註解;包掃描不支持裸體類上的註解 --> <context:component-scan base-package="test"/> <!-- 使用外部屬性文件值賦值 --> <bean id="user" class="test.User" p:age="${age}"/> </beans>spring my.xml
Java配置及註解方式:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.16.RELEASE</version> </dependency>pom.xml
name=zhangsan
age=23
my.properties
package test; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Driver { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class); User user = context.getBean("user", User.class); System.out.println(user.getName()); System.out.println(user.getAge()); } } class User { @Value("${name}") private String name; @Value("${age}") private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }java類
package test; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; @Configuration @ComponentScan public class MyConfig { @Bean public User user() { return new User(); } @Bean public PropertyPlaceholderConfigurer PropertyPlaceholderConfigurer() { PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer(); configurer.setLocation(new ClassPathResource("my.properties")); return configurer; } }java 配置類
spring 使用外部屬性文件