长生的梦呓


  • 归档
  • 算法
  • 基础
  • 关于

  • 分类
  • 日志
  • Servlet
  • Archive
  • 数据结构
  • IO 流

  • 标签
  • 友链
  • MyBatis
  • About
  • Spring 5
  • Java SE
  • Java EE
  • Algorithms
  • 新特性
  • 位运算技巧

  • 搜索
内网穿透 项目实战 数据库 MySQL 安卓踩坑 开发工具 设计模式 Enum 枚举 Linux MyBatis-plus JSON IDEA Transactions AOP IO 流 DP IoC 与 DI 位运算技巧 工具类 学习技巧 Git JDK 排序 Spring Boot Spring MVC Spring Framework MyBatis Log4J Regex Jsoup JDBC 数据结构 递推 递归 算法 Servlet与JSP 小难 中等 简单

【Spring】(9.2)AOP - 实现方式二:注解

发表于 2020-04-09 | 1 | 阅读次数 288

一、注解实现切面

项目结构

在这里插入图片描述
接口

/** 用户数据访问层接口 */
public interface UserDao {
    void ins();
    void del();
    void upd();
    void sel();
}

实现类

public class UserDaoImpl implements UserDao {
    @Override
    public void ins() {
        System.out.println("insert命令");
    }

    @Override
    public void del() {
        System.out.println("delete命令");
    }

    @Override
    public void upd() {
        System.out.println("update命令");
    }

    @Override
    public void sel() {
        System.out.println("select命令");
    }
}

==使用注解自定义切面==

@Aspect
public class AnnoPointCut {
    @Before("execution(* com.shengjava.aop.dao.impl.UserDaoImpl.*(..))")
    public void before() {
        System.out.println("----- before -----");
    }

    @After("execution(* com.shengjava.aop.dao.impl.UserDaoImpl.*(..))")
    public void after () {
        System.out.println("----- after -----");
    }

    /** 在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点 */
    @Around("execution(* com.shengjava.aop.dao.impl.UserDaoImpl.*(..))")
    public void around (ProceedingJoinPoint jp) throws Throwable {
        System.out.println("----- around before -----");
        // 执行(该类还可以获取到一些类的其他信息)
        jp.proceed();
        System.out.println("----- around after -----");
    }
}

配置文件

<?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
       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="userDaoImpl" class="com.shengjava.aop.dao.impl.UserDaoImpl"/>
    <bean id="myPointCut" class="com.shengjava.aop.log.AnnoPointCut"/>

    <!-- 方式三:使用注解(请看AnnoPointCut类中的注解) -->
    <!-- 开启注解支持 (属性proxy-target-class值:默认"false"使用JDK动态代理,"true"使用cglib动态代理)-->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

测试类

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 动态代理(代理的是接口,所以类型应该也是接口类型)
        UserDao ud = context.getBean("userDaoImpl", UserDao.class);
        // 执行
        ud.ins();
    }
}

输出

----- around before -----
----- before -----
insert命令
----- around after -----
----- after -----

扩展

aop设置开启注解支持时,可以设置属性proxy-target-class值:默认"false"使用JDK动态代理),"true"使用cglib动态代理)-->

    <aop:aspectj-autoproxy proxy-target-class="true"/>
  • 本文作者: 长生的梦呓
  • 本文链接: https://shengjava.com/archives/spring92aop-实现方式二注解
  • 版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0 许可协议。转载请注明出处!
# 内网穿透 # 项目实战 # 数据库 # MySQL # 安卓踩坑 # 开发工具 # 设计模式 # Enum # 枚举 # Linux # MyBatis-plus # JSON # IDEA # Transactions # AOP # IO 流 # DP # IoC 与 DI # 位运算技巧 # 工具类 # 学习技巧 # Git # JDK # 排序 # Spring Boot # Spring MVC # Spring Framework # MyBatis # Log4J # Regex # Jsoup # JDBC # 数据结构 # 递推 # 递归 # 算法 # Servlet与JSP # 小难 # 中等 # 简单
【Spring】(9.1)AOP - 实现方式一:使用Spring API接口
【Spring】(9.3)AOP - 实现方式三:自定义切面
  • 文章目录
  • 站点概览
长生的梦呓

长生的梦呓

110 日志
39 分类
40 标签
RSS
E-mail CSDN
Creative Commons
Links
  • CSDN 地址
  • waltz26
  • Ryan Wang's Blog
  • JohnNiang's Blog
  • 廖雪峰
  • 菜鸟教程
© 2021 长生的梦呓
浙ICP备20005262号-1