1 引入aop命名空间
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
2配置,打开对@Aspect注解的支持
<aop:aspectj-autoproxy/>
3 引入 jar包
cglib-nodep-2.1_3.jar
aspectjweaver.jar
aspectjrt.jar
例子:
<?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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy/>
<bean id="myInterceptor" class="cn.service.MyInterceptor"/> 这是AOP切面,在业务方法执行时插入其它方法 ,也可使用第6讲中的"自动扫描" 就不用在这里配置了,但要在MyInterceptor 类加上@Conponent
<bean id="personService" class="cn.service.impl.PersonServiceBean"></bean> 这是业务类
</beans>
package cn.service;
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;
/**
* 切面
*
*/
@Aspect
public class MyInterceptor {
@Pointcut("execution (* cn.service.impl.PersonServiceBean.*(..))") //目标是PersonServiceBean类的所有方法
private void anyMethod() {}//声明一个切入点
@Before("anyMethod() && args(name)")
public void doAccessCheck(String name) {
System.out.println("前置通知:"+ name);
}
@AfterReturning(pointcut="anyMethod()",returning="result")
public void doAfterReturning(String result) {
System.out.println("后置通知:"+ result);
}
@After("anyMethod()")
public void doAfter() {
System.out.println("最终通知");
}
@AfterThrowing(pointcut="anyMethod()",throwing="e")
public void doAfterThrowing(Exception e) {
System.out.println("例外通知:"+ e);
}
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
//if(){//判断用户是否在权限
System.out.println("进入方法");
Object result = pjp.proceed();
System.out.println("退出方法");
//}
return result;
}
}
execution (* cn.service.impl.PersonServiceBean.*(..))
任何返回类型 包名.类名.任何方法(任意参数)
execution (* cn.service.impl..*.*(..))
任何返回类型 cn.service.impl 及子包 任意类.任意方法(任意参数) . impl.. 后有两点,表示本包及子包下面, 没有两点, 表示本包下面.
分享到:
相关推荐
使用Spring的注解方式实现AOP的细节
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
Spring通过注解实现AOP,Spring通过注解实现AOP,Spring通过注解实现AOPSpring通过注解实现AOP,Spring通过注解实现AOP,Spring通过注解实现AOP
注解方式实现AOP编程
在AOP注解中一共分为五中类型的通知注解,接下来,我们通过实例,对这五种AOP注解进行讲解
在SpringBoot中演示实现了aop的使用方法,包括pointcut和Advice
Spring 使用AspectJ 实现 AOP(基于xml文件、基于注解)
NULL 博文链接:https://88548886.iteye.com/blog/1571102
spring注解方式实现aop,希望对大家有所帮助,内附注释
AOP面向切面编程(Aspect Oriented Programming),是通过预编译方式和运行期动态代理实现程序功能的统一...下面这篇文章主要给大家介绍了关于利用C#实现AOP常见的几种方法,需要的朋友可以参考借鉴,下面来一起看看吧。
基于Spring boot + maven,以注解+AOP方式实现的java后端项目接口参数校验框架。迄今为止使用最简单、最容易理解的参数校验方案。博客地址:https://blog.csdn.net/weixin_42686388/article/details/104009771
使用Spring的注解方式实现AOP入门
http://blog.csdn.net/shan9liang/article/details/22295841 EJB+Annotation实现AOP的DEMO
NULL 博文链接:https://huangminwen.iteye.com/blog/1486873
2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑实现 (需要知道原理的请看spring aop源码,此处不做赘述) 3、可在现有源码上快速进行功能扩展 4、spring boot,mybatis,druid,spring aop的使用
NULL 博文链接:https://sunbin.iteye.com/blog/2373014
springboot spring aop 拦截器 注解方式实现脱敏(涉及到:pom.xml -->application.properties --->启动类-->拦截器)
Spring中实现AOP有很多方式,这里总结了四种,有基于代理的实现方式,也有基于AspectJ 的注解方式,还有基于AOP标签的配置
使用SpringBoot通过自定义注解+AOP+全局异常处理实现参数统一非空校验