One, through the Context:property-placeholder tag implementation configuration file load 1) Usage:
1. Add tags to the spring.xml configuration file
<ignore-unresolvable= "true" location= "Classpath: Redis-key.properties "/>
2. Using the profile property in Spring.xml: $
<!--Basic Property URL, user, password -< Propertyname= "url"value= "${jdbc.url}" />< Propertyname= "username"value= "${jdbc.username}" />< Propertyname= "Password"value= "${jdbc.password}" /
3. Use in Java files:
@Value ("${jdbc.url}") private // Note: Here the variable cannot be defined as static
2) Note: Step over the Pit
When importing a configuration file using the <context:property-placeholderlocation> tag in XML in spring, you want to import multiple properties profiles, as follows:
<context:property-placeholderlocation= "Classpath:db.properties"/>
<context:property-placeholderlocation= "Classpath:zxg.properties"/>
It turns out that no, the second profile is never read, and theSpring container is a discovery mechanism that uses a reflection scan to instantiate an instance through the namespace of a tag, when Spring detects that there is a Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer beans will stop on the remaining Propertyplaceholderconfigurer Scan, i.e. only one instance can exist
If you have more than one configuration file, you can use the "," Partition
< context:property-placeholderlocation = "Classpath:db.properties,classpath:monitor.properties" />
You can use a wildcard character *
< context:property-placeholderlocation = "Classpath:*.properties" />
3) Attribute Usage
Ignore-resource-not-found//If the property file is not found, ignore, default false, not ignore, file not found and no exception is thrown. Ignore-unresolvable//Whether the unresolved properties are ignored, and if not ignored, an exception will be thrown. But the main reason it is set to True is:
Second, through the util:properties tag implementation configuration file load 1) usage
1. Usage example: Adding tags to the spring.xml configuration file
<?XML version= "1.0" encoding= "UTF-8"?>Xmlns:util= "Http://www.springframework.org/schema/util"xsi:schemalocation= "Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd" >
<!--Load configuration file -<util:propertiesID= "jdbc" local-override="true" Location= "Classpath:properties/jdbc.properties"/>
2. Using profile properties in Spring.xml: #
<!--DataSource -<BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource"> < Propertyname= "Driverclass"value= "#{jdbc.driverclass}" /> < Propertyname= "Jdbcurl"value= "#{jdbc.jdbcurl}" /> < Propertyname= "User"value= "#{jdbc.user}" /> < Propertyname= "Password"value= "#{jdbc.password}" /></Bean>
3.java file, let spring inject the value of the property read from the resource file, and for the sake of simplicity, write several injected methods directly into a file to show:
@Component Public class sysconf { @Value ("#{jdbc.url}") private String url;
@Value ("#{jdbc}") publicvoid setjdbcconf (Properties jdbc) { URL= sys.getproperty ("url"); } }
Note: The #{jdbc} here corresponds to the 1th step of the id= "JDBC"
Third, through the @PropertySource annotations to implement the configuration file loading
The use is similar to Context:property-placeholder
1. Usage Example: Using Propertysource annotations in Java class files
@PropertySource (value={"Classpath:mail.properties"})publicclass readproperties {@ Value (Value= "${mail.username}"private String user_name;}
Three ways spring loads the properties configuration file