簡體   English   中英

在春季為CGLib生成的代理進行資源注入

[英]Ressource injection for CGLib generated proxies in Spring

我有一個小問題,希望有人可以復習一下並為我提供幫助。 我正在使用CGLib Mixin代理創建兩個接口/實現的混合。 這個mixin用於我的spring應用程序。 一切正常:

    static InterfaceBoth createInstance() {
      final Class<?>[] interfaces = new Class[] { InterfaceOne.class, InterfaceTwo.class, InterfaceBoth.class };
      final Object[] delegates = new Object[] { new ClassOne(), new ClassTwo() };
      return ((InterfaceBoth) Mixin.create(interfaces, delegates));
    }

    <!-- Create mixin instance -->
    <bean id="mixin" class="org.literadix.tests.mixins.InterfaceBoth" factory-method="createInstance">
       <property name="password" ref="password"/>
    </bean>

   /**
    * Load mixin by spring bean.
    */
   @Test
   public final void testSpring() {

          final ApplicationContext sCtx = new ClassPathXmlApplicationContext("Application.xml");
          final InterfaceBoth instance = sCtx.getBean(InterfaceBoth.class);

          Assert.assertNotNull(instance.helloOne());
          Assert.assertNotNull(instance.helloTwo());
          Assert.assertNotNull(instance.getPassword());

          log.debug("service instance: {}", instance.getService());
          Assert.assertNotNull(instance.getService());

   }

我還通過屬性將值(密碼)注入到該實例中。 不起作用的是通過注解注入資源。 我完整的spring定義是這樣的:

   <?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"

          default-autowire="byName">

       <!-- Enable annotations. -->
       <bean id="annotations" class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

       <bean id="password" class="java.lang.String">
           <constructor-arg value="password"/>
       </bean>

       <!-- Service bean -->
       <bean id="service" class="org.literadix.tests.mixins.NewServiceImpl">
       </bean>

       <!-- Create mixin instance -->
       <bean id="mixin" class="org.literadix.tests.mixins.InterfaceBoth" factory-method="createInstance">
           <property name="password" ref="password"/>
       </bean>

   </beans>

   public class ClassOne implements InterfaceOne {

     /**
      * !!! THIS SHOULD BE SET BY SPRING !!!
      */
     @Resource
     public NewService service;    
     ...

春季專家可以復述我的示例,並告訴我為什么它不能用於資源的注釋解析嗎? 我想這是Spring bean加載生命周期的問題。 但是我找不到解決方案。

我將所有內容打包到一個github存儲庫中。 如果要在計算機上進行測試,請直接下載:

https://github.com/literadix/JavaMixins https://github.com/literadix/JavaMixins.git

非常感謝你,

馬切伊

我現在有了解決該問題的想法。 傑里米·B(Jeremie B)寫道,對象創建是在春天之外的,這完全是事實。 因此,一種解決方案是編寫一個工廠bean,該工廠bean由spring框架創建,並將在最初設置接口。 然后可以使用該工廠創建mixin,它將設置所需的依賴關系。 但是,與僅查看以下源代碼相比,很難理解成千上萬個單詞:

    <bean id="mixinFactory" class="org.literadix.tests.mixins.InterfaceBoth.Factory"/>

    <!-- Create mixin instance -->
    <bean id="mixin" scope="prototype" class="org.literadix.tests.mixins.InterfaceBoth" factory-bean="mixinFactory" factory-method="create">
        <property name="password" ref="password"/>
    </bean>

    public interface InterfaceBoth extends InterfaceOne, InterfaceTwo {

        /** Logger. */
        final static Logger log = LoggerFactory.getLogger(InterfaceBoth.class);

        /**
         * Factory to create a mixin instance from both interfaces and both implementations.
         *
         * @return Merged instance
         */
        static InterfaceBoth createInstance() {
            return new Factory().create();
        }

        static class Factory {

            @Autowired(required = false)
            NewService service;

            InterfaceBoth create(){

                log.debug("created instance {}", service);

                final Class<?>[] interfaces = new Class[] { InterfaceOne.class, InterfaceTwo.class, InterfaceBoth.class };
                final Object[] delegates = new Object[] { new ClassOne() {{setService(service);}}, new ClassTwo() };
                return ((InterfaceBoth) Mixin.create(interfaces, delegates));
            }
        }

    }   

只需查看我的存儲庫中的最新版本即可:

https://github.com/literadix/JavaMixins

最后一句話:該解決方案不僅適用於spring,而且由於micin工廠是CGLib工廠,因此只能在純Java應用程序中使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM