Spring Management Properties Profile properties--using Propertyplaceholderconfigurer

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.