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

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

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

    ALL is Well!

    敏捷是一條很長的路,摸索著前進著

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      30 隨筆 :: 23 文章 :: 71 評論 :: 0 Trackbacks
    通過上一篇 利用自定義Java注解實現(xiàn)資源注入 介紹的方法,我們實現(xiàn)了通過自定義注解完成了對DataSource資源的注入,但在實際應(yīng)用中,我們通常不希望去顯式的去聲明這樣的MyAnnotationBeanProcessor對象來幫助我們完成注入,而是希望通過Spring幫我們“悄悄地”完成。
    利用自定義Java注解實現(xiàn)資源注入 里的代碼(部分代碼)不變,我們希望在測試類中以如下方法調(diào)用便可以實現(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,此時已完成了對標有DataSource注解的資源的注入
            System.out.println(b.selectForObjectFromB(""null));
            System.out.println(b.selectForObjectFromA(
    ""null));
        }

    }

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

    為了實現(xiàn)上面的目標,我們就不能使用MyAnnotationBeanProcessor.java類來實現(xiàn)對資源的注入了,我們必須實現(xiàn)一個能融入Spring的BeanProcessor類才行。
    DataSourceBeanProcessor.java類實現(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的實現(xiàn),與前一篇 里的DataSourceWiring.java類相比,改動點有以下三個:
    1.不需要實現(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();
            
    // 這里可以用緩存來實現(xiàn),不用每次都去創(chuàng)建新的SqlMapClient對象
            SqlMapClient sqlMapImpl = new SqlMapClient(sqlMap, type);
            ReflectUtils.setFieldValue(object, field.getName(), SqlMapClient.
    class, sqlMapImpl);
        }

    }

    注:SqlMapClient、ReflectUtils實現(xiàn)在上一篇 利用自定義Java注解實現(xià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" />

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

    測試代碼其實已經(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完成了對DataSource資源的注入了。

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

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

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

    評論

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

    主站蜘蛛池模板: 亚洲欧洲无卡二区视頻| 最近最好最新2019中文字幕免费| 亚洲精品中文字幕无码AV| 啊v在线免费观看| 无码中文在线二区免费| 国产成人免费视频| 国产免费怕怕免费视频观看| 999任你躁在线精品免费不卡| 九一在线完整视频免费观看| 亚洲人成网站999久久久综合| 中文字幕在线观看亚洲| 大香人蕉免费视频75| 午夜视频在线免费观看| 免费播放在线日本感人片| 一级成人生活片免费看| 国产精品亚洲一区二区三区在线观看 | 国产大片51精品免费观看| 成人无遮挡裸免费视频在线观看| 无码av免费一区二区三区试看| 香蕉免费在线视频| 亚洲综合自拍成人| 国产成人麻豆亚洲综合无码精品| 国产伦精品一区二区三区免费下载 | 国产免费久久精品| 日韩成全视频观看免费观看高清| 黄色片在线免费观看| free哆啪啪免费永久| 69xx免费观看视频| 1000部拍拍拍18勿入免费视频下载 | 免费在线观影网站| 免费a级毛片无码a∨免费软件| 羞羞视频免费网站在线看| 国产精品午夜免费观看网站| 亚洲成人免费电影| 亚洲午夜精品一区二区公牛电影院 | 亚洲一区影音先锋色资源| 亚洲午夜精品久久久久久人妖| 亚洲av无码成人黄网站在线观看| 亚洲AV综合色区无码另类小说| 亚洲av无码国产精品色午夜字幕 | 香蕉国产在线观看免费|