Spring深入讲解实现AOP的三种方式(spring aop用法)

网友投稿 251 2022-07-29


[重点] 使用AOP织入 需要导入一个依赖包

org.aspectj

aspectjweaver

1.9.9.1

方式一:使用原生Spring API接口

配置文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p" xmlns:c="http://springframework.org/schema/c"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p" xmlns:c="http://springframework.org/schema/c"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd">

Log

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {

//method:要执行的目标对象的方法

//objects:参数

//target:目标对象

@Override

public void before(Method method, Object[] args, Object target) throws Throwable {

System.out.println(target.getClass().getName() + "的" + method.getName() + "被执行了");

}

}

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {

//method:要执行的目标对象的方法

//objects:参数

//target:目标对象

//returnValue:返回值

@Override

public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {

System.out.println("执行了"+ method.getName() + "方法,返回结果为:"+returnValue);

}

}

Service

import org.springframework.stereotype.Service;

@Service

public interface UserService {

public void add();

public void delete();

public void update();

public void search();

}

public class UserServiceImpl implements UserService{

@Override

public void add() {

}

@Override

public void delete() {

}

@Override

public void update() {

}

@Override

public void search() {

}

}

test动态代理 代理的是接口(代理模式是SpringAOP的底层)

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

//动态代理 代理的是接口

UserService userService = context.getBean("userService", UserService.class);

userService.add();

}

}

方式二:使用自定义类

配置文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p" xmlns:c="http://springframework.org/schema/c"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p" xmlns:c="http://springframework.org/schema/c"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd">

DIY类

public class DiyPointCut {

public void before(){

System.out.println("````方法执行前````");

}

public void after(){

System.out.println("````方法执行后````");

}

}

其他的不变

方式三:使用注解实现

配置文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p" xmlns:c="http://springframework.org/schema/c"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p" xmlns:c="http://springframework.org/schema/c"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

自定义类

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

//使用注解方式实现AOP

@Aspect

//标注这个类是一个切面

public class AnnotationPointCut {

@Before("execution(* com.kero.service.UserServiceImpl.*(..))")

public void before(){

System.out.println("````方法执行前````");

}

@After("execution(* com.kero.service.UserServiceImpl.*(..))")

public void after(){

System.out.println("````方法执行后````");

}

}

其他不变

补充知识:execution表达式

execution表达式的详解

切入点表达式:execution(* 包名.*.*(..))

整个表达式可以分为五个部分:

1、execution(): 表达式主体。

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

3、包名:表示需要拦截的包名。

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

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

其中除了返回类型模式、方法名模式和参数模式外,其它项都是可选的。

举例:

execution(public * *(..)) 匹配所有的public修饰符的方法

execution(* set*(..)) 匹配所有”set”开头的方法:

execution(* com.kero.service.UserServiceImpl.*(..))) 匹配UserServiceImpl接口/类的所有方法:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:利用Java代码写一个并行调用模板(java怎么实现并行)
下一篇:Mybatis步骤分解实现一个增删改查程序(springboot整合mybatis实现增删改查)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~