Results 1 to 5 of 5

Thread: How to inject a spring bean into my @Aspect?

  1. #1
    Join Date
    Jan 2009
    Posts
    4

    Default How to inject a spring bean into my @Aspect?

    This is a newbie question: I can successfully get the advice in my aspect class to fire based on my pointcuts; but the work my advice needs to do depends on access to a spring bean. Is there a pattern to follow to inject a bean into my @Aspect class? I've tried @Resource, @Autowired, ApplicationContextAware and nothing works.

    Any help is appreciated.

  2. #2
    Join Date
    May 2009
    Posts
    24

    Default

    Quote Originally Posted by jsinai View Post
    This is a newbie question: I can successfully get the advice in my aspect class to fire based on my pointcuts; but the work my advice needs to do depends on access to a spring bean. Is there a pattern to follow to inject a bean into my @Aspect class? I've tried @Resource, @Autowired, ApplicationContextAware and nothing works.

    Any help is appreciated.

    you need to create properties in the aspect, and then use spring property injections to java set methods.

  3. #3
    Join Date
    Jan 2009
    Posts
    4

    Default Thanks

    Thanks for the tip. The setter injection worked (as you imply, it has to be in the application context xml file). However, by the time my advice fires, the bean is null. To avoid that problem, I needed to make the bean property static.

  4. #4
    Join Date
    May 2009
    Posts
    24

    Default

    Quote Originally Posted by jsinai View Post
    Thanks for the tip. The setter injection worked (as you imply, it has to be in the application context xml file). However, by the time my advice fires, the bean is null. To avoid that problem, I needed to make the bean property static.
    if you create a class variable in the aspect for for the bean that you are setting, it shouldnt go out of scope.

  5. #5
    Join Date
    Dec 2007
    Posts
    27

    Default

    Quote Originally Posted by jsinai View Post
    This is a newbie question: I can successfully get the advice in my aspect class to fire based on my pointcuts; but the work my advice needs to do depends on access to a spring bean. Is there a pattern to follow to inject a bean into my @Aspect class? I've tried @Resource, @Autowired, ApplicationContextAware and nothing works.

    Any help is appreciated.
    If you don't mind mixing .aj aspect and @Aspect in your project :

    Code:
    package __SET_YOURS__;
    
    import javax.annotation.Resource;
    import java.lang.reflect.Field ;
    
    import org.aspectj.lang.reflect.FieldSignature ;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.context.ApplicationContext;
    
    public abstract aspect AbstractDependencyInjection implements ApplicationContextAware {
    
    	private ApplicationContext	_ctx ;
    
    	public abstract pointcut resourceAccess(); 
    
    	Object around():resourceAccess(){
    		FieldSignature fs = (FieldSignature)thisJoinPoint.getSignature();
    		Field field = fs.getField();
    		
    		String beanid = field.getAnnotation(Resource.class).name();
    
    		if (beanid.equals("")) {
    			beanid = field.getName();
    		}
    
    		return this._ctx.getBean(beanid) ;
    	}
    
    	/*
    	 * (non-Javadoc)
    	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
    	 */
    	public void setApplicationContext(ApplicationContext inContext) {
            this._ctx = inContext ;
          }
    }
    Code:
    package __SET_YOURS__;
    
    import javax.annotation.Resource;
    import CLASS_THAT_WILL_HAVE_RESOURCE_INJECTED;
    
    public aspect REAL_DependencyInjection extends AbstractDependencyInjection {
    	public pointcut resourceAccess(): get(@Resource * *) && within(CLASS_THAT_WILL_HAVE_RESOURCE_INJECTED+) ;
    }
    (you can adapt pointcut definition at your criteria…)

    Code:
    @Aspect
    public class CLASS_THAT_WILL_HAVE_RESOURCE_INJECTED {
            …
    	@Resource(name="BEAN_ID_TO_INJECT")
    	protected YourInjectedBeanClass yourInjectedField = null ;
    
            …
    }
    and in your applicationContext.xml
    Code:
    	<bean class="CLASS_THAT_WILL_HAVE_RESOURCE_INJECTED"  factory-method="aspectOf" />
    Last edited by JeitEmgie; Jul 5th, 2010 at 11:29 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •