springAOP学习笔记

导入依赖

  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

目标类

public interface UserService {

    void findUserById();

    void deleteUserById();

}
public class UserServiceImpl implements UserService {

    @Override
    public void findUserById() {

        System.out.println("核心业务---> 查询用户" );

    }
    @Override
    public void deleteUserById() {

        System.out.println("核心业务---> 删除用户" );

    }
}

切面类

public class MyAspect {

    /**
     * 定义增强的方法:如果前后都要增强的方法
     * 那么就还使用环绕通知/增强
     */
    /**
     * 自定义环绕通知
     * @param joinPoint 连接点/切入点,即目标方法
     * @return 目标方法的返回值
     */
    public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
        // 目标方法前:
        System.out.println("开启事务/权限校验" );

        // 目标方法执行
        Object ret = joinPoint.proceed( );
        
        System.out.println("目标方法返回值---> " + ret );
        
        // 目标方法后:
        System.out.println("提交事务/日志记录" );
        return ret;
    }

}

配置(织入)

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

    <!-- 目标类,UserService -->
    <bean id="userService" class="com.qf.service.impl.UserServiceImpl"/>

    <!-- 切面类 -->
    <bean id="myAspect" class="com.qf.aspect.MyAspect"/>

    <!-- 织入 -->
    <aop:config>
        <!-- 确定切面-->
        <aop:aspect ref="myAspect">
            <!-- 1确定通知/增强类型 -->
            <!-- 2确定切面中增强的方法 -->
            <!-- 3确定目标类以及目标方法
               execution(返回值类型 包路径.类名.方法名(参数))
               返回值类型任意,*
               包路径,写com.qf
               类名,UserServiceImpl,如果是所有类,*
               方法名,findUserById,如果是所有方法,*
               () 参数列表
               (..) 代表任意参数
            -->
            <aop:around method="myAround"
                        pointcut="execution(* com.qf.service.impl.*.*(..))"/>
        </aop:aspect>
    </aop:config>
</beans>

2.注解开发AOP

在这里插入图片描述
1.业务层类加注解@Service, @Autowired

2.给切面类添加注解

@Component // 该注解,spring创建该类对象
@Aspect    // 该注解,该类是切面类
public class MyAspectAnno {

    /**
     * 定义一个切入点表达式公共方法
     */
    @Pointcut("execution(* com.qf.service.impl.*.*(..))")
    public void myPointcut(){}


    @Around("myPointcut()")
    public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
        // 目标方法前:
        System.out.println("开启事务/权限校验" );

        // 目标方法执行
        Object ret = joinPoint.proceed( );
        System.out.println("目标方法返回值---> " + ret );
        // 目标方法后:
        System.out.println("提交事务/日志记录" );
        return ret;
    }

    @Before("myPointcut()")
    public void myBefore(JoinPoint joinPoint) {
        // 目标对象
        Object target = joinPoint.getTarget( );
        System.out.println("target = " + target);
        // 获得目标方法签名(方法名)
        Signature signature = joinPoint.getSignature( );
        System.out.println("signature = " + signature);

        System.out.println("前置通知--->权限校验--->OK" );

        // 假设权限校验没有通过,通过抛出异常让代码停下,不再执行目标方法
        // System.out.println("前置通知--->权限校验--->ERROR" );
        // throw new RuntimeException("权限校验--->ERROR");
    }

    @After("myPointcut()")
    public void myAfter() {
        System.out.println("后置通知--->记录日志,释放资源" );
    }

    @AfterReturning(value = "myPointcut()",returning = "ret")
    public Object myAfterRet(Object ret) {
        System.out.println("后置返回通知,接收到目标方法返回值--->" + ret);
        // 可以对返回的数据进行处理
        if (ret instanceof Integer) {
            Integer value = (Integer) ret;
            value *= 3;
            // 【BUG,返回值没有返回给目标方法】
            return value;
        }
        return ret;
    }

    @AfterThrowing(value = "myPointcut()",throwing = "e")
    public void myException(Exception e) {
        System.out.println("目标方法报的错---> " + e.getMessage());
    }
}

配置文件扫描注解,让注解生效
applicationAnnoxml

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


    <!-- 扫描IOC,DI注解 -->
    <context:component-scan base-package="com.qf"/>

    <!-- AOP开启自动代理,注解生效 -->
    <aop:aspectj-autoproxy/>

</beans>

测试

    @Test
    public void testAspectByAnno() {
        String path = "applicationContextAnno.xml";
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(path);
        // 使用注解,对象名,默认是类名首字母小写
        UserService userService = context.getBean("userServiceImpl", UserService.class);
        userService.findUserById();

        System.out.println("-----------------" );

        userService.deleteUserById();

        System.out.println("-----------------" );
        HouseService houseService = context.getBean("houseServiceImpl", HouseService.class);
        int i = houseService.deleteHouse( );
        System.out.println("i = " + i);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想要入门的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值