当前位置: 首页 > news >正文

Spring AOP简单的配置(注解和xml配置)

注解配置AOP

  项目路径:E:\JavaWebSrc\FirstSpringAOP

  1:接口代码 

    接口为 IPerson ,接口不需要写注释

public interface IPerson {
    public void sayNmae();
    public void introduceOneSelf();
}

  2:实体类代码

     student继承了IPerson接口 

//将student实例化。装配到spring容器中
@Component("student") public class Student implements IPerson { @Value(value = "1") private int id; @Value(value = "余文辉") private String name; @Value(value = "") private String sex; @Override public void sayNmae() { System.out.println(this.name); } @Override public void introduceOneSelf() { System.out.println(this.toString()); } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", sex='" + sex + '\'' + '}'; } }

  3:切面类

    这里使用的注解需要导入(aopaliance.jar和  aspectjweaver.jar 两个jar包) 

//切面声明
@Aspect
//装配到spring容器中
@Component
public class LoggingAspect {
//    声明切入点  声明切入点表达式
    @Pointcut("execution(* com.yuwenhui.annotation.Student.* (..))")
    public void JoinPointExpecssion(){};
//前置通知
    @Before("JoinPointExpecssion()")
    public void beforMethod(JoinPoint joinPoint){
//        获得方法名
        String name = joinPoint.getSignature().getName();
//        获得参数列表,再将参数数组转换成List集合
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("前置通知");
        System.out.println("参数:"+args);

    }
//后置通知
    @After("JoinPointExpecssion()")
    public void afterMethod(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("后置通知");
        System.out.println("参数:"+args);
    }
//    返回通知,在方法正常结束的时候返回,且 必须要有一个返回值
    @AfterReturning(value = "JoinPointExpecssion()",returning = "result")
    public void afterMethodRetuning(JoinPoint joinPoint,Object result){
        String name = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("返回通知"+ result);
    }
//    异常通知 ,将在方法抛出异常时触
    @AfterThrowing(value = "JoinPointExpecssion()",throwing ="e")
    public void  afterThrowing(JoinPoint joinPoint,Exception e){
        String name = joinPoint.getSignature().getName();
        System.out.println("返回通知");
        System.out.println("异常信息"+e);
    }
}

  xml配置:

<?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.yuwenhui.annotation"></context:component-scan>
<!-- 声明Aspect配置 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

  测试类

public class TestAop {
    @Test
    public void testAop(){
        ApplicationContext applicationContext = new  ClassPathXmlApplicationContext("applicationContext.xml");
       IPerson student= (IPerson) applicationContext.getBean("student");
       student.sayNmae();
       student.introduceOneSelf();
    }
}

 

XML配置AOP

  1:接口配置

    不管是XML配置还是annotation配置,接口类都不需要改变

  

public interface IPerson {
    public void sayNmae();
    public void introduceOneSelf();
}

  2:实体类配置

    

public class Student implements IPerson {

    private int id;

    private String name;
    private String sex;

    @Override
    public void sayNmae() {
        System.out.println(this.name);
    }

