一、前言

  使用注解代替之前在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. Flask中的单例模式

    1,基于文件的单例模式: import pymysql import threading from DBUtils.PooledDB import PooledDB class SingletonDB ...

  2. poj 1743

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24835   Accepted: 8377 De ...

  3. c语言几个松散的地方(不足的地方,不严谨的地方,它容易出错的地方)。

    c语言是面向过程的语言,是弱类型语言,c语言的源代码基本就是无数个函数的堆砌.即很多函数就组成c语言源代码了,也即它的源代码基本就是函数构成的. C语言里面的test()和test(void)是不一样 ...

  4. Logger之Logger.getLogger(CLass)技巧代替system.out.print

    ---恢复内容开始--- 尊重原创:http://www.cnblogs.com/zxf330301/p/5876117.html 之前一直在使用System.out.println()来调试.但是用 ...

  5. 教你上传本地代码到github转载

    原创 2015年07月03日 10:47:13 标签: 上传代码github   转载请标明出处: http://blog.csdn.net/hanhailong726188/article/deta ...

  6. Spider爬虫 の 事

      初识Spider_Man(爬爬虫) Spider_Man_2 の requests模块   Spider_Man_3 の selenium   Spider_Man_4 の BeautifulSo ...

  7. UE4 径向模糊radiu blur

    hlsl代码为: float2 ScreenMult = ; ; ] = {-0.08,-0.05,-0.03,-0.02,-0.01,0.01,0.02,0.03,0.05,0.08}; float ...

  8. 腾讯云中ssL证书的配置安装

    https://cloud.tencent.com/document/product/619/12797 配置 Nginx 和 HTTPS 完成以上准备工作,就要开始配置 Nginx 和 HTTPS ...

  9. parse_str() 函数把查询字符串解析到变量中。

    注释:如果未设置 array 参数,则由该函数设置的变量将覆盖已存在的同名变量. 注释:php.ini 文件中的 magic_quotes_gpc 设置影响该函数的输出.如果已启用,那么在 parse ...

  10. ThinkPHP5上传图片并压缩为缩略图

    使用thinkphp开发app后端中,需要实现一个处理上传图片队列的功能 这是个上传多图片保存并且需要对其中一张图片进行压缩的功能 (使用的html5 mui框架开发app,如果直接载入原图,app客 ...