For a property configuration, you typically use the form of a key-value pair, such as:
Key=value
Property profiles generally use xxx.properties, and of course sometimes in order to avoid eclipse of the properties file transcoding, on the server do not recognize Chinese, you can use the form of xxx.conf management property configuration.
Spring has its own way of managing property files, and through spring management you can directly get property values directly using @value.
The property profile is managed using Org.springframework.beans.factory.config.PropertiesFactoryBean first.
1. Create a new Java project, named Spring_test;
2. Import Jar Package:
Aopalliance-1.0.jar
Commons-logging-1.1.1.jar
Org.springframework.test-3.1.0.release.jar
Spring-aop-3.1.1.release.jar
Spring-asm-3.1.1.release.jar
Spring-beans-3.1.1.release.jar
Spring-context-3.1.1.release.jar
Spring-context-support-3.1.1.release.jar
Spring-core-3.1.1.release.jar
Spring-expression-3.1.1.release.jar
3. Create a new config.propertites under SRC:
Author_name=lee
4. Create a new folder config;
5. Create a new app.conf:
Project_info= Project
6. Create a new spring configuration file Applicationcontext.xml:
<?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> <!--use annotations to inject values in properties--> <bean id= "setting" class= "Org.spri" Ngframework.beans.factory.config.PropertiesFactoryBean "> <property name=" Locations "> <list > <value>classpath:*.properties</value> <value>file:config/*.conf<
/value> </list> </property> <!--set encoding format--> <property name= "fileencoding" value= "UTF-8" ></property> </bean> < /beans>
7. Create a new class Configproperty.java to get property profile properties:
Package com.lee.property.test;
Import Org.springframework.beans.factory.annotation.Value;
Import org.springframework.stereotype.Component;
/**
* config.properties file mapping class
*
*
*
/@Component ("Configproperty") Public
class Configproperty {
/** author name *
/@Value ("#{setting[author_name]}")
private String authorname;
/** Project information *
/@Value ("#{setting[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;
}
}
8. New Test class:
@RunWith (Springjunit4classrunner.class)
@ContextConfiguration (locations={"Classpath:applicationContext.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. The way to get properties using Org.springframework.beans.factory.config.PropertiesFactoryBean is:
@Value ("${beanid[' Properties_key ']}")
1. Role
<!--set encoding format-->
<property name= "fileencoding" value= "UTF-8" ></property>
Mainly in order to solve the property file in the value of Chinese characters garbled problem.