Results 1 to 2 of 2

Thread: Advice on @Autowired

  1. #1
    Join Date
    Jun 2010
    Posts
    1

    Default Advice on @Autowired

    Hi all,

    I'd like to weave an aspect on the @Autowired annotation in order to detect beans resolution failure, within a port from a Carbon environment.
    The idea, is to inject in the Spring converted components some Carbon components using Spring's annotation. So through the porting process I won't have to come back to these injections.


    My first idea was to weave a NoSuchBeanException aspect (throwing) on @Autowired ; but I could not manage to weave on an annotation ; furthermore, at the exception's throwing, arm is already done : impossible to stop the exception I guess.

    Thus, I did that :
    <aop:aspectj-autoproxy />
    <bean id="carbonAutowiredAdvice" class="com.egencia.carbon.components.CarbonAutowir edAdvice" />
    @Aspect
    public class CarbonAutowiredAdvice {
    private static final Log LOG = LogFactory.getLog(CarbonAutowiredAdvice.class);

    @Around("execution(* org.springframework.beans.factory.support.DefaultL istableBeanFactory.resolveDependency(..)) " +
    "&& args(descriptor,beanName)")
    public Object carbonAutowired(ProceedingJoinPoint invocation,
    DependencyDescriptor descriptor, String beanName)
    throws Throwable {
    Object result = null;
    try {
    result = invocation.proceed();
    } catch (NoSuchBeanDefinitionException se) {
    LOG.info("////////// attempt to inject Carbon component...");
    try {
    // find the targeted interface
    Class c = Class.forName(beanName);
    assert c.isInterface();
    // look for an inner DEFAULT_COMPONENT_PATH field
    Field f = c.getField("DEFAULT_COMPONENT_PATH");
    String componentPath = (String) f.get(null); //FIXME ok static fields ?

    // retrieve the component in Carbon context
    result = Lookup.getInstance().fetchComponent(componentPath) ;
    } catch (Exception ce) {
    LOG.debug(ce);
    throw se; // in case of failure, let's be forgotten
    }
    }
    return result;
    }
    }

    But nothing happens. My impression is that the weaving is only occurring after beans instantiation, right ?
    If anyone sees thoughts or code errors, has another idea... please let me know because I'm feeling a bit depressed about this right now ;-)
    Thanks in advance.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    there is no weaving.... I suggest you read chapter 7 of the reference guide (or 6 prior to spring 3).

    Spring uses proxy based aop and proxies are created AFTER the dependencies are injected so what you want to achieve isn't possible with spring aop, you will have to use either loadtime or compile time weaving.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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