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. dstat 监控命令详解

    一.工具介绍 dstat的man手册对于该工具的解释: dstat - versatile tool for generating system resource statistics 系统资源多用途 ...

  2. 091、Java中String类之使用“==”比较

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  3. Java中很少用的CopyOnWriteArrayList

    类注释 /** * A thread-safe variant of {@link java.util.ArrayList} in which all mutative * operations ({ ...

  4. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 图片:缩略图功能

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. [noip2014]P2312 解方程

    P2312 解方程 其实这道题就是求一个1元n次方程在区间[1, m]上的整数解. 我们枚举[1, m]上的所有整数,带进多项式中看看结果是不是0即可. 这里有一个技巧就是秦九韶算法,请读者自行查看学 ...

  6. office2019激活办法

    @echo off (cd /d "%~dp0")&&(NET FILE||(powershell start-process -FilePath '%0' -ve ...

  7. 免杀PHP一句话一枚

    免杀PHP一句话shell,利用随机异或免杀D盾,免杀安全狗护卫神等 <?php class VONE { function HALB() { $rlf = 'B' ^ "\x23&q ...

  8. Excel的查询函数vlookup和index使用

    需求 有一些省市的区县,有600多条数据,只有名称,没有编码.现在要根据名称去3000多条数据里面查询. 如图,拿出一部分数据来演示 vlookup 使用vlookup,由于vlookup只能查询数据 ...

  9. 012.Oracle数据库,字符串文本大小写转换,转大写,转小写,首字母大写

    /*转大写*/ SELECT UPPER(TITLE_EN) FROM ME_EO WHERE ( ISSUE_DATE BETWEEN to_date( '2017-02-04', 'yyyy-MM ...

  10. mysql数据库可视化工具—Navicat Premium—安装与激活

    一.Navicat premium简介 Navicat premium是一款数据库管理工具.将此工具连接数据库,你可以从中看到各种数据库的详细信息.包括报错,等等.当然,你也可以通过他,登陆数据库,进 ...