服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Spring深入讲解实现AOP的三种方式

Spring深入讲解实现AOP的三种方式

2022-12-20 17:16张志明(努力奋斗版) Java教程

Spring的AOP就是通过动态代理实现的,使用了两个动态代理,分别是JDK的动态代理和CGLIB动态代理,本文重点给大家介绍下Spring Aop的三种实现,感兴趣的朋友一起看看吧

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

?
1
2
3
4
5
6
7
<dependencies>
      <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.9.9.1</version>
      </dependency>
  </dependencies>

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

配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--    注册bean-->
    <bean id="userService" class="com.kero.service.UserServiceImpl"/>
    <bean id="log" class="com.kero.log.Log"/>
    <bean id="afterlLog" class="com.kero.log.AfterLog"/>
<!--    配置aop 需要导入aop的约束-->
    <aop:config>
<!--        切入点 expression表达式 expression(要执行的位置  )-->
        <aop:pointcut id="pointcut" expression="execution(* com.kero.service.UserServiceImpl.*(..))"/>
<!--        执行环绕增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterlLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

Log

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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的底层)

?
1
2
3
4
5
6
7
8
9
10
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();
    }
}

方式二:使用自定义类

配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--    注册bean-->
    <bean id="userService" class="com.kero.service.UserServiceImpl"/>
    <bean id="log" class="com.kero.log.Log"/>
    <bean id="afterlLog" class="com.kero.log.AfterLog"/>
    <bean id="diy" class="com.kero.diy.DiyPointCut"/>
    <aop:config>
<!--        自定义切面 ref要引用的类-->
        <aop:aspect ref="diy">
<!--            切入点-->
            <aop:pointcut id="point" expression="execution(* com.kero.service.UserServiceImpl.*(..))"/>
<!--            通知-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>
</beans>

DIY类

?
1
2
3
4
5
6
7
8
public class DiyPointCut {
    public void before(){
        System.out.println("````方法执行前````");
    }
    public void after(){
        System.out.println("````方法执行后````");
    }
}

其他的不变

方式三:使用注解实现

配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--    注册bean-->
    <bean id="userService" class="com.kero.service.UserServiceImpl"/>
    <bean id="log" class="com.kero.log.Log"/>
    <bean id="afterlLog" class="com.kero.log.AfterLog"/>
<!--    开启注解支持-->
    <aop:aspectj-autoproxy/>
    <bean id="annotationPointCut" class="com.kero.diy.AnnotationPointCut"/>
</beans>

自定义类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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接口/类的所有方法:

到此这篇关于Spring深入讲解实现AOP的三种方式的文章就介绍到这了,更多相关Spring AOP内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/TIANJIAWEN/article/details/124814797

延伸 · 阅读

精彩推荐