Spring代码实例系列-09:通过Spring PropertyPlaceholderConfigurer将properties配置的属性注入到xml配置文件中

超级通道:Spring代码实例系列-绪论

PropertyPlaceholderConfigurer可以实现:在单独的标准java Properties文件中配置属性,然后在spring的xml配置文件中,通过${key}获取配置属性的效果,这样做可以达到bean配置与properties配置分离的目的。
本章主要涉及的知识点有:
1. PropertyPlaceholderConfigurer
2. properties文件
3. 多properties文件加载

1.目录

project
    \---src
        \---main
            \---java
            |   \---pers
            |       \---hanchao
            |           \---propertyplaceholderconfigurer
            |               \---App.java
            |               \---SolrUtils.java
            \---resources
                \---spring-config
                    \---spring-propertyplaceholderconfigurer.xml
                \---spring-env.properties
                \---spring-jdbc.properties

2.App.java

package pers.hanchao.propertyplaceholderconfigurer;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;

/**
 * <p>对PropertyPlaceholderConfigurer的实例代码</p>
 * @author hanchao 2018/1/11 22:41
 **/
public class App {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config/spring-propertyplaceholderconfigurer.xml");
        SolrUtils solrUtils = (SolrUtils) context.getBean("solrUtils");
        System.out.println(solrUtils.toString());

        DataSource dataSource = (DataSource) context.getBean("dataSource");
        System.out.println(dataSource.toString());
    }
}

3.SolrUtils.java

package pers.hanchao.propertyplaceholderconfigurer;

public class SolrUtils {

    private String host;
    private String defaultCollection;

    public void setHost(String host) {
        this.host = host;
    }

    public void setDefaultCollection(String defaultCollection) {
        this.defaultCollection = defaultCollection;
    }

    @Override
    public String toString(){
        return "solrUtils[host:" + host + ",defaultCollection:" +defaultCollection + "]";
    }
}

4.spring-propertyplaceholderconfigurer.xml

<?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.xsd">
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <!--多properties文件配置-->
        <property name="locations">
            <list>
                <value>spring-env.properties</value>
                <value>spring-jdbc.properties</value>
            </list>
        </property>
        <property name="fileEncoding" value="UTF-8"/>
    </bean>

    <bean id="solrUtils" class="pers.hanchao.propertyplaceholderconfigurer.SolrUtils">
        <property name="host" value="${solr.host}"/>
        <property name="defaultCollection" value="${solr.default.collection}"/>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${mysql.driverClassName}"/>
        <property name="url" value="${mysql.url}"/>
        <property name="username" value="${mysql.username}"/>
        <property name="password" value="${mysql.password}"/>
    </bean>
</beans>

5.spring-env.properties

solr.host=http://127.0.0.1:8983/solr
solr.default.collection=question

6.spring-jdbc.properties

mysql.driverClassName=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/exam?useSSL=false
mysql.username=root
mysql.password=******

7.result

solrUtils[host:http://127.0.0.1:8983/solr,defaultCollection:question]
org.springframework.jdbc.datasource.DriverManagerDataSource@6737fd8f
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot,可以通过继承PropertyPlaceholderConfigurer类来获取配置文件配置项。 首先,创建一个类,继承自PropertyPlaceholderConfigurer,重写processProperties方法,该方法在Spring应用程序上下文启动时会被调用,可以在该方法获取配置文件属性值,例如: ```java public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private Properties properties; public MyPropertyPlaceholderConfigurer() { this.properties = new Properties(); } @Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { super.processProperties(beanFactoryToProcess, props); this.properties.putAll(props); } public String getProperty(String key) { return this.properties.getProperty(key); } } ``` 在该类,我们重写了processProperties方法,并在该方法配置文件属性值保存到一个Properties对象。然后,提供了一个getProperty方法,可以根据属性名获取属性值。 接下来,在Spring Boot启动类注入该类的实例,并通过该实例获取配置文件属性值,例如: ```java @SpringBootApplication public class MyApp { @Autowired private MyPropertyPlaceholderConfigurer propertyConfigurer; public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } @PostConstruct public void init() { String propertyValue = propertyConfigurer.getProperty("my.property"); System.out.println("my.property value is: " + propertyValue); } } ``` 在上述代码,我们通过@Autowired注入了MyPropertyPlaceholderConfigurer实例,并在init方法,获取了配置文件属性值,并打印出来。 在application.yml或application.properties,可以定义my.property属性值,例如: ```yaml my.property: my custom property value ``` 这样,当应用启动时,就会打印出"my.property value is: my custom property value"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值