AspectJ用注解替换xml配置
AspectJ基于注解的使用
AspectJ简介
AspectJ是一个基于Java语言的AOP框架,一般
其主要用途:自定义开发
一般情况下spring自动生成代理,要配置aop,
首先确定目标类,aspectj 切入点表达式,需要导入jar包
spring-framework-3.0.2.RELEASE-dependencies\org.aspectj\com.springsource.org.aspectj.weaver\1.6.8.RELEASE
除了以上这个关键包,还有spring所需的5个基础包,以及其他三个包,具体情况如下图:

在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"> <!-- 1.扫描 注解类 -->
<context:component-scan base-package="com.xk.proxy.aspectJ_zhujie"></context:component-scan> </beans>
接着在service层用注解替换原先xml文件中的Bean
@Service("userServiceId")
public class UserServiceImpl implements UserService {
}
替换
<bean id="userServiceId" class="xxx.xxx.UserServiceImpl"></bean>
在切面类中用注解替换相应Bean
@Component
public class MyAspect {
}
替换
<bean id="myAspectId" class="xxx.xxx.xxx.aspectJ.MyAspect"></bean>
接着必须要进行aspectj 自动代理配置,否则即使声明了切面,也无法获取切面类中的方法
<!-- 1.扫描 注解类 -->
<context:component-scan base-package="xxx.xxx.xxx.aspectJ_zhujie"></context:component-scan>
添加注解@Aspect ,声明切面,以获取切面方法
@Component
@Aspect
public class MyAspect {
}
注解@Aspect替换了
<aop:aspect ref="myAspectId">
<aop:aspect> 将切面类 声明“切面”,从而获得通知(方法) ref 切面类引用
接着替换 公共切入点
<aop:pointcut expression="execution(* xxx.xxx.proxy.aspectJ.UserServiceImpl.*(..))" id="myPointCut"/>
这里涉及到了切入点表达式
execution()用于描述方法
语法:execution(修饰符 返回值 包.类.方法名(参数) throws异常)
修饰符,一般省略
返回值,不能省略,
方法名,不能省略
‘ * ’表示任意
(参数)
() 表示无参
(..) 表示参数任意
用注解替换为
//声明公共切入点
@Pointcut("execution(* xxx.xxx.proxy.aspectJ.UserServiceImpl.*(..))")
private void myPointCut(){
}
这里我用的是环绕通知类型,所以替换环绕
<aop:around method="myAround" pointcut-ref="myPointCut"/>
@Around(value = "myPointCut()")
public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("前");
//手动执行目标方法
Object obj = joinPoint.proceed(); System.out.println("后");
return obj;
}
最后替换抛出异常
<aop:after-throwing method="myAfterThrowing" pointcut="execution(* xxx.xxx.UserServiceImpl.*(..))" throwing="e"/>
@AfterThrowing(value="execution(* xxx.xxx.UserServiceImpl.*(..))" ,throwing="e")
public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
System.out.println("抛出异常通知 : " + e.getMessage());
}
最终切面类:
/**
* 切面类,可含有多个通知
*/
@Component
@Aspect
public class MyAspect { //切入点当前有效
//@Before("execution(*xxx.xxx.UserServiceImpl.*(..))")
public void myBefore(JoinPoint joinPoint){
System.out.println("前置通知 : " + joinPoint.getSignature().getName());
} //声明公共切入点
@Pointcut("execution(*xxx.xxx.UserServiceImpl.*(..))")
private void myPointCut(){
} // @AfterReturning(value="myPointCut()" ,returning="ret")
public void myAfterReturning(JoinPoint joinPoint,Object ret){
System.out.println("后置通知 : " + joinPoint.getSignature().getName() + " , -->" + ret);
} // @Around(value = "myPointCut()")
public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("前");
//手动执行目标方法
Object obj = joinPoint.proceed();
System.out.println("后");
return obj;
} // @AfterThrowing(value="execution(* xxx.xxx.UserServiceImpl.*(..))" ,throwing="e")
public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
System.out.println("抛出异常通知 : " + e.getMessage());
} @After("myPointCut()")
public void myAfter(JoinPoint joinPoint){
System.out.println("最终通知");
} }
最终spring配置:
<!-- 1.扫描 注解类 -->
<context:component-scan base-package="com.xk.proxy.aspectJ_zhujie"></context:component-scan> <!-- 2.确定 aop注解生效 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
aop注解总结:
@Aspect 声明切面,修饰切面类,从而获得 通知。
通知
@Before 前置
@AfterReturning 后置
@Around 环绕
@AfterThrowing 抛出异常
@After 最终
切入点
@PointCut ,修饰方法 private void xxx(){} 之后通过“方法名”获得切入点引用
AspectJ用注解替换xml配置的更多相关文章
- Spring使用AspectJ注解和XML配置实现AOP
本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...
- @ComponentScan注解及其XML配置
开发中会经常使用包扫描,只要标注了@Controller.@Service.@Repository,@Component 注解的类会自动加入到容器中,ComponentScan有注解和xml配置两种方 ...
- [spring]Bean注入——使用注解代替xml配置
使用注解编程,主要是为了替代xml文件,使开发更加快速. 一.使用注解前提: <?xml version="1.0" encoding="UTF-8"?& ...
- mybatis使用注解替代xml配置,动态生成Sql
mybatis使用注解替代xml配置时,遇到判断条件是否为null或者为空时,@Select很难搞定,不知道怎么办? mybatis3中增加了使用注解来配置Mapper的新特性,使用 SelectPr ...
- Spring基础篇——通过Java注解和XML配置装配bean
自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应用程序维护,而是引用了第三方的类库,这个时候自动装配便无法实现,Spring对此也提供了相应的解决方案 ...
- 注解 和 xml 配置的优缺点【转】
java annotation(注解) 的优点缺点 Annotation和xml各自作为配置项的优点与缺点. Annotation 一.Annotation 的优点 1.保存在 class 文件中,降 ...
- Spring_Task初探(注解,XML配置)
这几天想写一个动态添加任务项目找了找Spring下的自带定时功能发现还真有,然后网上找了找资料写了个demo 写了两种方式来执行定时的任务(XML配置和注解) 先建两个普通的任务类(XML配置调用的任 ...
- Spring基础篇——通过Java注解和XML配置装配bean(转载)
作者:陈本布衣 出处:http://www.cnblogs.com/chenbenbuyi 本文版权归作者和博客园共有,欢迎转载分享,但必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留 ...
- 【SSH网上商城项目实战02】基本增删查改、Service和Action的抽取以及使用注解替换xml
转自:https://blog.csdn.net/eson_15/article/details/51297698 上一节我们搭建好了Struts2.Hibernate和Spring的开发环境,并成功 ...
随机推荐
- Python 包内的导入问题(绝对导入和相对导入)
基本概念 Python 中的包,即包含 __init__.py 文件的文件夹. 对于 Python 的包内导入,即包内模块导入包内模块,存在绝对导入和相对导入问题. 普通 Python 模块的搜索路径 ...
- Linux 分卷压缩
例如,要将大文件夹 PYNQ 分卷压缩成 1G 的单元大小,如下命令(类似的可以指定 tar 的参数为 czf 而生产 .tar.gz 格式的压缩包:可以指定分卷大小例如 500M 等),压缩完成后, ...
- Java中volatile关键字解析
一.内存模型的相关概念 大家都知道,计算机在执行程序时,每条指令都是在CPU中执行的,而执行指令过程中,势必涉及到数据的读取和写入.由于程序运行过程中的临时数据是存放在主存(物理内存)当中的,这时就存 ...
- npm太慢, 淘宝npm镜像使用方法
淘宝 npm 地址: http://npm.taobao.org/ 如何使用 有很多方法来配置npm的registry地址,下面根据不同情境列出几种比较常用的方法.以淘宝npm镜像举例: 1.临时使用 ...
- GFF高仿QQ客户端及服务器
一.GFF简介 GFF是仿QQ界面,通信基于SAEA.MessageSocket.SAEA.Http.SAEA.MVC实现包含客户端和服务器的程序,源码完全公开,项目源码地址:https://gith ...
- kafka+storm结合存在的一些问题与解决方法
在配置kafka和storm的时候, 经常的会出现一些问题, 主要在以下几个: 1. 打jar包上去storm集群的时候会出现jar包冲突,类似于log4j或者sf4j的报错信息. 2. kafka ...
- keras神经网络三个例子
keras构造神经网络,非常之方便!以后就它了.本文给出了三个例子,都是普通的神经网络 例一.离散输出,单标签.多分类 例二.图像识别,单标签.多分类.没有用到卷积神经网络(CNN) 例三.时序预测, ...
- vscode快捷键大全
一般Ctrl + Shift + P,F1显示命令调色板 Ctrl + P快速打开,转到文件...Ctrl + Shift + N新窗口/实例 Ctrl + Shift + W关闭窗口/实例 Ctrl ...
- 你知道Java的四种引用类型吗
关于java四种引用类型,我也是刚了解,特此记下! 在Java中提供了四个级别的引用:强引用,软引用,弱引用和虚引用.在这四个引用类型中,只有强引用FinalReference类是包内可见,其他三种引 ...
- docker部署nginx
1. 下载nginx [root@localhost my.Shells]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/ ...