Spring AOP的注解式开发实现

AOP常用注解

注解名称
@Before前置通知
@After后置通知
@AfterThrowing异常通知
@AfterReturning最终通知
@Around环绕通知

注解开发实现步骤

1. 导入Maven项目依赖

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.12</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!-- 连接池-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.10</version>
    </dependency>
    <!-- mysql驱动包-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.0.2.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <!--AOP联盟-->
    <dependency>
        <groupId>aopalliance</groupId>
        <artifactId>aopalliance</artifactId>
        <version>1.0</version>
    </dependency>
    <!--Spring Aspects-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <!--aspectj-->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.3</version>
    </dependency>
</dependencies>

2. 准备一个实体类(先定义接口再实现)

package com.qcby.service;

public interface UserService {
    public void save();
}
package com.qcby.service.impl;

import com.qcby.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    public void save() {
//        int aa = 1/0;
        System.out.println("业务层:Hello World!");
    }
}

3. 定义切面类

package com.qcby.util;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyAspect {
    //自定义切面类 = 切入点(表达式) + 通知(增强代码)

    //通知
    @Before("execution(* com.qcby.*.*.*ServiceImpl.save*(..))")
    public void log1() {
        System.out.println("我是前置增强方法!");
    }

    @After("execution(* com.qcby.*.*.*ServiceImpl.save*(..))")
    public void log2() {
        System.out.println("我是后置增强方法!");
    }

    @AfterThrowing("execution(* com.qcby.*.*.*ServiceImpl.save*(..))")
    public void log3() {
        System.out.println("我是异常增强方法!");
    }

    @AfterReturning("execution(* com.qcby.*.*.*ServiceImpl.save*(..))")
    public void log4() {
        System.out.println("我是最终增强方法!");
    }

    //开启环绕通知
    //需要传入一个参数 spring提供的一个接口 ProceedingJoinPoint
    //这个接口中有个proceed方法,作用是让目标对象的方法调用
    @Around("execution(* com.qcby.*.*.*ServiceImpl.save*(..))")
    public void aroundLog(ProceedingJoinPoint point) {
        try {
            log1();
            point.proceed(); //目标对象方法手动调用
            log4(); //失败就不会调用的最终方法
        } catch (Throwable e) {
            log3();
            e.printStackTrace();
        } finally {
            log2(); //无论成功与否都会调用的后置方法
        }
    }
}

4. 准备配置文件

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

<!--    注解开发方式-->

<!--    开启注解扫描-->
    <context:component-scan base-package="com.qcby"/>
<!--    开启自动代理-->
    <aop:aspectj-autoproxy/>
</beans>

5. 编写测试类

import com.qcby.service.UserService;
import com.qcby.util.SpringConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class UserServiceTest {
    @Autowired
    private UserService userService;

    @Test
    public void run() {
        userService.save();
    }
}

注意事项

  1. 使用注解式开发还需要在spring的配置文件中开启自动代理:<aop:aspectj-autoproxy/>
  2. AOP纯注解式开发需要在配置类中多加一个注解@EnableAspectJAutoProxy开启自动代理
  • 32
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值