Spring AOP development of AOP based on AspectJ

Keywords: xml Spring Junit encoding

Realize AOP development mode based on AspectJ:

  • Annotation mode
  • XML mode
    AspectJ is an AOP framework based on Java language; support for AspectJ pointcut expressions has been added since spring 2.0
    @AspectJ is a new function of AspectJ 1.5. With JDK 1.5 annotation technology, you can directly define facets in Bean classes

@AspectJ provides different notification types

  • @Before notice, equivalent to BeforeAdvice
  • @AfterReturning post notification, equivalent to AfterReturning advice
  • @Around notification, equivalent to MethodInterceptor
  • @After final notification, no matter whether it is abnormal or not, it will execute
  • @The AfterThrowing exception throws a notification, which is equivalent to ThrowAdvice
  • @DeclareParents referral notification, equivalent to introductioninterceptor

Implement the above notification by annotation

<!---applicationContext3.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop" 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">
    <!--open AspectJ Annotation development, auto agent-->
    <aop:aspectj-autoproxy/>
    <!--Target class-->
    <bean id="productDao" class="com.imooc.aspectJ.demo3.ProductDao"/>
    <!--section-->
    <bean class="com.imooc.aspectJ.demo3.MyAspectJAnnotation"/>
</beans>
//ProductDao target class
package com.imooc.aspectJ.demo3;

public class ProductDao {
    public void find(){
        System.out.println("find ....");
    }

    public void update(){
        System.out.println("update....");
        int i =1/0;//Verify if an exception is thrown
    }

    public void delete(){
        System.out.println("delete......");
    }

    public String save(){
        System.out.println("save .......");
        return "Hello Ally441";
    }

    public void findOne(){
        System.out.println("findOne .......");
        int i =1/0;//Verify that the final method to throw the exception executes
    }
}
package com.imooc.aspectJ.demo3;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * Section class
 */
@Aspect
public class MyAspectJAnnotation {
    @Before(value="pointcut1()")
    public void Before(JoinPoint joinPoint){
        System.out.println("Before advice....."+joinPoint);
    }

    @AfterReturning(value = "pointcut2()",returning = "result")
    public void AfterReturning(Object result){
        System.out.println("Post notification....."+result);
    }

    @Around(value = "pointcut3()")
    public Object Around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("Before surround notification......");
        Object object = joinPoint.proceed();
        System.out.println("After surround notification......");
        return object;
    }

    @AfterThrowing(value = "pointcut4()" ,throwing = "e")
    public void AfterThrowing(Throwable e){
        System.out.println("Exception throw notification....."+e.getMessage());
    }

    @After(value = "pointcut5()")
    public void After(){
        System.out.println("Final notice......");
    }
    //Pointcut is used to define the pointcut, because if the pointcut is defined in each notice, repeated pointcuts will cause heavy workload and difficult maintenance
    @Pointcut(value = "execution(* com.imooc.aspectJ.demo3.ProductDao.find(..))")
    public void pointcut1(){}

    @Pointcut(value = "execution(* com.imooc.aspectJ.demo3.ProductDao.save(..))")
    public void pointcut2(){}

    @Pointcut(value = "execution(* com.imooc.aspectJ.demo3.ProductDao.delete(..))")
    public void pointcut3(){}

    @Pointcut(value = "execution(* com.imooc.aspectJ.demo3.ProductDao.update(..))")
    public void pointcut4(){}

    @Pointcut(value = "execution(* com.imooc.aspectJ.demo3.ProductDao.findOne(..))")
    public void pointcut5(){}
}
//Implementation class
package com.imooc.aspectJ.demo3;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext3.xml")
public class Springdemo3 {

    @Resource(name = "productDao")
    private ProductDao productDao;

    @Test
    public void demo1(){
        productDao.find();
        productDao.delete();
        productDao.save();
        productDao.findOne();
        productDao.update();
    }
}

Operation result:

Implement the above notification in XML

<!--applicationContext4.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop" 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">
    <!--XML Configuration mode of AOP Development-->
    <!--Configure target class-->
    <bean id="customerDao" class="com.imooc.aspectJ.demo2.CustomerDaoImpl"/>

    <!--Configure facet classes-->
    <bean id="myAspectXml" class="com.imooc.aspectJ.demo2.MyAspectXml"/>

    <!--aop Configuration-->
    <aop:config>
        <!--Define pointcuts: which classes and methods need to be enhanced-->
        <aop:pointcut id="pointCut1" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.update(..))"/>
        <aop:pointcut id="pointCut2" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.delete(..))"/>
        <aop:pointcut id="pointCut3" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.save(..))"/>
        <aop:pointcut id="pointCut4" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findOne(..))"/>
        <aop:pointcut id="pointCut5" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findAll(..))"/>

        <!--Configuration section-->
        <aop:aspect ref="myAspectXml">
            <aop:before method="before" pointcut-ref="pointCut1"/>
            <aop:after-returning method="afterReturning" pointcut-ref="pointCut2" returning="object"/>
            <aop:around method="around" pointcut-ref="pointCut3"/>
            <aop:after-throwing method="afterThrowing" pointcut-ref="pointCut4" throwing="e"/>
            <aop:after method="after" pointcut-ref="pointCut5"/>
        </aop:aspect>
    </aop:config>

</beans>
//Target class
package com.imooc.aspectJ.demo4;

public class ProductDao {
    public void find(){
        System.out.println("find ....");
    }

    public void update(){
        System.out.println("update....");
        int i =1/0;
    }

    public void delete(){
        System.out.println("delete......");
    }

    public String save(){
        System.out.println("save .......");
        return "Hello Ally441";
    }

    public void findOne(){
        System.out.println("findOne .......");
    }
}
//Section class
package com.imooc.aspectJ.demo4;


import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspectJXml {
    public void before(){
        System.out.println("Preamplifier.......");
    }

    public void afterReturning(Object object){
        System.out.println("Post enhancement......."+object);
    }

    public Object Around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("Before surround enhancement.......");
        Object object = joinPoint.proceed();
        System.out.println("After surround enhancement.......");
        return object;
    }

    public void afterThrowing(Throwable e){
        System.out.println("Exception throw notification......."+e.getMessage());
    }

    public void after(){
        System.out.println("Final notice.......");
    }
}
//Test class
package com.imooc.aspectJ.demo4;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext4.xml")
public class Springdemo4 {
    @Autowired
    private ProductDao productDao;

    @Test
    public void demo1(){
        productDao.find();
        productDao.findOne();
        productDao.delete();
        productDao.save();
        productDao.update();
    }
}

Operation result

Posted by winsonlee on Thu, 05 Dec 2019 14:26:33 -0800