繁体   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