Srping配置文件屬性注入

GITHUB:https://github.com/a422478514/java-practice/tree/master/src/main/java/com/daquan/_202007/_01/spring/propertyplaceholder

Spring中可以通過兩種方式把資源文件中properties裏的key-value注入到bean中。

第一種: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" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd">


    <context:annotation-config />
    <!--自動掃描含有@Service將其注入爲bean -->
    <context:component-scan base-package="com.daquan._202007._01.spring.propertyplaceholder" />

    <!--注入屬性值,xml方式實現-->
    <context:property-placeholder location="classpath:placeholder.properties"  />
    <bean id="myPropertyServiceBean" class="com.daquan._202007._01.spring.propertyplaceholder.XmlMyPropertyServiceBean">
        <property name="id" value="${id}"/>
        <property name="name" value="${name}"/>
    </bean>
</beans>

第二種:註解方式


/**
 * 註解注入屬性值,通過註解注入bean和屬性
 */
@Service
@PropertySource(value = "classpath:placeholder.properties",ignoreResourceNotFound = true)
public class AnnotationMyPropertyServiceBean {
    @Value("${id}")
    private int id;
    @Value("${name}")
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

以下爲xml配置和註解配置的完整代碼示例:

AnnotationMyPropertyServiceBean.java

package com.daquan._202007._01.spring.propertyplaceholder;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

/**
 * 註解注入屬性值,通過註解注入bean和屬性
 */
@Service
@PropertySource(value = "classpath:placeholder.properties",ignoreResourceNotFound = true)
public class AnnotationMyPropertyServiceBean {
    @Value("${id}")
    private int id;
    @Value("${name}")
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

XmlMyPropertyServiceBean.java

package com.daquan._202007._01.spring.propertyplaceholder;


/**
 * 通過xml方式配置bean以及注入
 */
public class XmlMyPropertyServiceBean {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

TestMyPropertyServiceBean.java

package com.daquan._202007._01.spring.propertyplaceholder;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMyPropertyServiceBean {
    public static void main(String[] args) {
        System.out.println("加載spring容器");
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("application.xml");
        //xml方式
        XmlMyPropertyServiceBean myPropertyServiceBean = (XmlMyPropertyServiceBean) classPathXmlApplicationContext.getBean("myPropertyServiceBean");
        System.out.println(myPropertyServiceBean.getId());
        System.out.println(myPropertyServiceBean.getName());
        //註解方式
        AnnotationMyPropertyServiceBean annotationMyPropertyServiceBean = (AnnotationMyPropertyServiceBean) classPathXmlApplicationContext.getBean("annotationMyPropertyServiceBean");
        System.out.println(annotationMyPropertyServiceBean.getId());
    }
}

application.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" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd">


    <context:annotation-config />
    <!--自動掃描含有@Service將其注入爲bean -->
    <context:component-scan base-package="com.daquan._202007._01.spring.propertyplaceholder" />

    <!--注入屬性值,xml方式實現-->
    <context:property-placeholder location="classpath:placeholder.properties"  />
    <bean id="myPropertyServiceBean" class="com.daquan._202007._01.spring.propertyplaceholder.XmlMyPropertyServiceBean">
        <property name="id" value="${id}"/>
        <property name="name" value="${name}"/>
    </bean>
    <!--如果存在多個配置文件,可以如下配置。在Spring3.1版本之前是通過PropertyPlaceholderConfigurer實現的。而3.1之後則是通過PropertySourcesPlaceholderConfigurer實現的,注意他們不在同一個包下。-->

    <!--<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:placeholder.properties</value>
            </list>
        </property>
    </bean>-->
</beans>

如果存在多個配置文件:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
               <value>classpath:placeholder_1.properties</value>
               <value>classpath:placeholder_2.properties</value>
               <value>classpath:placeholder_3.properties</value>
               <value>classpath:placeholder_4.properties</value>
            </list>
        </property>
</bean>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章