栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring-AOP

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring-AOP

Spring Aop

Aop:面向切面编程,这个种编程的底层实现是代理模式,它允许用户自定义切面,其实就是再不改变原来代码的情况下,去增加功能。

有三种方式来实现Aop:第一种使用Spring的API来实现;第二种自定义类来实现;第三种通过注解来实现

SpringAOP中,通过Advice定义横切逻辑,Spring中支持5中类型的Advice

这里主要是在常用编程的增、删、改、查中添加日志来实现AOP

Aop的依赖


    org.aspectj
    aspectjweaver
    1.9.4

还需要去配置xml文件


方式一:使用Spring的API实现

XML文件




    
    
    
    

    
    
    
        
        
        
        
    

1、execution(): 表达式主体 (必须加上execution)。

2、第一个*号:表示返回值类型,*号表示所有的类型。

3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,com.zkw.service.*(…)包、子孙包下所有类的方法。

4、第二个*号:表示类名,*号表示所有的类。

5、*(…):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。

书写的注意事项:execution(* com.zkw.service.*(…).*.*(…))

业务接口

package com.zkw.service;

public interface UserService {
    public void add();
    public void del();
    public void modify();
    public void select();
}

业务接口实现

package com.zkw.service;

public class UserServiceImpl implements UserService {
    public void add() {
        System.out.println("增加了一个用户");
    }

    public void del() {
        System.out.println("删除了一个用户");
    }

    public void modify() {
        System.out.println("修改了一个用户");
    }

    public void select() {
        System.out.println("查询了一个用户");
    }
}

日志类(前置日志、后置日志)

package com.zkw.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class BeforeLog implements MethodBeforeAdvice {
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"使用了"+method.getName()+"方法");
    }
}
package com.zkw.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println(method.getName()+"方法被使用了,方法的返回值为:"+returnValue);
    }
}

测试类

import com.zkw.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void TestAop(){
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
        userService.modify();
    }
}

结果如下


方式二:自定义类实现

此时只需要把XML文件的方式一那一部分替换成这个就可以了

XML文件



    
    
        
        
        
        
        
    

日志类

package com.zkw.diy;

public class DiyPointCut {
    public void before(){
        System.out.println("========方法执行前========");
    }

    public void after(){
        System.out.println("========方法执行后========");
    }
}

其它的不变执行结果如下


方式三:通过注解来实现

XML文件

 
    
   
    

日志类

package com.zkw.diy;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect //标注这是一个切面
public class AnnotationPointCut {

    @Before("execution(* com.zkw.service.UserServiceImpl.*(..))")
    public void AnnotationBefore(){
        System.out.println("====方法执行前====");
    }

   @After("execution(* com.zkw.service.UserServiceImpl.*(..))")
    public void AnnotationAfter(){
        System.out.println("====方法执行后====");
    }

    //在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入点
    @Around("execution(* com.zkw.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前");

        Signature signature = pjp.getSignature();//获得标签
        System.out.println(signature);

        pjp.proceed();//执行方法

        System.out.println("环绕后");
    }
}

其它不变,结果如下

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/324668.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号