How to configure and read multiple Properties files in Spring
- 2020-06-23 00:25:36
- OfStack
The following configuration files in the form of Properties usually exist in a system
1. Database configuration file ES5en-ES6en. properties:
database.url=jdbc:mysql://localhost/smaple
database.driver=com.mysql.jdbc.Driver
database.user=root
database.password=123
2. Message service profile ES12en-ES13en. properties:
#congfig of ActiveMQ
mq.java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory
mq.java.naming.provider.url=failover:(tcp://localhost:61616?soTimeout=30000&connectionTimeout=30000)?jms.useAsyncSend=true&timeout=30000
mq.java.naming.security.principal=
mq.java.naming.security.credentials=
jms.MailNotifyQueue.consumer=5
3. Remote call configuration file ES19en-ES20en. properties:
remote.ip=localhost
remote.port=16800
remote.serviceName=test
1. Multiple Properties configuration files need to be loaded in the system
Application scenario: There are more than one Properties configuration file, and multiple Properties files need to be loaded simultaneously at system startup.
Configuration method:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Multiple configuration files are read into the container and handed over Spring management -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- Multiple addressing modes are supported here: classpath and file -->
<value>classpath:/opt/demo/config/demo-db.properties</value>
<!-- It is recommended to use file Is introduced so that configuration and code can be separated -->
<value>file:/opt/demo/config/demo-mq.properties</value>
<value>file:/opt/demo/config/demo-remote.properties</value>
</list>
</property>
</bean>
<!-- use MQ The configuration in -->
<bean id="MQJndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop>
<prop key="java.naming.provider.url">${mq.java.naming.provider.url}</prop>
<prop key="java.naming.security.principal">${mq.java.naming.security.principal}</prop>
<prop key="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop>
<prop key="userName">${mq.java.naming.security.principal}</prop>
<prop key="password">${mq.java.naming.security.credentials}</prop>
</props>
</property>
</bean>
</beans>
We can also extract List from the configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Place multiple configuration file locations in the list -->
<bean id="propertyResources" class="java.util.ArrayList">
<constructor-arg>
<list>
<!-- Multiple addressing modes are supported here: classpath and file -->
<value>classpath:/opt/demo/config/demo-db.properties</value>
<!-- It is recommended to use file Is introduced so that configuration and code can be separated -->
<value>file:/opt/demo/config/demo-mq.properties</value>
<value>file:/opt/demo/config/demo-remote.properties</value>
</list>
</constructor-arg>
</bean>
<!-- Read the configuration file into the container and hand it over Spring management -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" ref="propertyResources" />
</bean>
<!-- use MQ The configuration in -->
<bean id="MQJndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop>
<prop key="java.naming.provider.url">${mq.java.naming.provider.url}</prop>
<prop key="java.naming.security.principal">${mq.java.naming.security.principal}</prop>
<prop key="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop>
<prop key="userName">${mq.java.naming.security.principal}</prop>
<prop key="password">${mq.java.naming.security.credentials}</prop>
</props>
</property>
</bean>
</beans>
2. Integrate multiple dispersed Properties under multiple projects
Application scenario: There are multiple profiles in a project group, but these profiles are used in multiple places, so they need to be loaded separately.
The configuration is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- will DB The properties profile location is placed in the list -->
<bean id="dbResources" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>file:/opt/demo/config/demo-db.properties</value>
</list>
</constructor-arg>
</bean>
<!-- will MQ The properties profile location is placed in the list -->
<bean id="mqResources" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>file:/opt/demo/config/demo-mq.properties</value>
</list>
</constructor-arg>
</bean>
<!-- with Spring Loading and management DB Property profile -->
<bean id="dbPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations" ref="dbResources" />
</bean>
<!-- with Spring Loading and management MQ Property profile -->
<bean id="mqPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations" ref="mqResources" />
</bean>
<!-- use DB Configuration properties in -->
<bean id="rmsDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${demo.db.driver}" p:url="${demo.db.url}" p:username="${demo.db.username}"
p:password="${demo.db.password}" pp:maxActive="${demo.db.maxactive}"p:maxWait="${demo.db.maxwait}"
p:poolPreparedStatements="true" p:defaultAutoCommit="false">
</bean>
<!-- use MQ The configuration in -->
<bean id="MQJndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop>
<prop key="java.naming.provider.url">${mq.java.naming.provider.url}</prop>
<prop key="java.naming.security.principal">${mq.java.naming.security.principal}</prop>
<prop key="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop>
<prop key="userName">${mq.java.naming.security.principal}</prop>
<prop key="password">${mq.java.naming.security.credentials}</prop>
</props>
</property>
</bean>
</beans>
Note: Where the order attribute represents its load order, and ignoreUnresolvablePlaceholders is whether to ignore non-parsable Placeholder or not, it needs to be set to true if multiple PropertyPlaceholderConfigurer are configured. Here 1 must set these two parameters in this way.
3. Bean directly infuses the values in the Properties configuration file
Application scenario: Bean requires direct injection of values from the Properties profile. For example, the following code needs to get the value in ES67en-ES68en.properties above:
public class Client() {
private String ip;
private String port;
private String service;
}
The configuration is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="<a href="http://www.springframework.org/schema/beans" rel="external nofollow" rel="external nofollow" >http://www.springframework.org/schema/beans</a>"
xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" rel="external nofollow" >http://www.w3.org/2001/XMLSchema-instance</a>"
xmlns:util="<a href="http://www.springframework.org/schema/util" rel="external nofollow" rel="external nofollow" >http://www.springframework.org/schema/util</a>"
xsi:schemaLocation="
<a href="http://www.springframework.org/schema/beans" rel="external nofollow" rel="external nofollow" >http://www.springframework.org/schema/beans</a> <a href="http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" rel="external nofollow" >http://www.springframework.org/schema/beans/spring-beans-3.0.xsd</a>
<a href="http://www.springframework.org/schema/util" rel="external nofollow" rel="external nofollow" >http://www.springframework.org/schema/util</a> <a href="http://www.springframework.org/schema/util/spring-util-3.0.xsd" rel="external nofollow" >http://www.springframework.org/schema/util/spring-util-3.0.xsd</a>">
<!-- This type of loading can be done in code @Value Annotations are injected ,
You can assign the configuration as a whole to Properties Type of a class variable, which can also be retrieved from 1 Assigned to String Class variable of type -->
<!-- <util:properties/> Tags can only load 1 , which can be used when multiple properties files are loaded -->
<util:properties id="remoteSettings" location="file:/opt/demo/config/demo-remote.properties" />
<!-- <util:properties/> The implementation class for the tag is PropertiesFactoryBean .
Directly using the class bean Configure, set it locations Properties can be achieved 1 With the above 1 Sample the purpose of loading multiple configuration files -->
<bean id="settings"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>file:/opt/rms/config/rms-mq.properties</value>
<value>file:/opt/rms/config/rms-env.properties</value>
</list>
</property>
</bean>
</beans>
Annotation is used in Client class as follows:
import org.springframework.beans.factory.annotation.Value;
public class Client() {
@Value("#{remoteSettings['remote.ip']}")
private String ip;
@Value("#{remoteSettings['remote.port']}")
private String port;
@Value("#{remoteSettings['remote.serviceName']}")
private String service;
}
4. There are class variables of type Properties in Bean
Application scenario: When a class variable of type Properties exists in Bean, it needs to be initialized by injection
1. Configuration method: We can use the configuration method in (3), but the code annotation is modified as follows
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
public class Client() {
@Value("#{remoteSettings}")
private Properties remoteSettings;
}
2. Configuration: Bean can also be declared in xml and injected
#congfig of ActiveMQ
mq.java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory
mq.java.naming.provider.url=failover:(tcp://localhost:61616?soTimeout=30000&connectionTimeout=30000)?jms.useAsyncSend=true&timeout=30000
mq.java.naming.security.principal=
mq.java.naming.security.credentials=
jms.MailNotifyQueue.consumer=5
0
The code is as follows:
#congfig of ActiveMQ
mq.java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory
mq.java.naming.provider.url=failover:(tcp://localhost:61616?soTimeout=30000&connectionTimeout=30000)?jms.useAsyncSend=true&timeout=30000
mq.java.naming.security.principal=
mq.java.naming.security.credentials=
jms.MailNotifyQueue.consumer=5
1
5. Use wildcards to load multiple properties files
#congfig of ActiveMQ
mq.java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory
mq.java.naming.provider.url=failover:(tcp://localhost:61616?soTimeout=30000&connectionTimeout=30000)?jms.useAsyncSend=true&timeout=30000
mq.java.naming.security.principal=
mq.java.naming.security.credentials=
jms.MailNotifyQueue.consumer=5
2
or
<context:property-placeholder location="classpath*:/*.properties"/>