Spring的AOP

什么是AOP

AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各个部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

使用Spring实现AOP

[注意]:导入maven依赖,才能使用AOP,不然会报错

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.5</version>
        </dependency>

方式一:使用Spring的API接口

目录结构

第一步:创建接口

package com.cbbpp.service;

public interface UserService {

    public void add();
    public void delete();
    public void update();
    public void query();
}

第二步:创建实体类实现接口方法

package com.cbbpp.service;

public class UserServiceImpl implements UserService{
    public void add() {
        System.out.println("增加了用户");
    }
    public void delete() {
        System.out.println("删除了用户");
    }
    public void update() {
        System.out.println("更新了用户");
    }
    public void query() {
        System.out.println("查询了用户");
    }
}

第三步:

编写BeforeLog要执行的内容,实现MethodBeforeAdvice

package com.cbbpp.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {
    //method:要执行的目标对象的方法
    //args:参数
    //target:目标对象
    public void before(Method method, Object[] objects, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
    }
}

编写AfterLog实现AfterReturningAdvice

package com.cbbpp.log;

import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("执行了"+method.getName()+"方法,返回的结果为"+o);
    }
}

第四步:配置ioc,注入bean

<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--注册bean-->
    <bean id="userService" class="com.cbbpp.service.UserServiceImpl"/>
    <bean id="log" class="com.cbbpp.log.Log"/>
    <bean id="afterLog" class="com.cbbpp.log.AfterLog"/>

    <!--方式一:使用原生的Spring API接口-->
    <!--配置aop:需要再头文件中导入约束-->
    <aop:config>
        <!--pointcut:切入点  expression:表达式  execution(要执行的位置!* * * * * )-->
        <aop:pointcut id="pointcut" expression="execution(* com.cbbpp.service.UserServiceImpl.*(..))"/>
    <!--执行环绕增强-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>

</beans>

第五步:测试

import com.cbbpp.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理的是一个接口,不能是实现类
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}
//结果
com.cbbpp.service.UserServiceImpl的add被执行了
增加了用户
执行了add方法,返回的结果为null

Process finished with exit code 0

方式二:自定义类来实现AOP

在方式一的基础上新创建一个diyPointCut类

package com.cbbpp.diy;

public class diyPointCut {

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

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

 自定义切入的主配置文件的配置:

    <bean id="diypointcut" class="com.cbbpp.diy.diyPointCut"/>
    <aop:config>
        <!--自定义切面,ref为要引入的类-->
        <aop:aspect ref="diypointcut">
        <!--切入点-->
            <aop:pointcut id="point" expression="execution(* com.cbbpp.service.UserServiceImpl.*(..))"/>
        <!--通知-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>

方式三:注解实现

实体类中用注解

package com.cbbpp.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 AnnocationCut {

    @Before("execution(* com.cbbpp.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("执行前111");
    }
    @After("execution(* com.cbbpp.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("执行后222");
    }
    //在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
    @Around("execution(* com.cbbpp.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("环绕前222");
        Signature signature = jp.getSignature();
        System.out.println(signature);
        Object proceed = jp.proceed();
        System.out.println("环绕后222");
    }

}

 主配置文件中注入bean和开启注解aspect

    <!--方式三:注解实现-->
    <bean id="annotationcut" class="com.cbbpp.diy.AnnocationCut"/>
    <!--开启注解支持!   默认是JDK(proxy-target-class="false")(可不写)            cglib( proxy-target-class="true")    -->
    <aop:aspectj-autoproxy proxy-target-class="false"/>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值