1. 导入jar包

/SpringAOPmy/lib/com.springsource.net.sf.cglib-2.2.0.jar
/SpringAOPmy/lib/com.springsource.org.aopalliance-1.0.0.jar
/SpringAOPmy/lib/com.springsource.org.aspectj.tools-1.6.6.RELEASE.jar
/SpringAOPmy/lib/commons-logging.jar
/SpringAOPmy/lib/junit-4.8.2.jar
/SpringAOPmy/lib/spring-aop-4.1.0.RELEASE.jar
/SpringAOPmy/lib/spring-aspects-4.1.0.RELEASE.jar
/SpringAOPmy/lib/spring-beans-4.1.0.RELEASE.jar
/SpringAOPmy/lib/spring-context-4.1.0.RELEASE.jar
/SpringAOPmy/lib/spring-core-4.1.0.RELEASE.jar
/SpringAOPmy/lib/spring-expression-4.1.0.RELEASE.jar

2.配置文件

<?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.aspect.annonaction"></context:component-scan>
      <!--Spring 中使用aspectj 包中的@Aspect 注解标注的当前组件为切面
       -->
      <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

3. 注解切面方法

package com.aspect.annonaction;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

import org.springframework.stereotype.Component;

@Component
@Aspect
public class AOP {
    @Before("execution(* com.aspect.annonaction.Testss.say())")
    public void before() {
        System.out.println("前置通知");
    }

    @AfterReturning(pointcut="within(com.aspect.annonaction.Testss)",returning="obj")
    public void afterreturning(JoinPoint jp,Object obj) {
        System.out.println(jp.getSignature());
        System.out.println("返回值的类型:"+obj);
    }

    @AfterThrowing(pointcut="execution(* com.aspect.annonaction.Testss.say())",throwing="e")
    public void exception(Throwable e) {
        System.out.println("异常通知:"+e.getMessage());
    }

    @Around(value="within(com.aspect.annonaction.Testss)")
    public void around(ProceedingJoinPoint pjp) {
        System.out.println("前置通知");
        try {
            pjp.proceed();
            System.out.println("后置通知");
        } catch (Throwable e) {
            System.out.println("异常通知");
            e.printStackTrace();
        }
        System.out.println("最终通知");

    }

}

4. 测试

package com.aspect.annonaction;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class Testss {

    public void say() {
        System.out.println("张韶涵");
    }

    @Test
    public void test1 () {
        ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext2.xml");
        Testss t=(Testss) ac.getBean("testss");
        System.out.println(t);

        t.say();

    }

}

end

SpringAOP 使用注解的简单使用的更多相关文章

  1. Spring AOP注解形式简单实现

    实现步骤: 1:导入类扫描的注解解析器 命名空间:xmlns:context="http://www.springframework.org/schema/context" xsi ...

  2. spring注解开发中常用注解以及简单配置

    一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...

  3. Java注解--实现简单读取excel

    实现工具类 利用注解实现简单的excel数据读取,利用注解对类的属性和excel中的表头映射,使用Apache的poi就不用在业务代码中涉及row,rows这些属性了. 定义注解: @Retentio ...

  4. java基础强化——深入理解java注解(附简单ORM功能实现)

    目录 1.什么是注解 2. 注解的结构以及如何在运行时读取注解 2.1 注解的组成 2.2 注解的类层级结构 2.3 如何在运行时获得注解信息 3.几种元注解介绍 3.1 @Retention 3.2 ...

  5. Springboot使用自定义注解实现简单参数加密解密(注解+HandlerMethodArgumentResolver)

    前言 我黄汉三又回来了,快半年没更新博客了,这半年来的经历实属不易,疫情当头,本人实习的公司没有跟员工共患难, 直接辞掉了很多人.作为一个实习生,本人也被无情开除了.所以本人又得重新准备找工作了. 算 ...

  6. SpringAOP+注解实现简单的日志管理

    今天在再次深入学习SpringAOP之后想着基于注解的AOP实现日志功能,在面试过程中我们也经常会被问到:假如项目已经上线,如何增加一套日志功能?我们会说使用AOP,AOP也符合开闭原则:对代码的修改 ...

  7. hibernate注解的简单应用

    注解代替了我们用的*.hbm.xml文件.简少了我们的代码量:应用简单. @Override 用途:重写父类的同名方法 单元测试注解 @Test 用途:用于测试 @Before 用途:单测方法走之前执 ...

  8. 关于spring的IOC和DI的xml以及注解的简单介绍

    xml 一 目的:通过ApplicationContext对象的getBean方法获取所需类的对象. 编写一个service类 public class service { private Strin ...

  9. Spring-AOP 基于注解的实现

    一.AOP: 是对OOP编程方式的一种补充.翻译过来为“面向切面编程”. 可以理解为一个拦截器框架,但是这个拦截器会非常武断,如果它拦截一个类,那么它就会拦截这个类中的所有方法.如对一个目标列的代理, ...

随机推荐

  1. [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)

    [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...

  2. CBC加密原理及攻击

    原理基于分组加密加密过程 Plaintext:明文,待加密的数据.IV:用于随机化加密的比特块,保证即使对相同明文多次加密,也可以得到不同的密文,初始向量,用来与第一块的明文异或运算.Key:被一些如 ...

  3. 【LOJ2542】「PKUWC2018」随机游走

    题意 给定一棵 \(n\) 个结点的树,你从点 \(x\) 出发,每次等概率随机选择一条与所在点相邻的边走过去. 有 \(Q\) 次询问,每次询问给定一个集合 \(S\),求如果从 \(x\) 出发一 ...

  4. 从零到Django大牛的的进阶之路02

    Cookie/Session Cookie Cookie以键值对的格式进行信息的存储. Cookie基于域名安全,不同域名的Cookie是不能互相访问的,如访问itcast.cn时向浏览器中写了Coo ...

  5. firewalld学习--维护命令

    启动 systemctl start firewalld 停止 systemctl stop firewalld 重启 systemctl restart firewalld 查询状态 systemc ...

  6. NFS PersistentVolume【转】

    上一节我们介绍了 PV 和 PVC,本节通过 NFS 实践. 作为准备工作,我们已经在 k8s-master 节点上搭建了一个 NFS 服务器,目录为 /nfsdata: 下面创建一个 PV mypv ...

  7. 洛谷 P5509 派遣

    题目传送门 心路历程: 每想到一种思路,就有一种要做出来的感觉.但一接着想就会发现这种方法有一些极小的问题,但是我没法解决... 于是就再换思路... 最后在请教了出题人神仙zcq之后,终于做出来了 ...

  8. 0109 springboot的部署测试监控

    springboot的部署测试监控 部署 基于maven 打包 JAR 打包方式一般采用的jar包,使用springboot的默认方式即可: 使用maven命令: mvn clean package ...

  9. Quartus设计FIR滤波器的系数文件格式(适用于FIR II的IP核)

    对常用的FIR,我们使用MATLAB的fdatool(或者filterDesigner) 设计滤波器,给定指标,生成系数.为了方便,我们将系数保存到文件,其保存格式比较简介,在此进行说明. 1.FIR ...

  10. c# copydata 消息

    using PublicCode; using System; using System.Collections.Generic; using System.ComponentModel; using ...