1、代理类接口Person.java

 package com.xiaostudy;

 /**
* @desc 被代理类接口
*
* @author xiaostudy
*
*/
public interface Person { public void add();
public void update();
public void delete();
}

2、代理类PersonImple.java

 package com.xiaostudy;

 import org.springframework.stereotype.Component;

 /**
* @desc 被代理类
*
* @author xiaostudy
*
*/
@Component("person")//类注解
public class PersonImple implements Person { /**
* @desc 实现接口方法
*/
public void add() {
System.out.println("add().....");
} @Override
public void update() {
System.out.println("update().....");
// int i = 1/0;
} @Override
public void delete() {
System.out.println("delete().....");
} }

3、通知类MyAspectJ.java

 package com.xiaostudy;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; /**
* @desc 通知类
*
* @author xiaostudy
*
*/
@Component//类注解
@Aspect//AspectJ注解
public class MyAspectJ { //声明公共切入点
@Pointcut("execution(* com.xiaostudy.PersonImple.*(..))")
public void myPointcut() { } //前置通知注解,只有一个参数时,value可以省略不写
@Before("execution(* com.xiaostudy.PersonImple.*(..))")
public void myBefort(JoinPoint joinPoint) {
System.out.println("前置通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName());
} //后置通知注解,当参数大于1时,value必须写
@AfterReturning(value="myPointcut()", returning="ret")
public void myAfterReturning(JoinPoint joinPoint, Object ret) {
System.out.println("后置通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName()
+ ", ret: " + ret);
} //环绕通知注解
@Around("myPointcut()")
public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕通知====前>>>>>>>>>>>");
Object obj = joinPoint.proceed();
System.out.println("环绕通知====后<<<<<<<<<<<");
return obj;
} //异常通知注解
@AfterThrowing(value="myPointcut()", throwing="e")
public void myThrowint(JoinPoint joinPoint, Throwable e) {
System.out.println("异常通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName()
+ ", e: " + e.getMessage());
System.exit(0);
} //最终通知注解
@After("myPointcut()")
public void myAfter(JoinPoint joinPoint) {
System.out.println("最终通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName());
}
}

4、spring配置文件applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描注解类 -->
<context:component-scan base-package="com.xiaostudy"></context:component-scan>
<!-- 确定 AOP注解生效 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

5、测试类Test.java

 package com.xiaostudy;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @desc 测试类
*
* @author xiaostudy
*
*/
public class Test { public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = ac.getBean("person", Person.class);
person.add();
person.update();
person.delete();
} }

spring通知的注解的更多相关文章

  1. spring aop 使用注解方式总结

    spring aop的注解方式:和xml的配置方式略有区别,详细如下: 1.首先还是建立需要的切面类:切面类里面定义好切点配置,以及所有的需要实现的通知方法. /** * */ package com ...

  2. 利用Spring AOP自定义注解解决日志和签名校验

    转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...

  3. Spring笔记04_AOP注解开发_模板_事务

    目录 1. Spring基于AspectJ的注解的AOP开发 1. 1 SpringAOP的注解入门 1.2 Spring的AOP的注解通知类型 1.2.1 @Before:前置通知 1.2.2 @A ...

  4. spring 、spring boot 常用注解

    @Profile 1.用户配置文件注解. 2.使用范围: @Configration 和 @Component 注解的类及其方法, 其中包括继承了 @Component 的注解: @Service. ...

  5. spring AOP自定义注解方式实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  6. Spring基于纯注解方式的使用

    经过上篇xml与注解混合方式,对注解有了简单额了解,上篇的配置方式极大地简化了xml中配置,但仍有部分配置在xml中进行,接下来我们就通过注解的方式将xml中的配置用注解的方式实现,并最终去掉xml配 ...

  7. spring AOP自定义注解 实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  8. Spring AOP的注解方式实现

    spring也支持注解方式实现AOP,相对于配置文件方式,注解配置更加的轻量级,配置.修改更加方便. 1.开启AOP的注解配置方式 <!-- 开启aop属性注解 --> <aop:a ...

  9. (转)利用Spring AOP自定义注解解决日志和签名校验

    一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: ...

随机推荐

  1. 自己根据java的LinkedList源码编写的一个简单的LinkedList实现

    自己实现了一个简单的LinkedList /** * Create by andy on 2018-07-03 11:44 * 根据 {@link java.util.LinkedList}源码 写了 ...

  2. 【BZOJ4665】小w的喜糖 容斥+组合数

    [BZOJ4665]小w的喜糖 Description 废话不多说,反正小w要发喜糖啦!! 小w一共买了n块喜糖,发给了n个人,每个喜糖有一个种类.这时,小w突发奇想,如果这n个人相互交换手中的糖,那 ...

  3. c#学习笔记之使用 TableLayoutPanel 控件设置窗体布局

    使用 TableLayoutPanel 控件设置窗体布局 在 Visual Studio IDE 左侧,找到“工具箱”选项卡. 选择“工具箱”选项卡,随即将显示工具箱.(或者,在菜单栏上,依次选择“视 ...

  4. 巨蟒python全栈开发-第20天 核能来袭-约束 异常处理 MD5 日志处理

    一.今日主要内容 1.类的约束(对下面人的代码进行限制;项目经理的必备技能,要想走的长远) (1)写一个父类,父类中的某个方法要抛出一个异常 NotImplementedError(重点) (2)抽象 ...

  5. HTTP 协议介绍

    HTTP 协议规定了浏览器和服务器之间互相通信的规则. 请求协议: 规定了客户端发送给服务器的内容格式 响应协议: 服务器发送给客户端的内容格式 请求协议 请求协议格式: 请求行 多个请求头信息(属性 ...

  6. Java 集合框架查阅技巧

    如何记录每一个容器的结构和所属体系呢? List ArrayList LinkedList Set HashSet TreeSet 其中,后缀名就是该集合所属的体系,前缀名就是该集合的数据结构. 看到 ...

  7. 洛谷 P4451 [国家集训队]整数的lqp拆分

    洛谷 这个题目是黑题,本来想打表的,但是表调不出来(我逊毙了)! 然后随便打了一个递推,凑出了样例, 竟然. 竟然.. 竟然... A了!!!!!!! 直接:\(f[i]=f[i-1]*2+f[i-2 ...

  8. Linux安装redis数据库及添加环境变量

    1.下载安装包 [root@localhost opt]# yum install wget [root@localhost opt]# wget http://download.redis.io/r ...

  9. 协程+IO切换实现并发

    from gevent import monkey # 以后代码中遇到IO都会自动执行greenlet的switch进行切换 monkey.patch_all() import requests im ...

  10. Android安装APK报错:Installation error: INSTALL_FAILED_UPDATE_INCOMPATIBLE解决方法

    今天调试一个android应用的时候,安装报了Installation error: INSTALL_FAILED_UPDATE_INCOMPATIBLE错误,代码如下: [2015-12-28 15 ...