Spring筆記——Bean後處理器

1.Bean後處理器

Bean後處理器是一種特殊的Bean,這種特殊的Bean並不對外提供服務,它主要負責對容器中的其他Bean執行後處理。
Bean後處理器必須實現BeanPostProcessor接口,BeanPostProcessor包含如下兩個方法:
☞ public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException
該方法第一個參數,是系統即將驚醒後處理的Bean實例,第二個參數是該Bean實例的名字
☞ public Object postProcessAfterInitialization(Object bean, String name) throws BeansException
該方法第一個參數,是系統即將驚醒後處理的Bean實例,第二個參數是該Bean實例的名字

實現該接口的Bean後處理器必須實現這兩個方法,這兩個方法會對容器的Bean進行後處理,會在目標Bean初始化之前、初始化之後分別被回調,這兩個方法用於對容器中Bean實例進行增強處理。

2.Bean後處理器的用處

下面是spring提供的兩個常用的後處理器:
☞ BeanNameAutoProxyCreator:根據Bean實例的name屬性,創建bean實例的代理
☞ DefaultAdvisorAutoProxyCreator:根據提供的Advisor,對容器中所有的Bean實例創建代理

3.容器後處理器

容器後處理器負責處理容器本身,容器後處理器必須實現BeanFactoryPostProcessor接口。實現該接口必須實現如下方法:
☞ postProcessorBeanFactory(ConfigurableListableBeanFactory beanFactory)

Spring 已經提供瞭如下幾個常用的容器後處理器:
☞ PropertyPlaceholderConfigurer:屬性佔位符配置器,負責讀取Properties屬性文件裏的屬性值,並將這些屬性值設置成Spring配置文件的元數據
☞ PropertyOverrideConfigurer:重寫佔位符配置器
☞ CustomAutowireConfigurer:自定義自動裝配的配置器
☞ CustomScopeConfigurer:自定義作用域的配置器

4.屬性佔位符配置器:PropertyPlaceholderConfigurer

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

    <!-- PropertyPlaceholderConfigurer是一個Bean後處理器,它會讀取屬性文件信息,並將這些信息設置成Spring配置文件的元數據 -->
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>dbconn.properties</value>
                <!-- 如果有多個屬性文件,依次在下面列出來 -->
            </list>
        </property>
    </bean>

    <!-- 定義數據源bean -->
    <bean id="dataSource" class="main.java.dao.DataSource">
        <!-- 指定連接數據庫的驅動 -->
        <property name="driverClass" value="${jdbc.driverClassName}" />
        <!-- 指定連接數據庫的url -->
        <property name="url" value="${jdbc.url}" />
        <!-- 指定連接數據庫的用戶名 -->
        <property name="username" value="${jdbc.username}" />
        <!-- 指定連接數據庫的密碼 -->
        <property name="password" value="${jdbc.password}" />
    </bean>
</beans>

dbconn.properties:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/javaee
jdbc.username=root
jdbc.password=qx.29682

5.PropertyOverrideConfigurer:重寫佔位符配置器

PropertyOverrideConfigurer的屬性文件指定的信息可以直接覆蓋Spring配置文件中的元數據,比PropertyPlaceholderConfigurer要強大。
使用PropertyOverrideConfigurer的屬性文件,每條屬性應保持如下的格式:
beanName.property=value
beanName是屬性佔位符試圖覆蓋的Bean名,property是試圖覆蓋的屬性名。

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

    <!-- PropertyOverrideConfigurer是一個Bean後處理器,它會讀取屬性文件信息,並將這些信息設置覆蓋Spring配置文件的元數據 -->
    <bean
        class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
        <property name="locations">
            <list>
                <value>dbconn.properties</value>
                <!-- 如果有多個屬性文件,依次在下面列出來 -->
            </list>
        </property>
    </bean>

    <!-- 定義數據源bean,配置該Bean時沒有指定任何信息,但properties文件裏的信息將會直接覆蓋該Bean的屬性值 -->
    <bean id="dataSource" class="main.java.dao.DataSource" />
</beans>

dbconn.properties:

dataSource.driverClass=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/javaee
dataSource.username=root
dataSource.password=qx.29682

main.java.dao.DataSource.java:

main.java.dao.DataSource:
package main.java.dao;

public class DataSource {

    private String driverClass;
    private String url;
    private String username;
    private String password;

    public void setDriverClass(String driverClass) {
        this.driverClass = driverClass;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void jdbcMessage() {
        System.out.println("driverClass:" + driverClass);
        System.out.println("url:" + url);
        System.out.println("username:" + username);
        System.out.println("password:" + password);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章