Using Aspectj to implement AOP ------------ configuration through XML annotation

In the last article https://blog.csdn.net/wyf2017/article/details/81239476 We briefly introduced that AOP is implemented in classes through Aspectj. Next, we will introduce how to implement AOP in xml at the end of next week

First, we delete all the annotations written in the class, as shown below

package com.Demo6;



public class UserDao implements IUserDao {
    @Override
    public void save() {
        System.out.println("-----------Open connection-----------");
       // int i = 1/0;
    }
}
package com.Demo6;

public interface IUserDao {
    public void save();
}
package com.Demo6;


public class OrderDao {
    public void save() {
        System.out.println("..............Complete modification..............");
    }
}
package com.Demo6;

import org.aspectj.lang.ProceedingJoinPoint;



public   class MyAop {


    public void beginTran(){
        System.out.println("...............Open transaction");
    }

    public void commite(){
        System.out.println("...............Submission of affairs");
    }

    public void afterReturn(){
        System.out.println("------------Used is AfterReturning-------------");
    }

    public void afterThrow(){
        System.out.println("------------Used is AfterThrowing-------------");
    }

    public void arround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("------------begin------------");
        //Open the core transaction. The processed () is equivalent to our core method save()
        proceedingJoinPoint.proceed();
        System.out.println("------------end------------");
    }
}
package com.Demo6;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Test4 {
    public static void main(String []args){
        ApplicationContext ap = new ClassPathXmlApplicationContext("bean2.xml");
        IUserDao proxy = (IUserDao) ap.getBean("userDao");
        proxy.save();
        OrderDao orderDao = (OrderDao) ap.getBean("orderDao");
        orderDao.save();
    }
}

The next step is to focus on configuring the corresponding attributes in 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:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean  id="userDao" class="com.Demo6.UserDao"></bean>
    <bean  id="orderDao" class="com.Demo6.OrderDao"></bean>
    <!--Create a tangent class-->
    <bean id="aop" class="com.Demo6.MyAop"></bean>
    <!--breakthrough point-->
    <aop:config>
        <aop:pointcut id="pt" expression="execution(* * .* ())"></aop:pointcut>
        <aop:aspect  ref="aop">
            <aop:before method="beginTran" pointcut-ref="pt"></aop:before>
            <aop:after method="commite" pointcut-ref="pt"></aop:after>
            <aop:after-returning method="afterReturn" pointcut-ref="pt"></aop:after-returning>
            <aop:after-throwing method="afterThrow" pointcut-ref="pt"></aop:after-throwing>
            <aop:around method="arround" pointcut-ref="pt"></aop:around>
        </aop:aspect>
    </aop:config>

</beans>

The operation results are as follows

 

 

 

 

 

 

 

 

 

 

 

 

Tags: xml Spring encoding

Posted on Fri, 31 Jan 2020 08:07:51 -0800 by Brendan Nolan