Spring AOP 的实现

Spring AOP 的实现,第1张

Spring AOP 的实现 Spring AOP 的实现 环境搭建

坐标依赖引入 pom.xml

    
    
    
      org.springframework
      spring-context
      5.2.4.RELEASE
    

    
    
      org.aspectj
      aspectjweaver
      1.8.9
    

添加spring.xml的配置

添加命名空间





    
    

    
    



注解实现

UserService.java

package com.xxxx.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {
    public void test(){
        System.out.println("UserService...");
        //int i= 1/0;
    }
}

定义切面

package com.xxxx.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;


@Component//将对象交给IOC容器维护
@Aspect//表示当前是一个切面类
public class LogCut {

    
    @Pointcut("execution (* com.xxxx.service..*.*(..) )")
    public void cut(){}

    
    @Before(value = "cut()")
    public void before(){
        System.out.println("前置通知...在方法执行前执行的通知");
    }

    
    @AfterReturning(value = "cut()")
    public void afterReturning(){
        System.out.println("返回通知...方法正常执行完后的通知");
    }

    
    @After(value = "cut()")
    public void after(){
        System.out.println("最终通知...方法执行结束执行的通知");
    }

    
    @AfterThrowing(value = "cut()",throwing = "e")
    public void afterThrowing(Exception e){
        System.out.println("异常通知...方法抛出异常时执行的通知"+ " 异常原因:" + e.getMessage());
    }

    
    @Around(value = "cut()")
    public Object around(ProceedingJoinPoint pjp) {
        System.out.println("环绕通知---前置通知...");
        Object object = null;
        try {
            //需要通过显式调⽤对应的⽅法,否则⽆法访问指定⽅法
            object = pjp.proceed();
            System.out.println(pjp.getTarget() + "======" + pjp.getSignature());
            System.out.println("环绕通知---返回通知...");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("环绕通知---异常通知...");
        }
        System.out.println("环绕通知---最终通知...");
        return object;
    }

}

starter.java

package com.xxxx;

import com.xxxx.dao.UserDao;
import com.xxxx.service.UserService;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Starter {

    public static void main(String[] args) {

        //得到上下文环境
        BeanFactory factory = new ClassPathXmlApplicationContext("spring.xml");

        //得到bean对象
        UserService userService = (UserService) factory.getBean("userService");
        userService.test();

    }
}

XML实现

定义切面

package com.xxxx.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;


@Component//将对象交给IOC容器维护
public class LogCut {

    
    public void cut(){}

    
    public void before(){
        System.out.println("前置通知...在方法执行前执行的通知");
    }

    
    public void afterReturning(){
        System.out.println("返回通知...方法正常执行完后的通知");
    }

    
    public void after(){
        System.out.println("最终通知...方法执行结束执行的通知");
    }

    
    public void afterThrowing(Exception e){
        System.out.println("异常通知...方法抛出异常时执行的通知"+ " 异常原因:" + e.getMessage());
    }

    
    public Object around(ProceedingJoinPoint pjp) {
        System.out.println("环绕通知---前置通知...");
        Object object = null;
        try {
            //需要通过显式调⽤对应的⽅法,否则⽆法访问指定⽅法
            object = pjp.proceed();
            System.out.println(pjp.getTarget() + "======" + pjp.getSignature());
            System.out.println("环绕通知---返回通知...");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("环绕通知---异常通知...");
        }
        System.out.println("环绕通知---最终通知...");
        return object;
    }

}

配置文件





    
    


    
    
   		
        
            
            
            
            
            
            
            
            
            
            
            
            
        
    



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5076517.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存