spring-aop-xml全自动

切面类

package com.mu.aop_xml.jdk_proxy.service.impl;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * 半自动/全自动切面类
 */
public class MyAspect1 implements MethodInterceptor{

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("开启事务");
        Object proceed = methodInvocation.proceed();
        System.out.println("拦截的返回值为"+proceed);
        System.out.println("提交事务");
        return proceed;
    }
}

目标类

package com.mu.aop_xml.jdk_proxy.service.impl;

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

/**
 * aspect讲解  五大通知类型
 */
public class MyAspect2 {
    /**
     * 获取目标类方法名
     * @param joinPoint 连接点
     */
  public void myBefoe(JoinPoint joinPoint){
      System.out.println("前置通知---"+joinPoint.getSignature().getName());
  }

    /**
     *  获取目标类方法名
     * @param joinPoint  连接点
     * @param setValue 方法执行后的返回值
     */
  public void myAfter(JoinPoint joinPoint,Object setValue){
      System.out.println("后置通知"+joinPoint.getSignature().getName()+"返回值"+setValue);
  }
    public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕通知前");
        Object proceed = proceedingJoinPoint.proceed();
//        获取目标类方法名
        System.out.println(proceedingJoinPoint.getSignature().getName());
        System.out.println("环绕通知后");
        return proceed;
    }

    public void myThrowing(JoinPoint joinPoint,Throwable throwable){
        System.out.println("异常通知---"+joinPoint.getSignature().getName()+"异常:"+throwable.getMessage());
    }
    public void  myZuiZhong(JoinPoint joinPoint){
        System.out.println("最终通知----"+joinPoint.getSignature().getName());
    }
}
package com.mu.aop_xml.jdk_proxy.service.impl;

import com.mu.aop_xml.jdk_proxy.service.UserService;

public class UserServiceImpl implements UserService {
    @Override
    public void addUser() {
        System.out.println("新增用户");
        try {
            int a=10/0;
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void updateUser() {
        System.out.println("更新用户");
    }

    @Override
    public void deleteUser() {
        System.out.println("删除用户");
    }

    @Override
    public int queryById(int id) {
        System.out.println("根据id查找并返回 id");
        return id;
    }
}

xml



    
    
    
    
    
        
        
    

测试

package com.mu.aop_xml.jdk_proxy.test;

import com.mu.aop_xml.jdk_proxy.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *  Spring编写代理全自动讲解
 *  @date 2019/09/26
 */
public class Test3 {
public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans13.xml");
    UserService userService = (UserService) context.getBean("userService");
    userService.queryById(55);
    userService.addUser();
}
}

你可能感兴趣的:(spring-aop-xml全自动)