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):在需要签名校验的接口里写校验的代码,例如: ...
随机推荐
- Less-css基础扩展
//扩展Extend less的伪类,合并了选择器,放在与它引用匹配的选择器上 Use Method:以在study上扩展test的样式为例 .test{ color:#000000; font-si ...
- Virtual Private Cloud 专有网络 软件定义网络的方式 私有网络 大流量视频、直播类业务
私有网络 VPC_云上网络空间_自定义网络 - 腾讯云 https://cloud.tencent.com/product/vpc 私有网络 VPC 简介 私有网络(Virtual Private C ...
- runtime(二)
前言 上一篇中我们大致的了解了runtime的一些基本概念,这一篇我们一起来看看如何使用它. 3.如何使用runtime. 3.1 方法交换 举一个老生常谈的例子.当你接手一个新的项目,需要查看这个程 ...
- 异常处理、socke基于TCP协议编程
一.异常处理 1.错误和异常 1.程序中难免出现错误,而错误分成两种 (1)语法错误(这种错误过不了Python解释器的语法检测,必须在程序执行前改正) #语法错误示范一 if #语法错误示范二 de ...
- linux c编程:Posix信号量
POSIX信号量接口,意在解决XSI信号量接口的几个不足之处: POSIX信号量接口相比于XSI信号量接口,允许更高性能的实现. POSIX信号量接口简单易用:没有信号量集,其中一些接口模仿了我们熟悉 ...
- 0601-Zuul构建API Gateway-API gateway简介、基础使用、路由配置、负载配置
一.API Gateway简介 参看:http://www.cnblogs.com/bjlhx/p/8794437.html 二.zuul简介[路由器和过滤器:Zuul] 在微服务架构的组成部分进行路 ...
- 0505-Hystrix保护应用-Turbine集群状态监控
https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_turbine
- Android图片加载框架Picasso最全使用教程4
通过前几篇的学习,我们已经对Picasso的加载图片的用法有了很深的了解,接下来我们开始分析Picasso为我们提供的其他高级功能及内存分析,Let’sGo ! Picasso进行图片的旋转(Rota ...
- Redis三(List操作)
List操作 redis中的List在在内存中按照一个name对应一个List来存储.如图: lpush(name,values) 1 2 3 4 5 6 7 8 # 在name对应的list中添加元 ...
- NIO服务端和客户端通信demo
代码转自 https://www.jianshu.com/p/a9d030fec081 服务端: package nio; import java.io.IOException; import jav ...