public class PropertiesFactoryBeanextends PropertiesLoaderSupportimplements FactoryBean, InitializingBean
Allows for making a properties file from a classpath location available as Properties instance in a bean factory. Can be used to populate any bean property of type Properties via a bean reference.
Supports loading from a properties file and/or setting local properties on this FactoryBean. The created Properties instance will be merged from loaded and local values. If neither a location nor local properties are set, an exception will be thrown on initialization.
Can create a singleton or a new object on each request. Default is a singleton.
一个系统中通常会存在如下一些以Properties形式存在的配置文件
1.数据库配置文件demo-db.properties:
database.url=jdbc:mysql://localhost/smaple
database.driver=com.mysql.jdbc.Driver
database.user=root
database.password=123
2.消息服务配置文件demo-mq.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.远程调用的配置文件demo-remote.properties:
remote.ip=localhost
remote.port=16800
remote.serviceName=test
一、系统中需要加载多个Properties配置文件
应用场景:Properties配置文件不止一个,需要在系统启动时同时加载多个Properties文件。
配置方式:
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">
classpath:/opt/demo/config/demo-db.properties
file:/opt/demo/config/demo-mq.properties
file:/opt/demo/config/demo-remote.properties
${mq.java.naming.factory.initial}
${mq.java.naming.provider.url}
${mq.java.naming.security.principal}
${mq.java.naming.security.credentials}
${mq.java.naming.security.principal}
${mq.java.naming.security.credentials}
我们也可以将配置中的List抽取出来:
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">
classpath:/opt/demo/config/demo-db.properties
file:/opt/demo/config/demo-mq.properties
file:/opt/demo/config/demo-remote.properties
$