    @Override
    public void introduceOneSelf() {
        System.out.println(this.toString());
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

  3:切面类

  

public class LoggingAspect {

    public void JoinPointExpecssion(){};

    public void beforMethod(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("前置通知");
        System.out.println("参数:"+args);

    }

    public void afterMethod(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("后置通知");
        System.out.println("参数:"+args);
    }

    public void afterMethodRetuning(JoinPoint joinPoint,Object result){
        String name = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("返回通知"+ result);
    }

    public void  afterThrowing(JoinPoint joinPoint,Exception e){
        String name = joinPoint.getSignature().getName();
        System.out.println("返回通知");
        System.out.println("异常信息"+e);
    }
}

  4:测试类

    

public class TestXml {
    @Test
    public void testAop(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-beanxml.xml");
        IPerson student= (IPerson) applicationContext.getBean("student");
        student.sayNmae();
        student.introduceOneSelf();
    }
}

   5:配置文件

    需要格外注意   

<?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-4.1.xsd">
    <!--  需要注意xml头,不是IDEA自动生成的   -->
    <!--  配置实体类,并为相关字段初始化  -->
    <bean id="student" class="com.yuwenhui.xml.Student">
        <property name="name" value="余文辉"></property>
        <property name="id" value="1"></property>
        <property name="sex" value=""></property>
    </bean>
    <!--  声明切面类,这里只是声明为普通的bean类  -->
    <bean id="loggingAspect" class="com.yuwenhui.xml.LoggingAspect"></bean>
    <!-- 配置aop   -->
    <aop:config>
        <!-- 配置切点表达式   -->
        <aop:pointcut id="pointcut" expression="execution(* com.yuwenhui.xml.Student.* (..))"/>
        <!-- 这里才是真正的声明切面   -->
        <aop:aspect ref="loggingAspect">
            <!--前置通知-->
            <aop:before method="beforMethod" pointcut-ref="pointcut"/>
            <!--后置通知-->
            <aop:after method="afterMethod" pointcut-ref="pointcut"/>
            <!--返回通知-->
            <aop:after-returning method="afterMethodRetuning" pointcut-ref="pointcut" returning="result"/>
            <!--异常通知-->
            <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
        </aop:aspect>
    </aop:config>
</beans>

 

转载于:https://www.cnblogs.com/yuwenhui/p/7519241.html

相关文章:

  • Swift,枚举
  • java操作Excel
  • 'NoneType' object is not iterable
  • AngularJS
  • C++ 清空队列(queue)的几种方法
  • MIME 类型(HttpContext.Response.ContentType)列表
  • 从微信官方获取微信公众号名片:https://open.weixin.qq.com/qr/code?username=haihongruanjian...
  • 分享 - 27 个机器学习、数学、Python 速查表
  • 浅析设计模式
  • 【BZOJ3331】[BeiJing2013]压力 Tarjan求点双
  • idea插件之——在markdown复制粘贴图片
  • Encourage_by_WeChat
  • 目标检测应用化之web页面(YOLO、SSD等)
  • 父类和子类(指针,对象,引用 ,盲点)
  • java NIO原理及实例
  • 【技术性】Search知识
  • 0x05 Python数据分析,Anaconda八斩刀
  • CSS居中完全指南——构建CSS居中决策树
  • JavaScript创建对象的四种方式
  • JavaScript的使用你知道几种?(上)
  • Laravel 实践之路: 数据库迁移与数据填充
  • Linux链接文件
  • MYSQL 的 IF 函数
  • Netty 4.1 源代码学习:线程模型
  • Node.js 新计划:使用 V8 snapshot 将启动速度提升 8 倍
  • Odoo domain写法及运用
  • React-Native - 收藏集 - 掘金
  • Sublime text 3 3103 注册码
  • 关键词挖掘技术哪家强(一)基于node.js技术开发一个关键字查询工具
  • 那些年我们用过的显示性能指标
  • 通过git安装npm私有模块
  • 突破自己的技术思维
  • 1.Ext JS 建立web开发工程
  • ​ssh-keyscan命令--Linux命令应用大词典729个命令解读
  • ​软考-高级-系统架构设计师教程(清华第2版)【第1章-绪论-思维导图】​
  • #QT(一种朴素的计算器实现方法)
  • #Spring-boot高级
  • #大学#套接字
  • (C语言)编写程序将一个4×4的数组进行顺时针旋转90度后输出。
  • (day6) 319. 灯泡开关
  • (HAL库版)freeRTOS移植STMF103
  • (Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (初研) Sentence-embedding fine-tune notebook
  • (动手学习深度学习)第13章 计算机视觉---微调
  • (附源码)python房屋租赁管理系统 毕业设计 745613
  • (附源码)springboot美食分享系统 毕业设计 612231
  • (六) ES6 新特性 —— 迭代器(iterator)
  • (三)uboot源码分析
  • (四) 虚拟摄像头vivi体验
  • (一)ClickHouse 中的 `MaterializedMySQL` 数据库引擎的使用方法、设置、特性和限制。
  • (译)2019年前端性能优化清单 — 下篇
  • (原創) 如何使用ISO C++讀寫BMP圖檔? (C/C++) (Image Processing)
  • (转)shell中括号的特殊用法 linux if多条件判断
  • .NET 6 Mysql Canal (CDC 增量同步,捕获变更数据) 案例版