spring通知的注解

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通知的注解的更多相关文章
- spring aop 使用注解方式总结
spring aop的注解方式:和xml的配置方式略有区别,详细如下: 1.首先还是建立需要的切面类:切面类里面定义好切点配置,以及所有的需要实现的通知方法. /** * */ package com ...
- 利用Spring AOP自定义注解解决日志和签名校验
转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...
- Spring笔记04_AOP注解开发_模板_事务
目录 1. Spring基于AspectJ的注解的AOP开发 1. 1 SpringAOP的注解入门 1.2 Spring的AOP的注解通知类型 1.2.1 @Before:前置通知 1.2.2 @A ...
- spring 、spring boot 常用注解
@Profile 1.用户配置文件注解. 2.使用范围: @Configration 和 @Component 注解的类及其方法, 其中包括继承了 @Component 的注解: @Service. ...
- spring AOP自定义注解方式实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
- Spring基于纯注解方式的使用
经过上篇xml与注解混合方式,对注解有了简单额了解,上篇的配置方式极大地简化了xml中配置,但仍有部分配置在xml中进行,接下来我们就通过注解的方式将xml中的配置用注解的方式实现,并最终去掉xml配 ...
- spring AOP自定义注解 实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
- Spring AOP的注解方式实现
spring也支持注解方式实现AOP,相对于配置文件方式,注解配置更加的轻量级,配置.修改更加方便. 1.开启AOP的注解配置方式 <!-- 开启aop属性注解 --> <aop:a ...
- (转)利用Spring AOP自定义注解解决日志和签名校验
一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: ...
随机推荐
- cdr X6 64位32位缩略图补丁包
cdr X6 64位32位缩略图补丁包下载 安装了X6没有缩略图的话,点击下面链接下载安装插件即可 点击下载
- Less-@import 导入选项
//@import 导入选项 --@import 可以至于任何你需要导入的地方 在标准的CSS,@import在规则必须先于所有其他类型的规则.但Less.js不关心 example: .test() ...
- SQL 2005 分页存储过程
-- ============================================= -- Description: <高效分页存储过程,适用于Sql2005以上> -- ...
- 流畅的python 闭包
闭包 人们有时会把闭包和匿名函数弄混.这是有历史原因的:在函数内部定义函数不常见,直到开始使用匿名函数才会这样做.而且,只有涉及嵌套函数时才有闭包问题.因此,很多人是同时知道这两个概念的.其实,闭包指 ...
- UTF-8具体解释
UTF-8是一种变长字节的编码方式.它以8位(1字节)为单位对Unicode进行编码. UTF-8理论上最多能够达到6字节长.但眼下全世界的字符仅仅须要4字节就能够表示完. UTF-8规定,对于某一字 ...
- 003-Java非堆CodeCache详解
一.概述 Java的内存由堆和非堆两个部分组成.对于堆来说,它的组成是比较确定的,它包含了年轻代和年老代两个部分,而年轻代又是由Eden区和两个Survivor区组成.可是,非堆由哪些部分组成呢? 在 ...
- 深入ff and ffbase
用ff 包读取一个csv 文件 >options(fftempdir = [二进制文件存放的位置]) >file_chunks <- read.csv.ffdf(file=”big_ ...
- iOS学习之数据持久化详解
前言 持久存储是一种非易失性存储,在重启设备时也不会丢失数据.Cocoa框架提供了几种数据持久化机制: 1)属性列表: 2)对象归档: 3)iOS的嵌入式关系数据库SQLite3: 4)Core Da ...
- Codeforces Round #302 (Div. 2)
A. Set of Strings 题意:能否把一个字符串划分为n段,且每段第一个字母都不相同? 思路:判断字符串中出现的字符种数,然后划分即可. #include<iostream> # ...
- Java集合(8):Hashtable
一.Hashtable介绍 和HashMap一样,Hashtable 也是一个散列表,它存储的内容是键值对(key-value)映射,它在很大程度上和HashMap的实现差不多. Hashtable ...