Announcement Announcement Module
Collapse
No announcement yet.
AspectJ, prototype beans, and JavaConfig Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AspectJ, prototype beans, and JavaConfig

    Hi, I'm experimenting injecting objects created outside of the application context utilizing AspectJ and Spring's AnnotationBeanConfigurerAspect. Unfortunately, in my simple example I cannot get JavaConfig to work. The equivalent xml configuration works however.

    JavaConfig configuration:
    Code:
    @Configuration
    public class TestConfiguration extends ConfigurationSupport {
    
    	@Bean 
    	public AnnotationBeanConfigurerAspect annotationBeanConfigurerAspect() {
    		return AnnotationBeanConfigurerAspect.aspectOf();
    	}
    		
    	@Bean(scope = DefaultScopes.PROTOTYPE)
    	public TestPrototype testPrototype() {
    		TestPrototype tp = new TestPrototype();
                    tp.setOneService(oneService());
    		tp.setTwoService(twoService());
    		return tp;
    	}
    	
    	@Bean
    	public OneService oneService() {
    		return new OneService();
    	}
    	
    	@Bean
    	public TwoService twoService() {
    		return new TwoService();
    	}
    }
    Equivalent xml configuration:
    Code:
    <beans>
        <bean
            class="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect"
            factory-method="aspectOf"/>
        
        <bean id="oneService" class="com.example.OneService" />
        
        <bean id="twoService" class="com.example.TwoService"/>
        
        <bean id="testPrototype" class="com.example.TestPrototype" scope="prototype">
            <property name="oneService" ref="oneService"/>
            <property name="twoService" ref="twoService"/>
        </bean>
        
    </beans>
    The following will result in an assertion error utilizing JavaConfig but not the traditional xml configuration:

    Code:
    TestPrototype tp = new TestPrototype();
    assert(tp.getOneService() != null);
    assert(tp.getTwoService() != null);
    Any help would be greatly appreciated!

  • #2
    did you get a reply?

    Comment


    • #3
      Bump - can this be done with JavaConfig?

      Comment


      • #4
        I'm running into this issue as well. Has a solution been found? Can one instantiate/initialize an Aspect using aspectOf() method in JavaConfig in Spring 3.x?

        Thanks,

        Eric

        Comment

        Working...
        X