Implementation of spring aop based on aspectj framework

Required jar packages:

 

 

 

xml configuration file way to achieve aop operation

 

① Create enhanced interface ThinkDao.java and interface implementation ThinkDaoImpl.java (cutting point)

1  package com.xml.dao;
 2  
3  / ** 
4  * Enhanced interface 
 5   * / 
6  public  interface ThinkDao {
 7  
8      / ** 
9       * Enhanced method
 10       * / 
11      public  void thinkDao ();
 12  }
 13  
14  
15  
16  package com.xml.dao.impl;
 17  
18  import com.xml.dao.ThinkDao;
 19  
20  / ** 
21  * Enhanced method implementation class
 22   * / 
23  public  classThinkDaoImpl implements ThinkDao {
 24  
25      @Override
 26      public  void thinkDao () {
 27          System.out.println ("I am the enhanced method ..." );
 28      }
 29 }

 

② Create Real.java (cutting plane)

1  package com.xml.dao;
 2  
3  import org.aspectj.lang.ProceedingJoinPoint;
 4  
5  / ** 
6  * Enhanced class (notification)
 7  * @author bianxc
 8   * / 
9  public  class Real {
 10  
11      public  void before ( ) {
 12          System.out.println ("Front notice ..." );
 13      }
 14      
15      public  void after () {
 16          System.out.println ("Post notice ..." );
 17      }
 18     
19     /**
20      * 环绕增强
21      * @param proceedingJoinPoint
22      * @throws Throwable
23      */
24     public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
25         System.out.println("方法之前。。。");
26         proceedingJoinPoint.proceed();
27         System.out.println("方法之后。。。");
28     }
29 }

 

③ Core configuration file xmlContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xsi:schemaLocation="
 6         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">
 8         
 9         <!>--Instantiate ThinkDao class-
10         <bean id="thinkDao" class="com.xml.dao.impl.ThinkDaoImpl"/>
11         <!-- 增强类对象 -->
12         <bean id="real" class="com.xml.dao.Real"/>
13         
14         <!-- aop操作 -->
15         <aop:config>
16             <!-- 切入点 -->
17             <aop:pointcut expression="execution(* com.xml.dao.impl.ThinkDaoImpl.*(..))" id="pc"/>
>Cutting surface-<!-18             
19             <aop:aspect ref="real">
20                 <!-- 前置通知 -->
21                 <aop:before method="before" pointcut-ref="pc"/>
22                 <!-- 后置通知 -->
23                 <aop:after method="after" pointcut-ref="pc"/>
24                 <!-- 环绕通知 -->
25                 <aop:around method="around" pointcut-ref="pc"/>
26             </aop:aspect>
27         </aop:config>
28 </beans>

 

④ Create test class TestXml.java

 1 package com.xml.test;
 2 
 3 import org.junit.jupiter.api.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import com.xml.dao.ThinkDao;
 8 
 9 public class TestXml {
10     
11     @Test
12     public void testXml() {
13         ApplicationContext context = new ClassPathXmlApplicationContext("xmlContext.xml");
14         ThinkDao thinkDao = (ThinkDao) context.getBean("thinkDao");
15         thinkDao.thinkDao();
16     }
17 }

 

 

 

Annotation to achieve aop operation

Note: The use of annotations to achieve aop operation is much simpler than the code of the xm configuration file method, reducing the cumbersome configuration in the configuration file

You only need to enable the automatic proxy of aop in the configuration file, and the automatic scanning of annotations. The rest is implemented by annotations.

 

① Create enhanced interface ThinkDao.java and interface implementation ThinkDaoImpl.java (cutting point)

1  package com.anno.dao;
 2  
3  / ** 
4  * Enhanced interface
 5   * / 
6  public  interface ThinkDao {
 7  
8      / ** 
9       * Enhanced method
 10       * / 
11      public  void thinkDao ();
 12  }
 13  
14  
15  
16  package com.anno.dao.impl;
 17  
18  import org.springframework.stereotype.Component;
 19  
20  import com.anno.dao.ThinkDao;
 21  
22  / ** 
23 * Enhanced method implementation class
 24   * / 
25 @Component ("thinkDao" )
 26  public  class ThinkDaoImpl implements ThinkDao {
 27  
28      @Override
 29      public  void thinkDao () {
 30          System.out.println ("I am the enhanced method ... . " );
 31      }
 32  
33 }

 

② Create Real.java (cutting plane)

 1 package com.anno.dao.impl;
 2 
 3 import org.aspectj.lang.ProceedingJoinPoint;
 4 import org.aspectj.lang.annotation.After;
 5 import org.aspectj.lang.annotation.Around;
 6 import org.aspectj.lang.annotation.Aspect;
 7 import org.aspectj.lang.annotation.Before;
 8 import org.springframework.stereotype.Component;
 9 
10 /**
11  * 增强类(通知)
12  * @author bianxc
13  */
14 @Component("real")
15 @Aspect
 16  public  class Real {
 17  
18      // Front notice 
19      @Before ("execution (* com.anno.dao.impl.ThinkDaoImpl. * (..))" )
 20      public  void before () {
 21          System. out.println ("Front Notice ..." );
 22      }
 23      
24      // Post-Enhanced 
25      @After ("execution (* com.anno.dao.impl.ThinkDaoImpl. * (..))" )
 26      public  void after () {
 27          System.out.println ("Post notification ..." );
 28      }
 29      
30      // 环绕增强
31     @Around("execution(* com.anno.dao.impl.ThinkDaoImpl.*(..))")
32     public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
33         System.out.println("方法之前。。。");
34         proceedingJoinPoint.proceed();
35         System.out.println("方法之后。。。");
36     }
37 }

 

③ core configuration file annoContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
 9          http://www.springframework.org/schema/context http: // www.springframework.org/schema/context/spring-context.xsd " > 
10          
11          <!- Enable annotation scanning- > 
12          < context: component-scan base-package =" com.anno. * " > </ context: component-scan > 
13          <!- Turn on automatic proxy- > 
14          < aop: aspectj-autoproxy /> 
15  </ beans >

 

④ Create test class TestAnno.java

 1 package com.anno.test;
 2 
 3 import org.junit.Test;
 4 import org.junit.runner.RunWith;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.test.context.ContextConfiguration;
 7 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 8 
 9 import com.anno.dao.ThinkDao;
10 
11 @ContextConfiguration("classpath:annoContext.xml")
12 @RunWith(SpringJUnit4ClassRunner.class)
13 public class TestAnno {
14     
15     @Autowired
16     private ThinkDao thinkDao;
17 
18     @Test
19     public void testAnno() {
20         thinkDao.thinkDao();
21     }
22 }

 

Results of the:

 

 

Summary: Annotated implementation of aop simplifies the bloated code in the xml configuration file, which is usually developed in the form of annotations in enterprise development!

 

Guess you like

Origin www.cnblogs.com/bianxcArticle/p/12704744.html