<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    ALL is Well!

    敏捷是一條很長的路,摸索著前進(jìn)著

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      30 隨筆 :: 23 文章 :: 71 評(píng)論 :: 0 Trackbacks
    通過上一篇 利用自定義Java注解實(shí)現(xiàn)資源注入 介紹的方法,我們實(shí)現(xiàn)了通過自定義注解完成了對(duì)DataSource資源的注入,但在實(shí)際應(yīng)用中,我們通常不希望去顯式的去聲明這樣的MyAnnotationBeanProcessor對(duì)象來幫助我們完成注入,而是希望通過Spring幫我們“悄悄地”完成。
    利用自定義Java注解實(shí)現(xiàn)資源注入 里的代碼(部分代碼)不變,我們希望在測(cè)試類中以如下方法調(diào)用便可以實(shí)現(xiàn)資源的注入:
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    import com.annotation.MyService;

    public class SpringWiringTest {
        
    public static void main(String args[]) {
            ClassPathXmlApplicationContext ctx 
    = new ClassPathXmlApplicationContext("com/spring/applicationContext.xml");
            MyService b 
    = (MyService)ctx.getBean("myService"); // 通過Spring去管理bean,此時(shí)已完成了對(duì)標(biāo)有DataSource注解的資源的注入
            System.out.println(b.selectForObjectFromB(""null));
            System.out.println(b.selectForObjectFromA(
    ""null));
        }

    }

    注:MyService類實(shí)現(xiàn)在 利用自定義Java注解實(shí)現(xiàn)資源注入 中。

    為了實(shí)現(xiàn)上面的目標(biāo),我們就不能使用MyAnnotationBeanProcessor.java類來實(shí)現(xiàn)對(duì)資源的注入了,我們必須實(shí)現(xiàn)一個(gè)能融入Spring的BeanProcessor類才行。
    DataSourceBeanProcessor.java類實(shí)現(xiàn)BeanPostProcessor、PriorityOrdered接口:
    import java.lang.reflect.Field;

    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.core.Ordered;
    import org.springframework.core.PriorityOrdered;

    public class DataSourceBeanProcessor implements BeanPostProcessor, PriorityOrdered {
        @Override
        
    // 在這里完成資源注入
        public Object postProcessAfterInitialization(Object bean, String beanName)
            
    throws BeansException {
            Class
    <?> cls = bean.getClass();
            
    for (Field field : cls.getDeclaredFields()) {
                
    if (field.isAnnotationPresent(DataSource.class)) {
                    DataSourceStaticWiring.wiring(bean, field);
                }

            }

            
    return bean;
        }


        @Override
        
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            
    throws BeansException {
            
    return bean;
        }


        @Override
        
    public int getOrder() {
            
    return Ordered.LOWEST_PRECEDENCE;
        }

    }

    下面來看DataSourceStaticWiring的實(shí)現(xiàn),與前一篇 里的DataSourceWiring.java類相比,改動(dòng)點(diǎn)有以下三個(gè):
    1.不需要實(shí)現(xiàn)IFieldWiring接口
    2.刪除annotationClass方法
    3.將wiring方法修改為static方法
    具體代碼如下:
    import java.lang.reflect.Field;

    public class DataSourceStaticWiring {

        
    public static void wiring(Object object, Field field) {
            Object fieldObj 
    = ReflectUtils.getFieldValue(object, field.getName());
            
    if (fieldObj != null{
                
    return;
            }

            DataSource annotation 
    = field.getAnnotation(DataSource.class);
            String type 
    = annotation.type();
            String sqlMap 
    = annotation.sqlMap();
            
    // 這里可以用緩存來實(shí)現(xiàn),不用每次都去創(chuàng)建新的SqlMapClient對(duì)象
            SqlMapClient sqlMapImpl = new SqlMapClient(sqlMap, type);
            ReflectUtils.setFieldValue(object, field.getName(), SqlMapClient.
    class, sqlMapImpl);
        }

    }

    注:SqlMapClient、ReflectUtils實(shí)現(xiàn)在上一篇 利用自定義Java注解實(shí)現(xiàn)資源注入 中。

    代碼已準(zhǔn)備就緒,接下來是配置Spring:applicationContext.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:aop
    ="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context
    ="http://www.springframework.org/schema/context"
        xsi:schemaLocation
    ="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                            http://www.springframework.org/schema/aop 
                            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                            http://www.springframework.org/schema/tx 
                            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                            http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd"

        default-lazy-init
    ="true">
        
        
    <!-- 自定義的BeanProcessor -->
        
    <bean class="com.annotation.DataSourceBeanProcessor" />
        
    <context:component-scan base-package="com.annotation" />

        
    <!-- 測(cè)試用bean -->
        
    <bean id="myService" class="com.annotation.MyService" destroy-method="close">
        
    </bean>
    </beans>

    測(cè)試代碼其實(shí)已經(jīng)在前面列出來了。SpringWiringTest.java
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    import com.annotation.MyService;

    public class SpringWiringTest {
        
    public static void main(String args[]) {
            ClassPathXmlApplicationContext ctx 
    = new ClassPathXmlApplicationContext("com/spring/applicationContext.xml");
            MyService b 
    = (MyService)ctx.getBean("myService");
            System.out.println(b.selectForObjectFromB(
    ""null));
            System.out.println(b.selectForObjectFromA(
    ""null));
        }

    }

    執(zhí)行結(jié)果:
    SqlMapClient[sqlMap=com/annotation/sql-map-config-B.xml,type=B]
    SqlMapClient[sqlMap
    =com/annotation/sql-map-config-A.xml,type=A]

    由結(jié)果可見,我們利用Spring完成了對(duì)DataSource資源的注入了。

    在這里如果還想擴(kuò)展的話,就需要新建類假設(shè)為InParamBeanProcessor,實(shí)現(xiàn)BeanPostProcessor、PriorityOrdered接口,然后實(shí)現(xiàn)其中的方法,對(duì)資源進(jìn)行注入,這里就是擴(kuò)展Spring了,與本篇介紹的方法相同。

    注:以上代碼重在演示,其實(shí)這個(gè)需求可以在Spring中管理兩個(gè)不同的SqlMapClient對(duì)象,然后通過Spring的自動(dòng)注入實(shí)現(xiàn)。

    本文為原創(chuàng),歡迎轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)注明出處BlogJava
    posted on 2010-10-04 10:31 李 明 閱讀(8237) 評(píng)論(1)  編輯  收藏 所屬分類: JavaSpring

    評(píng)論

    # re: 通過Spring實(shí)現(xiàn)對(duì)自定義注解屬性進(jìn)行資源注入 2016-05-10 18:28 如煩人煩人
    ffhty  回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 亚洲成AV人片在线观看无码| 两性色午夜视频免费播放| 久久亚洲国产成人亚| 国产精品久久久久影院免费| 无人在线直播免费观看| 蜜桃视频在线观看免费视频网站WWW| 99亚洲男女激情在线观看| 亚洲中文字幕久在线| 亚洲国产一区二区a毛片| 亚洲中文字幕丝袜制服一区| 日韩在线天堂免费观看 | 亚洲国产综合久久天堂| 成年轻人网站色免费看| 久草视频免费在线| 日韩中文字幕免费视频| 最近免费mv在线观看动漫| xvideos永久免费入口| 色www免费视频| 国产成人亚洲毛片| 久久亚洲中文无码咪咪爱| 最新国产精品亚洲| 在线精品亚洲一区二区| 亚洲天堂男人影院| 亚洲AV日韩综合一区尤物| 亚洲免费视频网址| 亚洲国产91在线| 亚洲中文字幕AV每天更新| 中文字幕在线日亚洲9| 日韩亚洲国产高清免费视频| 国内精品久久久久影院亚洲| 亚洲av无码一区二区三区观看| 亚洲人6666成人观看| 亚洲乱码在线卡一卡二卡新区| 亚洲夂夂婷婷色拍WW47 | 四虎影在线永久免费四虎地址8848aa| 午夜视频免费成人| 国产一级特黄高清免费大片| 四虎永久成人免费影院域名| 日产国产精品亚洲系列| 中文字幕亚洲综合久久菠萝蜜 | 亚洲狠狠婷婷综合久久|