一、前言

  使用注解代替之前在spring配置文件中配置目标类、切面类和aop配置。

二、注意

  • 需要注意的是,需要在spring配置文件中引入如下,如果不添加,切面类中的@Aspect注解将不起作用
<aop:aspectj-autoproxy/>
  • 使用的时候通知单独使用

  • 导入的jar包

  

三、注解的使用

切面类:

  @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编程的更多相关文章

  1. spring之AspectJ基于xml AOP编程

    一.引言: AspectJ框架不仅实现了面向切面编程,而且还支持注解,spring将它引入自己的规范之中. 二.需要了解: AspectJ是基于java语言的AOP框架 spring2.0之后支持As ...

  2. 使用 Spring 2.5 基于注解驱动的 Spring MVC

    http://www.ibm.com/developerworks/cn/java/j-lo-spring25-mvc/ 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sp ...

  3. 使用 Spring 2.5 基于注解驱动的 Spring MVC--转

    概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...

  4. spring的AspectJ基于XML和注解(前置、后置、环绕、抛出异常、最终通知)

    1.概念 (1)AspectJ是一个基于Java语言的AOP框架 (2)Spring2.0以后新增了对AspectJ切入点表达式的支持 (3)AspectJ是AspectJ1.5的新增功能,通过JDK ...

  5. 一步一步深入spring(5)--使用基于注解的spring实现 AOP

    1.要利用spring aop,至少需要添加以下jar包 使用spring需要的jarspring.jar .commons-logging.jar 使用切面编程(AOP)需要的jar aspectj ...

  6. Spring MVC中基于注解的 Controller

         终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...

  7. Spring IOC之基于注解的容器配置

    Spring配置中注解比XML更好吗?基于注解的配置的介绍提出的问题是否这种途径比XML更好.简单来说就是视情况而定. 长一点的答案是每一种方法都有自己的长处也不足,而且这个通常取决于开发者决定哪一种 ...

  8. 【spring】之基于注解@ComponentScan的一些使用

    基于xml形式ComponentScan的使用如下 <context:component-scan base-package="com.luna" use-default-f ...

  9. Spring(七)之基于注解配置

    基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...

随机推荐

  1. HttpClient 模拟发送Post和Get请求 并用fastjson对返回json字符串数据解析,和HttpClient一些参数方法的deprecated(弃用)的综合总结

    最近在做一个接口调用的时候用到Apache的httpclient时候,发现引入最新版本4.5,DefaultHttpClient等老版本常用的类已经过时了,不推荐使用了:去官网看了一下在4.3之后就抛 ...

  2. 最长递减子序列(nlogn)(个人模版)

    最长递减子序列(nlogn): int find(int n,int key) { ; int right=n; while(left<=right) { ; if(res[mid]>ke ...

  3. Kruscal(最小生成树)算法模版

    ;//最大点数 ;//最大边数 int n,m;//n表示点数,m表示边数 struct edge{int u,v,w;} e[maxm];//u,v,w分别表示该边的两个顶点和权值 bool cmp ...

  4. Codeforces Round #416 (Div. 2)(A,思维题,暴力,B,思维题,暴力)

    A. Vladik and Courtesy time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...

  5. [bzoj1826] [JSOI2010]缓存交换

    虽然不知道为什么..但显然,每次扔掉离下次查询最远的内存单元就行了233 用堆来维护贪心...(优先队列大法好 #include<cstdio> #include<iostream& ...

  6. 最长上升子序列(LIS经典变型) dp学习~5

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 Monkey and Banana Time Limit: 2000/1000 MS (Java ...

  7. 搭建vue开发环境的步骤

    相信很多人在刚开始学习vue这个框架的时候,在最开始搭建开发环境的时候,都会遇到一些大大小小的坑,我之前在学习angular的时候搭建过一次,过了一个月后在搭建第二次的时候,竟然有一些混乱,所以今天想 ...

  8. TCP 和 UDP

    TCP协议与UDP协议的区别    首先咱们弄清楚,TCP协议和UCP协议与TCP/IP协议的联系,很多人犯糊涂了,一直都是说TCP/IP协议与UDP协议的区别,我觉得这是没有从本质上弄清楚网络通信! ...

  9. HDU 1068 Girls and Boys(模板——二分图最大匹配)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1068 Problem Description the second year of the univ ...

  10. Nginx实战之反向代理WebSocket的配置实例

    http://www.jb51.net/article/112183.htm 最近在工作中遇到一个需求,需要使用 nginx 反向代理websocket,经过查找一番资料,目前已经测试通过,所以这篇文 ...