spring之AspectJ基于注解 AOP编程
一、前言
使用注解代替之前在spring配置文件中配置目标类、切面类和aop配置。
二、注意
- 需要注意的是,需要在spring配置文件中引入如下,如果不添加,切面类中的@Aspect注解将不起作用
<aop:aspectj-autoproxy/>
使用的时候通知单独使用
- 导入的jar包

- 当有多个切面,需要考虑其优先级时切面类使用@Order(数字:越小优先级越高) 参考:https://www.cnblogs.com/dreamfree/p/4102619.html
三、注解的使用
切面类:
@Aspect 声明切面,修饰切面类,从而获得 通知。
通知:
@Before 前置
@AfterReturning 后置
@Around 环绕
@AfterThrowing 抛出异常
@After 最终
切入点:
@PointCut ,修饰方法 private void xxx(){} 之后通过“方法名”获得切入点引用
四、代码实现
beans.xml
<!-- 扫描注解 -->
<context:component-scan base-package="com.xx"/>
<!-- aspectj自动代理 -->
<aop:aspectj-autoproxy/>
切面类:通知单独使用
package com.xx.myaspect; import java.util.Arrays; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* 切面类
* @author phoebe
* @Component:类级注解
* @Aspect:切面注解
*/
@Component
@Aspect
@Order(1)
public class MyAscpect { /*
* 切点,方法名即是切点的id
* 后面的切点直接引用方法名称即可
* 方法体不需要再配置其他
*/
@Pointcut(value="execution(* com.xx.dao.UserDaoImpl.*(..))") public void pointCut(){}
/*
* 前置通知
* JoinPoint:带有方法名称
*/
@Before("pointCut()")
public void before(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
System.out.println("方法名称是:"+methodName);
} /*
* 环绕通知需要携带ProceedingJoinPoint类型的参数
* 环绕通知类似于动态代理的全过程:ProceedingJoinPoint类型的参数可以决定是否执行目标方法。
* 而且环绕通知必须有返回值,返回值即为目标方法的返回值
*
*/
@Around(value="pointCut()")
public Object arround(ProceedingJoinPoint pjp){
Object obj = null;
String methodName = pjp.getSignature().getName();
try {
//前置通知@Before
System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjp.getArgs()));
//执行目标方法
obj = pjp.proceed();
//后置通知@After
System.out.println("The method " + methodName + " ends with " + Arrays.asList(pjp.getArgs()));
} catch (Throwable e) {
//异常通知@AfterThrowing
System.out.println("The method " + methodName + " occurs expection : " + e);
throw new RuntimeException(e);
}
//返回通知@AfterReturning
System.out.println("The method " + methodName + " ends");
return obj;
} }
dao接口类:
package com.xx.dao;
public interface UserDao {
public void run1();
public void run2();
public void run3();
}
dao实现类
package com.xx.dao;
import org.springframework.stereotype.Repository; @Repository("userDaoImpl")
public class UserDaoImpl implements UserDao{
@Override
public void run1() {
int x = 1/0;
System.out.println("run1");
}
@Override
public void run2() {
System.out.println("run2");
}
@Override
public void run3() {
System.out.println("run3");
}
}
测试类:
package com.xx;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xx.dao.UserDao; public class Test {
public static void main(String[] args) {
String xmlPath = "classpath:beans.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(xmlPath);
UserDao userDao = (UserDao) context.getBean("userDaoImpl");
userDao.run1();
userDao.run2();
userDao.run3();
}
}
spring之AspectJ基于注解 AOP编程的更多相关文章
- spring之AspectJ基于xml AOP编程
一.引言: AspectJ框架不仅实现了面向切面编程,而且还支持注解,spring将它引入自己的规范之中. 二.需要了解: AspectJ是基于java语言的AOP框架 spring2.0之后支持As ...
- 使用 Spring 2.5 基于注解驱动的 Spring MVC
http://www.ibm.com/developerworks/cn/java/j-lo-spring25-mvc/ 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sp ...
- 使用 Spring 2.5 基于注解驱动的 Spring MVC--转
概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...
- spring的AspectJ基于XML和注解(前置、后置、环绕、抛出异常、最终通知)
1.概念 (1)AspectJ是一个基于Java语言的AOP框架 (2)Spring2.0以后新增了对AspectJ切入点表达式的支持 (3)AspectJ是AspectJ1.5的新增功能,通过JDK ...
- 一步一步深入spring(5)--使用基于注解的spring实现 AOP
1.要利用spring aop,至少需要添加以下jar包 使用spring需要的jarspring.jar .commons-logging.jar 使用切面编程(AOP)需要的jar aspectj ...
- Spring MVC中基于注解的 Controller
终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...
- Spring IOC之基于注解的容器配置
Spring配置中注解比XML更好吗?基于注解的配置的介绍提出的问题是否这种途径比XML更好.简单来说就是视情况而定. 长一点的答案是每一种方法都有自己的长处也不足,而且这个通常取决于开发者决定哪一种 ...
- 【spring】之基于注解@ComponentScan的一些使用
基于xml形式ComponentScan的使用如下 <context:component-scan base-package="com.luna" use-default-f ...
- Spring(七)之基于注解配置
基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...
随机推荐
- I2C 读取总是 0xFF,但是 ACK 是正常的解决方法
最近要读写 24C256,没有参考网上代码,自己撸了几个小时,总是不对,读取结果总是 0xFF,但是ACK的返回都是正确的,经过一番努力,终于找到问题所在了. 在芯片规格书里面时序图只有 START ...
- hihoCoder 1039:字符消除(字符串处理)
#1039 : 字符消除 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最近在玩一个字符消除游戏.给定一个只包含大写字母"ABC"的字符串s,消 ...
- [bzoj2157]旅游 (lct)
这个应该也算裸的模板题吧..主要是边权的问题,对于每条边u->v,我们可以新建一个节点代替他,把边的信息弄到新的点上,就变成u->x->v了... 当然了这样的话要防止u和v这些没用 ...
- BZOJ3676: [Apio2014]回文串(回文树)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3676 这叫模版题TAT #include<cstring> #include< ...
- zookeeper的安装以及启动jps进程
2.7.1安装 将下载好的安装包,解压到指定位置,这里为直接解压到当前位置,命令如下: tar -zxvf zk-{version}.tar.gz 修改zk配置,将zk安装目录下conf/zoo_sa ...
- UWP: 通过命令行启动 UWP 应用
最近在开发应用的过程中,我遇到了如标题所述的需求,其实主要是为了能够快捷启动应用,正像我们可以在"运行"对话框中可以输入一些可执行程序的名称后,就能够直接启动它:这样做,可以增加 ...
- Laravel5中使用阿里大于(鱼)发送短信验证码
在做用户注册和个人中心的安全管理时,我实现借助第三方短信平台(阿里大于(鱼))在Laravel框架中进行手机验证的设置:阿里大于,是阿里通信旗下优质便捷的云通信服务平台,整合了三大运营商的通信能力,为 ...
- window.history.go(-1)返回且刷新页面
windows窗口对象(历史)history.go(),history.back(),history.forward(). 因为windows对象引用不是必须的.所以windows.history.g ...
- LNMP环境的搭建
http://blog.csdn.net/wzy_1988/article/details/8438355#
- 【开发技术】refactor 重构----实现文件改名
当我们要改类名或接口名时,可能会遇到该类(接口)在其它文件中也有使用的情况,如一个个找比较麻烦也容易漏,这里推荐使用右键refactor->rename进行修改.