Part 3 - Writing logging aspects

Let's now add the aspect that will handle logging with the help of following steps:

  1. Create MyLoggingAspect as a Java class in the com.packt.ch04.aspects package, which will have a method for the Before advice.
  2. Add a data member of type org.apache.log4j.Logger in it.
  1. Add a beforeAdvise()method in it. The signature of the method can be anything. Here, we are adding Joinpoint as an argument. Using this argument, we can get the information about the class where the aspect is being applied. The code will be as follows:
public class MyLoggingAspect { 
  Logger logger=Logger.getLogger(getClass()); 
  public void beforeAdvise(JoinPoint joinPoint) { 
    logger.info("method will be invoked :- 
    "+joinPoint.getSignature()); 
  } 
} 
  1. Now, the aspect has to be configured in the XML file in three steps:

1. Add a namespace for AOP using the following piece of code:

<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:aop="http://www.springframework.org/schema/aop" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd"> 

Now we can use AOP's tags using the aop namespace.

2. Add a bean for the aspect that we want to use in the application in connection_new.xml, as shown in the following lines of code:

<bean id="myLogger" 
  class="com.packt.ch04.aspects.MyLoggingAspect"/> 

3. Configure the Aspect.

  1. Each <aop:aspect> enables us to write an aspect inside the<aop:config> tag.
  2. Each aspect will have an id and ref as attributes. The ref attribute refers to the bean whose methods will be invoked to provide advice.
  3. Configure the advice for a pointcut expression, and the method that is to be invoked for the advice. The Before advice can be configured inside<aop:aspect> using the<aop:before>tag.

Let's write the Before advice to apply for the myLogger aspect, which will be invoked before the addBook() method of BookDAO. The configuration will be as follows:

<aop:config> 
  <aop:aspect id="myLogger"ref="logging"> 
    <aop:pointcut id="pointcut1" 
     expression="execution(*com.packt.ch03.dao.BookDAO.addBook(
com.packt.ch03.beans.Book))"/> <aop:before pointcut-ref="pointcut1"method="beforeAdvise"/> </aop:aspect> </aop:config>
  1. Copy MainBookDAO_operation.java from the Ch03_JdbcTemplates project, and execute it to get the following output on the console:
0    [main] INFO  org.springframework.context.support.ClassPathXmlApplicationContext  - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@533e64: startup date [Sun Oct 02 23:44:36 IST 2016]; root of context hierarchy 
66   [main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from class path resource [connection_new.xml] 
842  [main] INFO  org.springframework.jdbc.datasource.DriverManagerDataSource  - Loaded JDBC driver: com.mysql.jdbc.Driver 
931  [main] INFO  com.packt.ch04.aspects.MyLoggingAspect  - method will be invoked :-int com.packt.ch03.dao.BookDAO.addBook(Book) 
book inserted successfully 
book updated successfully 
book deleted successfully 

Here, BookDAO_JdbTemplate works as a target object, whose proxy will be created at runtime by weaving the code of the addBook() and beforeAdvise() methods. Now, once we know the process, let's add different pointcuts and advice one by one in the application with the help of the following steps.

More than one Advice can be applied on the same Joinpoint; however, for the simplicity of understanding the pointcuts and advices, we will keep a single advice each time with the comment already written.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.97.189