备忘一下, 基本的用法包括在配置文件中配置pointcut, 在java中用编写实际的aspect 类, 针对对切入点进行相关的业务处理.
配置文件如下
<?xml version="1.0" encoding="GBK"?>
<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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
default-autowire="byName">
<!-- aop 拦截器配置 -->
<aop:aspectj-autoproxy />
<bean id="cachedDAO" class="com.mysoft.cache.CacheAspect" >
</bean>
<aop:config>
<aop:aspect ref="cachedDAO">
<aop:around pointcut="execution(* com.mysoft.dao.DAO.getById(long)) and args(Id)" method="getById" arg-names="join,Id"/>
</aop:aspect>
</aop:config>
</beans>
aspect java类如下:
public class CacheAspect {
public Object getSpuById(ProceedingJoinPoint join, long spuId) throws Throwable {
...
}
}
所有的就这么简单, 全部搞定, 在需要的地方将该bean配置文件引入即可.
第二种方式, 采用注解来做aop, 主要是将写在spring 配置文件中的连接点, 写到注解里面
spring aop配置文件:
<!-- aop 拦截器配置 -->
<aop:aspectj-autoproxy />
<bean id="mockAspect" class="com.taobao.item.mock.aspect.MockAspect" lazy-init="false">
</bean>
用来定义连接点和拦截处理的java类:
@Aspect
public class MockAspect {
@Pointcut("execution(* com.mysoft.manager.propertyManager.*(. .))")
public void propertyManager() throws Throwable {
}
@Pointcut("execution(* com.mysoft.manager.impl.TxtFileManager.*(. .))")
public void txtFileManager() {
}
@Pointcut("execution(* com.mysoft.manager.KeywordsChecker.checkNormalKeywords(. .))")
public void checkNormalKeywords() {
}
@Pointcut("execution(* com.mysoft.manager.KeywordsChecker.checkFixKeywords()) && args(text, ..)")
public void checkFixKeywords(String text) {
}
@Around("propertyManager() || txtFileManager()")
public Object invoke(ProceedingJoinPoint join) throws Throwable {
return null;
}
@Around("checkNormalKeywords()")
public String invokeAndReturnString(ProceedingJoinPoint join) throws
Throwable {
return "";
}
/**
* @param text
* @return
* @throws Throwable
*/
@Around(value = "checkFixKeywords(text)")
public String invokeCheckFixKeywords(ProceedingJoinPoint join, String
text)
throws Throwable {
if ("abcflg".equals(text)) {
return "flg";
}
return null;
}
这里的配置很容易写错, 故我个人还是倾向于在配置文件中写pointcut表达式.
分享到:
相关推荐
NULL 博文链接:https://zhang-yingjie-qq-com.iteye.com/blog/319927
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
spring aop demo 通过注解和配置文件的两种实现方式 包含jar包 下载直接运行
Spring AOP的实现机制中文版,动态代理及原理,自定义类加载器
使用Spring的注解方式实现AOP的细节
AOP的意思就是面向切面编程。本文主要是通过梳理JDK中自带的反射机制,实现 AOP动态代理模式,这也是Spring AOP 的实现原理
死磕Spring之AOP篇 - Spring AOP两种代理对象的拦截处理(csdn)————程序
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
spring aop jar 包
一个基于配置文件的Spring AOP的实现。实现了前置通知,后置通知,以及拦截器的功能,配置中有详细的注释。
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-...
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
springboot spring aop 拦截器 注解方式实现脱敏(涉及到:pom.xml -->application.properties --->启动类-->拦截器)
里面包括4个例子:(1)Spring实现AOP方式之一:基于XML配置的Spring AOP (2)Spring实现AOP方式之二:使用注解配置 Spring AOP (3)Spring AOP : AspectJ Pointcut 切点 (4)Spring AOP : Advice 声明 (通知注解)
java springAOP实现数据字典
SpringAop xml方式配置通知
2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑实现 (需要知道原理的请看spring aop源码,此处不做赘述) 3、可在现有源码上快速进行功能扩展 4、spring boot,mybatis,druid,spring aop的使用
Spring中实现AOP有很多方式,这里总结了四种,有基于代理的实现方式,也有基于AspectJ 的注解方式,还有基于AOP标签的配置
博客《理解Spring AOP实现与思想》案例代码,对Java技术感兴趣的朋友可以关注一下我,我在csdn为您准备了很多Java干货。
使用Spring的AOP技术,实现日志记录功能。记录如下信息: 在哪个时间访问了哪个方法,方法传入了哪些参数