Announcement Announcement Module
Collapse
No announcement yet.
@Configuration, BeanNameAutoProxyCreator and LazyInitTargetSourceCreator Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Configuration, BeanNameAutoProxyCreator and LazyInitTargetSourceCreator

    I'm currently trying to get an annotation based solution to lazy init a bean on first use. This is similar to the xml based version specified here http://static.springsource.org/sprin...ceCreator.html.

    However, the following unit test fails. I would expect initialized to be false

    Code:
    import org.junit.Assert;
    import org.junit.Test;
    import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
    import org.springframework.aop.framework.autoproxy.TargetSourceCreator;
    import org.springframework.aop.framework.autoproxy.target.LazyInitTargetSourceCreator;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Lazy;
    
    public class BeanNameAutoProxyCreatorTest {
    
        public static boolean intialized = false;
    
        @Configuration
        public static class AppConf {
    
            @Bean
            public LazyInitTargetSourceCreator targetSourceCreator() {
                return new LazyInitTargetSourceCreator();
            }
    
            @Bean
            public BeanNameAutoProxyCreator autoProxyCreator() {
                BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
                autoProxyCreator.setProxyTargetClass( true );
                autoProxyCreator.setBeanNames( new String[] { "object" } );
                autoProxyCreator.setCustomTargetSourceCreators( new TargetSourceCreator[] { targetSourceCreator() } );
    
                return autoProxyCreator;
            }
    
            @Bean
            @Lazy
            public Object object() {
                intialized = true;
    
                return new Object();
            }
    
        }
    
        @Test
        public void test() {
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( AppConf.class );
    
            context.getBean( "object" );
    
            Assert.assertFalse( intialized );
        }
    
    }
    Any pointers would be greatly appreciated
    Last edited by btiernay; Apr 16th, 2012, 02:18 PM.

  • #2

    However, the following unit test fails. I would expect initialized to be false
    I wouldn't...

    You are doing a getBean on the context. This initializes the bean because you requested it to be created.

    Edit: Should read closer . Ignore comment. Could you crank up the logging to debug and post (probably zipped) a log file.
    Last edited by Marten Deinum; Apr 16th, 2012, 03:15 PM.

    Comment


    • #3
      For example, the following works:

      Code:
      import javax.annotation.Resource;
      
      import org.junit.Assert;
      import org.junit.Test;
      import org.junit.runner.RunWith;
      import org.springframework.aop.framework.ProxyFactoryBean;
      import org.springframework.aop.target.LazyInitTargetSource;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.context.annotation.Lazy;
      import org.springframework.test.context.ContextConfiguration;
      import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
      import org.springframework.test.context.support.AnnotationConfigContextLoader;
      
      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(classes = ProxyFactoryBeanTest.AppConf.class, loader = AnnotationConfigContextLoader.class)
      public class ProxyFactoryBeanTest {
      
          @Resource
          Object bean;
      
          public static boolean intialized = false;
      
          @Configuration
          public static class AppConf {
      
              @Bean
              public LazyInitTargetSource targetSource() {
                  LazyInitTargetSource lazyInitTargetSource = new LazyInitTargetSource();
                  lazyInitTargetSource.setTargetBeanName( "beanTarget" );
      
                  return lazyInitTargetSource;
              }
      
              @Bean
              public Object bean() {
                  ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
                  proxyFactoryBean.setTargetSource( targetSource() );
      
                  return proxyFactoryBean.getObject();
              }
      
              @Bean
              @Lazy
              public Object beanTarget() {
                  intialized = true;
                  return new Object();
              }
          }
      
          @Test
          public void test() {
              Assert.assertFalse( intialized );
              bean.toString();
              Assert.assertTrue( intialized );
          }
      
      }
      However, this is verbose, non-transparent and requires that I perform this for each required proxied bean. Also, it doesn't seem to be able to be @Autowired. This was the reason for my original post above.

      Comment


      • #4
        Originally posted by Marten Deinum View Post
        I wouldn't...

        You are doing a getBean on the context. This initializes the bean because you requested it to be created.

        Edit: Should read closer . Ignore comment. Could you crank up the logging to debug and post (probably zipped) a log file.
        Here is the log at TRACE level.

        Comment


        • #5
          Not sure why but moving the static field to the AppConf class seems to fix the problem. At least test passes without problems.

          Comment


          • #6
            Moving the flag to the AppConf class doesn't seem to work for me. Do you mind posting your code?

            Thanks

            Comment


            • #7
              Not sure whats wrong with me the last couple of days, reading seems to be hard. By accident commented out the setting of the initialize to true, so that made the testcase work. :s.

              Comment

              Working...
              X