Results 1 to 5 of 5

Thread: @Before advice to get the (spring)bean ID ?

  1. #1
    Join Date
    Jan 2005
    Posts
    144

    Default @Before advice to get the (spring)bean ID ?

    Hello

    I'd like to write a security advice that will require as parameter the ID of the spring bean beeing intercepted. Accoring to spring AOP reference, the AspectJ expression can be used to bind proxy, target, args .. to the advice, but I've not found anything beeing spring-dedicated

    is there something equivalent to the
    Code:
    bean()
    join point for binding advice parameters ?

  2. #2
    Join Date
    Jan 2005
    Posts
    144

    Default

    Really no way for doing this ???

  3. #3
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    Well, there is a bean() pointcut designator (see http://blog.springsource.com/main/20...bean-pointcut/). However, it does selection based on bean ids (and you seem to want to collect the bean id). For your problem, you may have your beans implement the BeanNameAware interface and collect the bean using this() pointcut to obtain the id.

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  4. #4
    Join Date
    Jan 2005
    Posts
    144

    Default

    That requires all my adviced beans to implement BeanNameAware + another interface for a custom getBeanName().

    I'l try another option to use the @Service annotation to declare my beans and to read it by reflection to retrieve the bean ID.

  5. #5
    Join Date
    Jul 2008
    Posts
    3

    Default

    Ok, I'm a little late but I think I found a solution. Simply ask the applicationContext:

    Code:
    @Aspect
    public class SecurityAspect implements ApplicationContextAware {
    
    	private ApplicationContext applicationContext;
    	
    	public void setApplicationContext(ApplicationContext applicationContext)
    			throws BeansException {
    		this.applicationContext = applicationContext;
    	}
    
    	@Around("execution(* *.*(..))")
    	public Object secure(ProceedingJoinPoint proceedingJoinPoint)
    			throws Throwable {
    
    		String[] beanNames = applicationContext.getBeanNamesForType(proceedingJoinPoint
    				.getSignature().getDeclaringType(), false, false);
    
            ...
    		return proceedingJoinPoint.proceed();
    	}
    }

Posting Permissions

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