spring要实现aop需要:aspectjweaver.jar , aspectjrt.jar , cglib-nodep.jar
实现注解:common-annotations.jar
用注解方式实现的切面类:
package com.fsj.aop;
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;
/**
* @Aspect 定义为切面
* @Component 自动扫描的组件,把此类交给spring管理
*
*/
@Component @Aspect
public class MyIntercepor {
/**
* @Pointcut 定义为切入点,切入点的名称即为方法名(包含括号)
* execution(* com.fsj..*.*(..))说明:
* 第一个“*”表示所有返回值类型,
* fsj后面的两个点表示包含子包,
* 第二个“*”表示所有类,
* 第三个“*”表示所有方法
* 括号中的两点表示任意个参数任意参数类型
*
@Pointcut("execution(* com.fsj..*.*(..))")
public void anyMethod()
{
}
*/
/**
* 只拦截com.fsj.service.impl.PersonServiceBean中的所有方法
*/
@Pointcut("execution(* com.fsj.service.impl.PersonServiceBean.*(..))")
public void anyMethod()
{
}
/**
* 定义前置通知
* 括号中为切入点的名称
*/
@Before("anyMethod()")
public void doAccessCheck()
{
System.out.println("-------1.前置通知--------");
}
/**
* 定义后置通知
* 括号中为切入点的名称
*/
@AfterReturning("anyMethod()")
public void doAfterReturning()
{
System.out.println("-------5.后置通知--------");
}
/**
* 定义最终通知
* 括号中为切入点的名称
*/
@After("anyMethod()")
public void doAfter()
{
System.out.println("-------3.最终通知--------");
}
/**
* 定义最终通知
* 括号中为切入点的名称
*/
@AfterThrowing("anyMethod()")
public void doAfterThrowing()
{
//异常通知在最终通知后执行
//有异常时不会执行后置通知,也不会有环绕后通知
System.out.println("-------异常通知--------");
}
/**
* 定义环绕通知
* 括号中为切入点的名称
* 环绕通知特别适合做权限控制
*/
@Around("anyMethod()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable
{
Object result = null;
System.out.println("-------2.环绕前通知:进入方法--------");
result = pjp.proceed();
System.out.println("-------4.环绕后通知:退出方法--------");
return result;
}
}
配置文件是:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<aop:aspectj-autoproxy /><!-- 启用aop -->
<context:component-scan base-package="com.fsj" /><!-- 启用自动扫描和管理bean -->
<!--
<bean id="myIntercepor" class="com.fsj.aop.MyIntercepor"></bean>
<aop:config>
<aop:aspect id="asp" ref="myIntercepor">
<aop:pointcut id="mycut" expression="execution(* com.fsj.service.impl.PersonServiceBean.*(..))"/>
<aop:before method="doAccessCheck" pointcut-ref="mycut" />
<aop:after-returning method="doAfterReturning" pointcut-ref="mycut"/>
<aop:after-throwing method="doAfterThrowing" pointcut-ref="mycut" />
<aop:after method="doAfter" pointcut-ref="mycut" />
<aop:around method="doAround" pointcut-ref="mycut" />
</aop:aspect>
</aop:config>
xml形式做aop
-->
</beans>
还可以得到拦截的参数和返回值:
/**
* 只拦截com.fsj.dao.impl.PersonDaoImpl中的所有方法
*/
@Pointcut("execution(* com.fsj.dao.impl.PersonDaoImpl.*(..))")
public void anyMethod()
{
}
/**
* 拦截anyMethod()指定的方法并且要满足只有一个int类型的参数的方法
* 得到输入参数
* @param id
*/
@Before("anyMethod() && args(id)")
public void doAccessCheck(int id)
{
System.out.println("-------前置通知--------" + id);
}
/**
* 拦截anyMethod()指定的方法
* 并且要满足只有一个String类型的参数和一个boolean类型的返回值的方法
* @param name 输入参数
* @param result 返回值
*/
@AfterReturning(pointcut="anyMethod() && args(name)",returning="result")
public void doAfterReturning(String name,boolean result)
{
//有异常时不会执行后置通知
System.out.println("-------后置通知--------name:"+name+"----------返回值:"+result);
}
@After("anyMethod()")
public void doAfter()
{
System.out.println("-------最终通知--------");
}
/**
* 拦截异常并获得异常信息
* @param e
*/
@AfterThrowing(pointcut="anyMethod()",throwing="e")
public void doAfterThrowing(Exception e)
{
System.out.println("-------异常通知--------"+e);
}
或者把连接点作为参数传入通知:
@Aspect
public class TestAspect {
@Pointcut("execution(* *(..))")
private void pointCutMethod(){};
@After("pointCutMethod()")
// @After("execution(* *(String))")
public void doAfter(JoinPoint jp) {
System.out.print("日志记录(方法执行后):\t"
+ "类名:" + jp.getTarget().getClass().getName() + "方法名:"
+ jp.getSignature().getName() + "\t");
Object[] args = jp.getArgs();
System.out.print("参数:" );
for(Object obj : args){
System.out.print(" " + obj + " ");
}
System.out.println();
}
@Around("execution(* *(..))")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long time = System.currentTimeMillis();
Object retVal = pjp.proceed();
time = System.currentTimeMillis() - time;
System.out.println("方法执行时间: " + time + " ms");
return retVal;
}
@Before("execution(* *(..))")
public void doBefore(JoinPoint jp) {
System.out.println("日志记录(方法执行之前): "
+ jp.getTarget().getClass().getName() + "."
+ jp.getSignature().getName());
}
@AfterThrowing(pointcut="pointCutMethod()" , throwing="e")
public void doThrowing(JoinPoint jp, Throwable e) {
System.out.println("method " + jp.getTarget().getClass().getName()
+ "." + jp.getSignature().getName() + " throw exception");
System.out.println(e.getMessage());
this.sendEx(e.getMessage());
}
private void sendEx(String ex) {
//TODO 发送短信或邮件提醒
if(ex != null)
System.out.println("向管理员发送警告");
}
分享到:
相关推荐
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...
赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...
3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 4、想看spring aop 注解实现记录系统日志并入库等 二、能学到什么 1、收获可用源码 2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑...
使用 Spring AOP 进行方法耗时监测的好处有以下几点: 1. 代码实现简单,易于维护:使用 Spring AOP 可以将耗时监测的逻辑与业务逻辑进行解耦,避免业务逻辑代码的冗余和代码维护难度的提高。 2. 安全性高:使用 ...
开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具...
AOP的概念也很好理解,跟中间件差不多,说白了,就是我可以任意地在方法的前面或后面添加代码,这很适合用于缓存、日志等处理。 在net core2.2时,我当时就尝试过用autofac实现aop,但这次我不想用autofac,我用了一...
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
内容概要:springboot+拦截器+aop+自定义注解+本地线程实现统一接口日志记录,记录下接口所在模块、接口描述、接口请求参数、接口返回参数、接口请求时间以及接口耗时用于接口优化,接口记录参数以及操作人防止使用...
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
java项目中 AOP事务所需要用的jar包....................................啊..........................................................
赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
aop所依赖的所有包+文档+源码,最新版全套aop aspectjweaver aopalliance aspects aspectjrt
AOP的意思就是面向切面编程。本文主要是通过梳理JDK中自带的反射机制,实现 AOP动态代理模式,这也是Spring AOP 的实现原理
spring aop jar 包
在使用spring的aop功能时,这两个jar是必须的,否则会报错,如下: Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException at java.net....
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。Spring AOP:通过配置管理特性,Spring AOP 模块直接将面向方面的编程功能集成到了 Spring 框架中。所以,可以很容易地使 Spring 框架管理的任何对象...
AOP in .NET: Practical Aspect-Oriented Programming 296 pages Publisher: Manning Publications; Pap/Psc edition (June 25, 2013) Language: English ISBN-10: 1617291145 ISBN-13: 978-1617291142 ...
SpringBoot下的Spring——DAY04——动态代理总结、AOP、自定义注解进行拦截、动态获取注解参数、通知方法 1.动态代理总结 1.1 JDK动态代理特点 1.2 CGlib动态代理 1.2.1 CGLib特点说明 1.3 动态代理的作用 2 Spring...