1
votes

I am trying to print logs for private and package scoped methods (from package scoped classes) in my application which is based on Spring. Since Spring's proxy based aspects don't work on private and package scoped methods(?), I tried to use AspectJ load time weaving as per the documentation. Below are the details:

LoggingAspect

@Component(Constants.LOGGING_ASPECT_BEAN)
@Aspect
public class LoggingAspect {
  @Around("@annotation(my.pkg.Loggable)")
  public Object doLogging(final ProceedingJoinPoint joinPoint) throws Throwable {
    // ... logging code.
  }
}

Spring Configuration

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@Import(AnotherBaseConfig.class)
@ComponentScan("my.pkg")
@EnableWebMvc
@EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.AUTODETECT)
public class AppConfiguration extends WebMvcConfigurerAdapter {
  // ... bean configs
}

src/main/webapp/META-INF/aop.xml

<aspectj>
  <weaver>
    <include within="my.pkg.*">
  </weaver>
  <aspects>
    <aspect name="my.logger.LoggingAspect"/>
  </aspects>
</aspectj>

JVM Config

-javaagent:"/path/to/spring-instrument-4.0.6.jar" -javaagent:"/path/to/aspectjweaver-1.8.1.jar"

With above configurations, I have my package scoped classes as:

@Component("someClass")
class SomeClass {
  @Loggable
  void doSomething(@Loggable final String s, @Loggable final Integer i) {
    // ... do something here.
  }
}

The weaving is not working for such package scoped classes. Am I doing anything wrong?

1
Can anyone from spring team help me here? - Niranjan

1 Answers

1
votes

You are mixing up two AOP styles here:

  • Spring AOP ist a proxy-based "AOP lite" approach which is activated via @EnableAspectJAutoProxy. It only works for public, non-static methods of Spring components.
  • AspectJ, a full-blown AOP framework which also works on package-local or private methods, no matter if they are Spring components or not, is enabled from within Spring via @EnableLoadTimeWeaving and the weaving agent via -javaagent:"/path/to/aspectjweaver-1.8.1.jar". In this case your aspect does not need to be a Spring component either. P.S.: I recommend to use the latest AspectJ version 1.8.5, not 1.8.1.

As you want to work with package-locals, you need AspectJ LTW. Please configure Spring clearly in preference of one AOP type. More information can be found in the Spring Manual, chapter 9. Specific information about AspectJ LTW configuration is in chapter 9.8.