0

I have a Spring AOP aspect used for logging, where a method can be included for logging by adding an annotation to it, like this:

@AspectLogging("do something")
public void doSomething() {
    ...
}

I've been using this on Spring beans and it's been working just fine. Now, I wanted to use it on a REST-service, but I ran into some problems. So, I have:

@Path("/path")
@Service
public class MyRestService {
    @Inject
    private Something something;

    @GET
    @AspectLogging("get some stuff")
    public Response getSomeStuff() {
        ...
    }
}

and this setup works just fine. The Rest-service that I'm trying to add the logging to now has an interface, and somehow that messes stuff up. As soon as I add the @AspectLogging annotation to one of the methods, no dependencies are injected in the bean, and also, the aspect is newer called!

I've tried adding an interface to the REST-service that works, and it gets the same error.

How can having an interface lead to this type of problems? The aspect-logger works on classes with interfaces elsewhere, seems it's only a problem when it's a REST-service..

Tobb
  • 11,850
  • 6
  • 52
  • 77

2 Answers2

2

Ref the below Spring documentation (para 2) -

To enable AspectJ annotation support in the Spring IoC container, you only have to define an empty XML element aop:aspectj-autoproxy in your bean configuration file. Then, Spring will automatically create proxies for any of your beans that are matched by your AspectJ aspects.

For cases in which interfaces are not available or not used in an application’s design, it’s possible to create proxies by relying on CGLIB. To enable CGLIB, you need to set the attribute proxy-targetclass= true in aop:aspectj-autoproxy.

In case your class implements an interface, a JDK dynamic proxy will be used. However if your class does not implement any interfaces then a CGLIB proxy will be created. You can achieve this @EnableAspectJAutoProxy. Here is the sample

@Configuration
@EnableAspectJAutoProxy
public class AppConfig {

 @Bean
 public LoggingAspect logingAspect(){
    return new LoggingAspect();
 }
}



@Component
@Aspect
public class LoggingAspect {

  ...
  ...

}
Pankaj
  • 3,512
  • 16
  • 49
  • 83
1

In my opinion what you are actually trying to do is to add spring annotations to a class maintained by jersey. In the result you are receiving a proxy of proxy of proxy of somethng. I do not think so this is a good idea and this will work without any problems. I had a similar issue when I tried to implement bean based validation. For some reasons when there were @PahtParam and @Valid annotations in the same place validation annotations were not visible. My advice is to move your logging to a @Service layer instead of @Controller.

Marek Raki
  • 3,056
  • 3
  • 27
  • 50
  • It might be something like that, though the rest-service already has @Inject annotations, so it will have spring-annotations in any case. But it might be that annotating methods messes stuff up.. – Tobb Aug 06 '14 at 07:15