Previous Article Spring Management property configuration file properties-- Use Propertiesfactorybean to refer to the use of Org.springframework.beans.factory.config.PropertiesFactoryBean management properties files, found in the learning process through Org.springfram Ework.beans.factory.config.PropertyPlaceholderConfigurer can also manage configuration files.
1. New spring configuration file applicationcontext2.xml on the basis of the previous project (Spring_test):
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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-3.1.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.0.xsd "> <context:annotation-config/> <context:component-scan base-package=" com.* " ></context:component-scan> <!--<context:property-placeholder location= "file:config/*.conf" Ignore-unresolvable= "true"/> <context:property-placeholder location= "Classpath:*.properties" Ignore-unresolvable= "true"/>--> <!--use annotations to inject values in properties--> <bean id= "setting" class= Pringframework.beans.factory.config.PropertyPlaceholderConfigurer "> <property name=" IgnoreuNresolvableplaceholders "value=" true "/> <property name=" Locations "> <list>
<value>classpath:*.properties</value> <value>file:config/*.conf</value> </list> </property> <!--set encoding format--> <property name= "fileencoding" V Alue= "UTF-8" ></property> </bean> </beans>
2. Modify Configproperty.java:
@Component ("Configproperty") public
class Configproperty {
/** author name *
/@Value ("${author_name}")
Private String AuthorName;
/** Project information *
/@Value ("${project_info}")
private String projectInfo;
Public String Getauthorname () {return
authorname;
}
public void Setauthorname (String authorname) {
this.authorname = authorname;
}
Public String Getprojectinfo () {return
projectInfo;
}
public void Setprojectinfo (String projectInfo) {
this.projectinfo = projectInfo;
}
}
3. New Test class:
@RunWith (Springjunit4classrunner.class)
@ContextConfiguration (locations={"classpath: Applicationcontext2.xml "}) public
class Configpropertytest {
@Resource (name =" Configproperty ")
private Configproperty Configproperty;
/**
* Test Spring annotation Gets the value of the properties file/*
@Test public
Void Test () {
System.out.println ( Configproperty.getauthorname ());
System.out.println (Configproperty.getprojectinfo ());
}
Summarize:
1. How to obtain properties using the Propertiesfactorybean Management properties file:
@Value ("${properties_key}")
And Org.springframework.beans.factory.config.PropertiesFactoryBean a little different;
2. In the spring configuration file, there is a configuration for the bean configuration:
<property name= "Ignoreunresolvableplaceholders" value= "true"/>
This is mainly to solve the exception that throws cannot be resolved.