1

I'm using CTW AspectJ and I'm trying to @inject a field into my Aspect. The injected field always returns null.

I'm using Java based configuration: @Configuration @EnableAspectJAutoProxy(proxyTargetClass=true) public class ServicesConfig..

The Spring itself is loaded using org.springframework.web.context.support.AnnotationConfigWebApplicationContext

From what I gather the my Aspect isn't instancing using the Spring framework but via the aspectj. what is missing to make the Aspect load with Spring?

This is how My aspect looks like:

@Aspect
public class MyAspect {

    @Inject
    private SomeBean someBean;

    @Around("execution(* com.mypackages..*(..))")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable   {
        Object result = null;
        //some Aspect logic
        return result;
    }
}

This is how the bean is instantiated, If I try to @inject it anywhere else in the application I'm getting the bean.

@Named
@Scope(value = "prototype")
public class SomeBean{
    //bean logic
}
smdy
  • 11
  • 3
  • show us the code please – Palcente Jun 04 '15 at 23:00
  • Is your aspect annotated as a Spring @Component? Without the code all we can do is guess. – sheltem Jun 05 '15 at 08:40
  • I have tired with and without the @Component and getting the same results. I've added My aspect code and bean code. – smdy Jun 05 '15 at 14:53
  • the around is bean call before the inject you can do one of the options 1.dont use the @inject 2 create bean post processor and there you can do what ever you want – Ran Adler Jun 07 '15 at 05:32

1 Answers1

0

The documentation for Spring-AspectJ with CTW can be found here. Note that you have to annotate your @Aspect with @Configurable. Take in account that you're probably missing the build phase or a part of it. I mean you have to include spring-aspects.jar as an aspects library (whether you are using Maven or Ant).

In addition, you can find good examples/answers here

Community
  • 1
  • 1
Modi
  • 2,200
  • 4
  • 23
  • 37