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

In the last article 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 {
    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 {
        //Open the core transaction. The processed () is equivalent to our core method save()
package com.Demo6;

import org.springframework.context.ApplicationContext;

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

The next step is to focus on configuring the corresponding attributes in xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <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: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>


The operation results are as follows













Tags: xml Spring encoding

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