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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值