- 浏览: 982091 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (826)
- 硬件 (8)
- 软件 (24)
- 软件工程 (34)
- JAVA (229)
- C/C++/C# (77)
- JavaScript (8)
- PHP (1)
- Ruby (3)
- MySQL (14)
- 数据库 (19)
- 心情记事 (12)
- 团队管理 (19)
- Hadoop (1)
- spring (22)
- mybatis(ibatis) (7)
- tomcat (16)
- velocity (0)
- 系统架构 (6)
- JMX (8)
- proxool (1)
- 开发工具 (16)
- python (10)
- JVM (27)
- servlet (5)
- JMS (26)
- ant (2)
- 设计模式 (5)
- 智力题 (2)
- 面试题收集 (1)
- 孙子兵法 (16)
- 测试 (1)
- 数据结构 (7)
- 算法 (22)
- Android (11)
- 汽车驾驶 (1)
- lucene (1)
- memcache (12)
- 技术架构 (7)
- OTP-Erlang (7)
- memcached (17)
- redis (20)
- 浏览器插件 (3)
- sqlite (3)
- Heritrix (9)
- Java线程 (1)
- scala (0)
- Mina (6)
- 汇编 (2)
- Netty (15)
- libevent (0)
- CentOS (12)
- mongod (5)
- mac os (0)
最新评论
-
kingasdfg:
你这里面存在一个错误添加多个任务 应该是这样的 /** * ...
Quartz的任务的临时启动和暂停和恢复【转】 -
kyzeng:
纠正一个错误,long型对应的符号是J,不是L。
Jni中C++和Java的参数传递 -
zhaohaolin:
抱歉,兄弟,只是留下作记录,方便学习,如果觉得资料不好,可以到 ...
netty的个人使用心得【转】 -
cccoooccooco:
谢谢!自己一直以为虚机得使用网线才可以与主机连接呢。。
主机网卡无网线连接与虚拟机通信 -
yuqilin001:
要转别人的东西,请转清楚点嘛,少了这么多类,误人子弟
netty的个人使用心得【转】
Spring Aop实现——Annotation方式(注解式)
一、spring依赖库
* SPRING_HOME/dist/spring.jar
* SPRING_HOME/lib/jakarta-commons/commons-logging.jar
* SPRING_HOME/lib/log4j/log4j-1.2.14.jar
* SPRING_HOME/lib/aspect
j/*.jar
二、编写切面aspect类
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
* 定义切面
* @author
*
*/
@Aspect
public class CheckAspect {
/**
* 定义切入点(Pointcut),Pointcut的名称就是allSaveMethod, 此方法不能有参数和返回值,仅是个标识。
* Pointcut的内容——"execution(*, save*(..))", 是个表达式, 描述哪些对象的哪些方法(订阅Joinpoint)
*
*/
@Pointcut("execution(* save*(..)) || execution(* del*(..))")
private void allSaveMethod(){};
/*
* 定义通知advice(before型),
标识在哪个切入点(allSaveMethod),织入(weaver)此方法
*/
@Before("allSaveMethod()")
public void checkUser(){
System.out.println("=======CheckAspect.checkUser()===========");
}
}
测试服务类:
public class UserManagerImpl implements IUserManager {
public void delUser(int id) {
System.out.println("=====UserManagerImpl.delUser()===========");
}
public String findUser(int id) {
System.out.println("=====UserManagerImpl.findUser()===========");
return null;
}
public void saveUser(String username, String password) {
System.out.println("=====UserManagerImpl.saveUser()===========");
}
public void updateUser(int id) {
System.out.println("=====UserManagerImpl.updateUser()===========");
}
}
三、applicationContext.xml中开启aop,配置相关切面aspect类
<?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
"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
"
>
<!-- 启用aop -->
<aop:aspectj-autoproxy/>
<!--配置aspect-->
<bean id="checkAspect" class="com.CheckAspect" />
<bean id="userManager" class="com.manager.impl.UserManagerImpl" />
</beans>
四、测试用例:
public void testAspect(){
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserManager userManager = (IUserManager) factory.getBean("userManager");//生成的代理类proxy
userManager.saveUser("cat", "123");
userManager.delUser(1);
userManager.updateUser(1); //没有调用切面advice,因为方法update与之不匹配。
}
执行结果:
=======CheckAspect.checkUser()===========
=====UserManagerImpl.saveUser()===========
=======CheckAspect.checkUser()===========
=====UserManagerImpl.delUser()===========
=====UserManagerImpl.updateUser()===========
=======================================
Schema-based式(xml配置)
一、类:
/**
* 定义切面
* @author dell
*
*/
public class CheckAspect {
/*
* 定义通知advice(before型),标识在哪个切入点(allSaveMethod),织入(weaver)此方法
*/
public void checkUser(){
System.out.println("=======CheckAspect.checkUser()===========");
}
}
二、applicationcontex.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:tx="http://www.springframework.org/schema/tx
"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
"
>
<aop:config> </beans> 三、测试用例:
public void testAspect(){ 输出结果: =======CheckAspect.checkUser()===========
四、Advice中可以加入JoinPoint参数,内含有代理类的方法的方法名和参数数组
import org.aspectj.lang.JoinPoint
; /* 输入结果: 参数0=cat ======================================= 注:Aspect默认情况下不用实现接口,但对于目标对象(UserManagerImpl.java),在默认情况下必须实现接口; ================================== Spring AOP 用户可能会经常使用 除了返回类型模式(上面代码片断中的ret-type-pattern),名字模式和参数模式以外,所有的部分都是可选的。 返回类型模式决定了方法的返回类型必须依次匹配一个连接点。 你会使用的最频繁的返回类型模式是 下面给出一些常见切入点表达式的例子。 任意公共方法的执行: 任何一个以“set”开始的方法的执行: 定义在service包里的任意方法的执行: 定义在service包或者子包里的任意方法的执行: 在service包里的任意连接点(在Spring AOP中只是方法执行) : 在service包或者子包里的任意连接点(在Spring AOP中只是方法执行) : 实现了 'this'在binding form中用的更多:- 请常见以下讨论通知的章节中关于如何使得代理对象可以在通知体内访问到的部分。
实现了 'target'在binding form中用的更多:- 请常见以下讨论通知的章节中关于如何使得目标对象可以在通知体内访问到的部分。
任何一个只接受一个参数,且在运行时传入的参数实现了 'args'在binding form中用的更多:- 请常见以下讨论通知的章节中关于如何使得方法参数可以在通知体内访问到的部分。
请注意在例子中给出的切入点不同于 有一个 '@target' 也可以在binding form中使用:请常见以下讨论通知的章节中关于如何使得annotation对象可以在通知体内访问到的部分。
任何一个目标对象声明的类型有一个 '@within'也可以在binding form中使用:- 请常见以下讨论通知的章节中关于如何使得annotation对象可以在通知体内访问到的部分。
任何一个执行的方法有一个 '@annotation' 也可以在binding form中使用:- 请常见以下讨论通知的章节中关于如何使得annotation对象可以在通知体内访问到的部分。
任何一个接受一个参数,并且传入的参数在运行时的类型实现了 '@args'也可以在binding form中使用:- 请常见以下讨论通知的章节中关于如何使得annotation对象可以在通知体内访问到的部分。
================================================
出现error at ::0 can't find referenced pointcut...这样的错误原因是:如果你用的JDK版本是1.6的话,而引用的aspectjrt.jar是spring-2.0中包含的jar包的情况下就会报这样的错误。
<bean id="checkAspect" class="com.CheckAspect" />
<bean id="userManager" class="com.manager.impl.UserManagerImpl" />
<!-- 配置切面appect , ref切面类 -->
<aop:aspect id="check" ref="checkAspect">
<!-- 配置切入点pointcut, 定义一个表达式 -->
<aop:pointcut id="allSaveMethod" expression="execution(* com.manager.impl.UserManagerImpl.save*(..))"/>
<!-- 设置before advice, 用checkAspect中的一个方法,并定位到相位的切入点pointcut -->
<aop:before method="checkUser" pointcut-ref="allSaveMethod"/>
</aop:aspect>
</aop:config>
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserManager userManager = (IUserManager) factory.getBean("userManager");
userManager.saveUser("cat", "123");
userManager.delUser(1);
userManager.updateUser(1);
}
=====UserManagerImpl.saveUser()===========
=====UserManagerImpl.delUser()===========
=====UserManagerImpl.updateUser()===========
* 定义通知advice(before型),标识在哪个切入点(allSaveMethod),织入(weaver)此方法
* JoinPoint 内含代理类的参数和方法
*/
public void checkUser(JoinPoint joinPoint
){
Object[] args = joinPoint.getArgs();
for(int i=0; i<args.length; i++){
System.out.println("参数" + i + "=" + args[i]);
}
System.out.println("代理类的方法:" + joinPoint.getSignature().getName());
System.out.println("=======CheckAspect.checkUser()===========");
}
参数1=123
代理类的方法:saveUser
=======CheckAspect.checkUser()===========
=====UserManagerImpl.saveUser()===========
=====UserManagerImpl.delUser()===========
=====UserManagerImpl.updateUser()===========
如果没有实现接口必须引入CGLIB库
execution
pointcut designator。执行表达式的格式如下:execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
*
,它代表了匹配任意的返回类型。 一个全称限定的类型名将只会匹配返回给定类型的方法。名字模式匹配的是方法名。 你可以使用 *
通配符作为所有或者部分命名模式。 参数模式稍微有点复杂:()
匹配了一个不接受任何参数的方法, 而 (..)
匹配了一个接受任意数量参数的方法(零或者更多)。 模式 (*)
匹配了一个接受一个任何类型的参数的方法。 模式 (*,String)
匹配了一个接受两个参数的方法,第一个可以是任意类型,第二个则必须是String类型。 请参见AspectJ编程指南的 Language Semantics
部分。
execution(public * *(..))
execution(* set*(..))
AccountService
接口的任意方法的执行:execution(* com.xyz.service.AccountService.*(..))
execution(* com.xyz.service.*.*(..))
execution(* com.xyz.service..*.*(..))
within(com.xyz.service.*)
within(com.xyz.service..*)
AccountService
接口的代理对象的任意连接点(在Spring AOP中只是方法执行) :this(com.xyz.service.AccountService)
AccountService
接口的目标对象的任意连接点(在Spring AOP中只是方法执行) :target(com.xyz.service.AccountService)
Serializable
接口的连接点 (在Spring AOP中只是方法执行)args(java.io.Serializable)
execution(* *(java.io.Serializable))
: args只有在动态运行时候传入参数是可序列化的(Serializable)才匹配,而execution 在传入参数的签名声明的类型实现了 Serializable
接口时候匹配。@Transactional
注解的目标对象中的任意连接点(在Spring AOP中只是方法执行)@target(org.springframework.transaction.annotation.Transactional)
@Transactional
注解的连接点(在Spring AOP中只是方法执行)@within(org.springframework.transaction.annotation.Transactional)
@Transactional
annotation的连接点(在Spring AOP中只是方法执行)@annotation(org.springframework.transaction.annotation.Transactional)
@Classified
annotation的连接点(在Spring AOP中只是方法执行)@args(com.xyz.security.Classified)
解决的办法就是下载最新的aspectjrt的jar包即可aspectj-1.6.6.jar
发表评论
-
调试jdk中的源码,查看jdk局部变量
2013-06-15 23:30 1006调试jdk中的源码,查看jdk局部变量 2012-04 ... -
Eclipse快捷键 10个最有用的快捷键<转>
2013-04-11 23:28 1004Eclipse中10个最有用的快捷键组合 一个Eclip ... -
Lucene 3.6 中文分词、分页查询、高亮显示等
2012-12-09 23:35 17631、准备工作 下载lucene 3.6.1 : htt ... -
Maven实战(九)——打包的技巧(转)
2012-10-12 00:41 894“打包“这个词听起 ... -
基于Maven的web工程如何配置嵌入式Jetty Server开发调试环境(转)
2012-10-12 00:28 8661、首先在web工程的POM文件里添加依赖jar包如下: ... -
轻轻松松学Solr(1)--概述及安装[转]
2012-09-18 14:59 945概述 这段时间对企 ... -
分析Netty工作流程[转]
2012-09-04 19:02 836下面以Netty中Echo的例 ... -
让eclipse在ubuntu下面好看一点
2012-03-27 10:17 877<p> </p> <h1 cla ... -
zookeeper安装和应用场合(名字,配置,锁,队列,集群管理)[转]
2012-01-12 17:59 1609安装和配置详解 本文 ... -
Jakarta-Common-BeanUtils使用笔记[转]
2012-01-10 14:13 1116Jakarta-Common-BeanUtils ... -
一个关于Java Thread wait(),notify()的实用例【转】
2012-01-07 16:05 980///// // ProducerConsume ... -
Java基础:Java中的 assert 关键字解析【转】
2012-01-06 19:50 970J2SE 1.4在语言上提供了 ... -
一篇不错的讲解Java异常的文章(转载)----感觉很不错,读了以后很有启发[转]
2012-01-06 15:02 1213六种异常处理的陋习 ... -
如何解决HP QC(Quality Center)在Windows 7下不能工作的问题
2011-12-26 10:48 1533HP QC(Quantity Center) 是一款不错的测 ... -
JAVA读写文件,中文乱码 【转】
2011-12-19 23:43 2067最近在做HTML静态生成,需要从硬盘上把模版文件的内容读出来。 ... -
Java 6 JVM参数选项大全(中文版)【转】
2011-12-19 19:51 920Java 6 JVM参数选项大全(中文版) 作者 ... -
使用assembly plugin实现自定义打包【转】
2011-12-13 01:58 920在上一篇文章中,讨论到在对maven的机制不熟悉的情况下,为了 ... -
使用maven ant task实现非标准打包[转]
2011-12-13 01:56 1000maven很强大,但是总有些事情干起来不是得心应手,没有使用a ... -
Java日期转换SimpleDateFormat格式大全【转】
2011-12-08 20:22 125924小时制时间 显示: public clas ... -
使用Spring的表单标签库
2011-11-22 20:08 102313.9. 使用Spring的 ...
相关推荐
SpringAOP的注解配置完成切面的编程,完成execution,annotation两种表达式的实例Ddemo
基于Log4j+SpringAOP+Annotation的可注解日志切面组件 使用方式 @Log(logAfter = true, logBefore = true) public returntype methodName(params) 测试类com.liam.aop.aspect.test.AopLogAnnotationTest 测试输出 ...
spring中自定义注解(annotation)与AOP中获取注解.通过实例演示自定义注解。
这里zip压缩包囊括了学习Spring过程中用到的所有的jar包; 有: ...spring-web-4.1.2.RELEASE.jar //aop注解需要 对于Spring环境的插件,你们可以到Eclipase中的help>Eclipse marketplace...去搜索安装
在微服务流行的当下,在使用SpringCloud/Springboot框架开发中,AOP使用的非常广泛,尤其是@Aspect注解方式当属最流行的,不止功能强大,性能也很优秀,还很舒心!所以本系列就结合案例详细介绍@Aspect方式的切面的...
NULL 博文链接:https://tonl.iteye.com/blog/1966822
在微服务流行的当下,在使用Spring Cloud / Spring Boot框架开发中,AOP使用的非常广泛,尤其是@Aspect注解方式当属最流行的,不止功能强大,性能也很优秀,还很舒心!所以本系列就结合案例详细介绍@Aspect方式的切...
两行为开启spring的注解配置 <aop:aspect id="aspect" ref="logIntercepter"> 引入具体的AOP操作类 <aop:pointcut expression="execution(* com.spring.service..*(..))" id="pointCut"/>声明一个切入点,注意...
本篇文章主要介绍了详解Spring Aop实例之AspectJ注解配置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...
(二) annotation注解方式的SessionFactory 30 二、 引入hibernate所需要使用的jar 31 (一) 基本jar 31 (二) 加入annotation功能的jar包 31 (三) 搭建日志环境并配置显示DDL语句jar包 31 三、 Spring整合hibernate3...
SpringBoot 自定义注解,属性支持SPEL表达式。介绍了SPEL表达式的解析
(二) annotation注解方式的SessionFactory 30 二、 引入hibernate所需要使用的jar 31 (一) 基本jar 31 (二) 加入annotation功能的jar包 31 (三) 搭建日志环境并配置显示DDL语句jar包 31 三、 Spring整合hibernate3...
设计思想及原理使用方法注解(Annotation)说明表达式的应用缓存删除注意事项缓存管理页面与Spring Cache的区别源码阅读已经实现基于aspectj,代码在com.jarvis.cache.aop.aspectj.AspectjAopInterceptor。想通过...
主要介绍了详解SpringBoot AOP 拦截器 Aspect,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
《Spring3.x企业应用开发实战》是在《精通Spring2.x——企业应用开发详解》的基础上,经过历时一年的重大调整改版而成的,本书延续了上一版本追求深度,注重原理,不停留在技术表面的写作风格,力求使读者在熟练使用...
2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)...
9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 事务传播 9.5.8. 通知...
Android AOP注解Annotation详解(一) Android AOP之注解处理解释器详解(二) Android AOP 注解详解及简单使用实例(三) 一、简介 在Android 里面 注解主要用来干这么几件事: 和编译器一起给你一些提示警告信息...
2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut ...