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. Day10 - A - Rescue the Princess ZOJ - 4097

    Princess Cjb is caught by Heltion again! Her knights Little Sub and Little Potato are going to Helti ...

  2. eclipse中maven web项目部署时缺少classes文件或者resources文件

    写这篇博客的原因 问题描述 昨天发现eclipse中maven web项目部署时缺少classes文件或者resources文件 本来以为是很常见的原因, 依次检查"Java Build P ...

  3. qrcode在手机上不显示的问题

    可以试试以下解决方案: 1.修改qrcode.min.js:里的function n()红线区域替换成这个 , 原因是这样子才能支持安卓机显示.

  4. leetcode445 Add Two Numbers II

    """ You are given two non-empty linked lists representing two non-negative integers. ...

  5. ROS 命令行工具的使用

    1.roscore 打开一个新的master(master:进程),只能运行一个,运行两个会报错,使用ROS第一步就是要打开roscore 2.rosrun rosrun的使用格式一般为:rosrun ...

  6. NO24 第三关--企业面试题

    [考试目的] 1.学生课后复习及预习情况. 2.未来实际工作中做人做事能力. 3.沟通及口头表达能力. [口头表达技能考试题] 1.描述linux的开机到登陆界面的启动过程(记时2分钟) *****L ...

  7. 802.11X用户身份验证

    静态WEP企图同时解决802.11无线网络安全的两个问题.它即打算提供身份验证以限定拥有特定密钥方能进行网络访问,也想要提供机密性以在数据经过无线链路时予以加密.然而,它在这两方面的表现都不是特别好. ...

  8. Day6 - K - 陌上花开 HYSBZ - 3262

    有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),用三个整数表示. 现在要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量. 定义一朵花A比另一朵花B要美丽,当且仅Sa>= ...

  9. Day6 - C - Count HYSBZ - 1452 /1452: [JSOI2009]Count

    Description 一个N*M的方格,初始时每个格子有一个整数权值,接下来每次有2个操作: 改变一个格子的权值 求一个子矩阵中某个特定权值出现的个数   Input 每一行有两个数字N,M 接下来 ...

  10. 024、MySQL字符串替换函数,文本替换函数

    #文本替换 ,,'); #520ABCDEFG ,,'); #520BCDEFG ,,'); #520CDEFG ,,'); #A520BCDEFG ,,'); #A520CDEFG ,,'); #A ...