Search code examples
javaloggingaopaspect

AOP without Spring


I implemented Logger using Spring 4.0 and AspectJ. But I'm now trying to make the logger independent of Spring. I couldn't initialize aspect in my application. Any hint to start will be helpful.

Thanks in advance.


Solution

  • If you have a Spring- based app and you want to use AspectJ, it is better to use the AspectJ features offered by Spring. You can also use Spring AOP without AspectJ.

    You can make cross-cutting functionality by using AspectJ annotations with Spring AOP.

    @Aspect annotation allow you to make the aspect from your Logger class.

    In the Logger class define set of the pointcut by using the @Pointcut annotation. By using annotations @Before, @AfterReturning, @AfterThrowing define the advices and when they will be applied relative to the set of the pointcut (before, after, in the case of throwing an exception).

    Then include the aop namespace in Spring configuration file and register the object of the class AnnotationAwareAspectJAutoProxyCreator by using <aop:aspectj-autoproxy> configuration element.

    This component also performs proxying of classes which have @Aspect annotation and which have methods included in a certain set of the pointcut.

    How to use AspectJ without Spring you can find here, for example: Using AspectJ logging without Spring