20

I am new to spring in my office . So there is no guidance for me.

I need to implement the logging with the AOP using the log4j.

I have implemented the logging without AOP in basic spring MVC example ?

Also did the small sample in AOP using the aspectJ without logging (just made the Sysout) ?

I don't know how to integrate it ?

Can any one please give me a start up idea?

Good answers are definitely appreciated ...

Human Being
  • 8,269
  • 28
  • 93
  • 136

2 Answers2

36

Spring makes it really easy for us to make use of AOP. Here's a simple logging example:

@Aspect
public class MyLogger {

    private Logger log = Logger.getLogger(getClass());

    @After("execution(* com.example.web.HomeController.*(..))")
    public void log(JoinPoint point) {
        log.info(point.getSignature().getName() + " called...");
    }
}

Then simply configure your applicationContext.xml (or equivalent):

    <aop:aspectj-autoproxy>
        <aop:include name="myLogger"/>
    </aop:aspectj-autoproxy>

    <bean id="myLogger" class="com.example.aspect.MyLogger"/>

You'll notice in the MyLogger class that I specified @After right above the method. This is called the advice and it basically specifies that this 'log' method will be called after the method in question. Other options include @Before, @Around, @AfterThrowing.

The expression "execution(* com.example.web.HomeController.*(..))" is called a pointcut expression and specifies what we're targeting (in this case all methods of the HomeController class).

P.S. The aop namespace (xmlns:aop="http://www.springframework.org/schema/aop") and the schema location (version dependent) would need to be added to your applicationContext.xml right at the top. Here is my setup:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
Markus Coetzee
  • 3,384
  • 1
  • 29
  • 26
  • Don't you also have to annotate the MyLogger class with `@Component`? You also need the `component-scan` element in your applicationContext.xml if you don't already. – Sotirios Delimanolis Apr 01 '13 at 20:16
  • 1
    The @Component annotation is used (amongst others) to tell Spring that the class in question should be managed in the container (i.e. a Spring bean). When you specify a bean in your application context like the MyLogger class, then you are effectively telling Spring to manage the class (thus there's no need for the annotation), which allows you put classes into the container from 3rd party libraries. – Markus Coetzee Apr 01 '13 at 21:01
  • 3
    From the [documentation](http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html): `You may register aspect classes as regular beans in your Spring XML configuration, or autodetect them throuch classpath scanning - just like any other Spring-managed bean. However, note that the @Aspect annotation is not sufficient for autodetection in the classpath: For that purpose, you need to add a separate @Component annotation (or alternatively a custom stereotype annotation that qualifies, as per the rules of Spring's component scanner).` – Sotirios Delimanolis Apr 01 '13 at 21:20
  • `` probably does the same thing. – Sotirios Delimanolis Apr 01 '13 at 21:21
  • 100% right. It doesn't matter how the class gets in the container, be it via annotation or XML configuration (just not both) :) – Markus Coetzee Apr 01 '13 at 22:18
  • @Markus Coetzee Well Said ! Before that Can u please tell me what is the difference between the logging using `log4j.properties` and `log4j.xml`? – Human Being Apr 02 '13 at 06:43
  • @Markus Coetzee What is the best choice to use `log4j.properties` Vs`log4j.xml` for logging ? – Human Being Apr 02 '13 at 06:44
  • @Anand Glad to help. I think this might answer your log4j.properties vs log4j.xml question: http://stackoverflow.com/questions/1256835/why-chose-xml-over-properties-files-for-log4j-configuration – Markus Coetzee Apr 02 '13 at 08:20
  • @Markus Thanks for your reply...Can u please make a new chat room for us now ?Having some more doubts to clarify with you ? – Human Being Apr 02 '13 at 08:30
  • 1
    Some serious limitations - spring logging is limited only to spring beans and the only join points supported are public methods on those beans. – ggb667 Apr 04 '14 at 17:17
  • Why don't you just add this method to your "HomeController" and call it when you want to log? Spring AOP interferes with normal execution, making it impossible to follow sequentially. It's a lot of extra effort with no benefits. – Philip Rego May 09 '19 at 21:05
0

You need to perform several steps to integrate Aspectj:

  1. Install AspectJ
  2. Add your aop.xml to META-INF\aop.xml in your project
  3. Add aspectjrt-x.x.0.jar and aspectjweaver-x.x.0.jar in your project classpath
  4. Add -javaagent:/path to aspectj installation/aspectjweaver-1.7.0.jar to your server's JVM.

Here is a sample aop.xml:

<aspectj>
 <aspects>
  <aspect name="test.MySimpleLoggerAspect" />
 </aspects>
 <weaver>
  <include within="test.myproject.*" />
 </weaver>     
</aspectj>

If you are already using Spring then it is better to use Spring to simplify your setup.

Laurel
  • 5,965
  • 14
  • 31
  • 57
Avinash Singh
  • 3,421
  • 2
  • 20
  • 21