-------------------siwuxie095

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

基于 AspectJ 的注解方式进行 AOP 开发

 
 

 
 

1、在
Spring 的核心配置文件中配置对象

 
 

 
 

 
 

 
 

 
 

2、在
Spring 的核心配置文件中通过命名空间 aop 开启

AspectJ 的自动代理

 
 

 
 

 
 

 
 

 
 

3、在增强类中使用注解完成
AOP 开发

 
 

 
 

 
 

 
 

4、AOP 注解如下:

 
 

 
 

 
 

 
 

 
 

5、具体实现

 
 

(1)编写一个被增强类

 
 

Book.java:

 
 

package com.siwuxie095.aop;

 
 

//被增强类

public class Book {

 
 

public
void add() {

System.out.println("----- add -----");

}

 

}

 
 

 
 

 
 

(2)编写一个增强类

 
 

MyBook.java:

 
 

package com.siwuxie095.aop;

 
 

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

 
 

 
 

/**

* 增强类

*

* 在增强类上使用注解 @Aspect

*

*/

@Aspect

public class MyBook {

 
 

/**

* 在方法上使用注解完成增强配置

*

* 其中,value 可以省略不写

*

* 即 @Before("execution(* com.siwuxie095.aop.Book.add(..))")

*/

@Before(value="execution(* com.siwuxie095.aop.Book.add(..))")

public
void beforeAdd() {

System.out.println("----- 前置增强 -----");

}

 

@AfterReturning(value="execution(* com.siwuxie095.aop.Book.add(..))")

public
void afterAdd(){

System.out.println("----- 后置增强 -----");

}

 

@Around(value="execution(* com.siwuxie095.aop.Book.add(..))")

public
void aroundAdd(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

System.out.println("----- 环绕增强(方法之前) -----");

//执行被增强的方法

proceedingJoinPoint.proceed();

System.out.println("----- 环绕增强(方法之前) -----");

}

 

}

 
 

 
 

 
 

 
 

(3)在配置文件中进行配置

 
 

applicationContext.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"

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">

 
 

 
 

<!-- 配置对象 -->

<bean
id="book"
class="com.siwuxie095.aop.Book"></bean>

<bean
id="myBook"
class="com.siwuxie095.aop.MyBook"></bean>

 
 

 
 

<!--

开启 AspectJ 的自动代理

 

通过配置织入 @Aspect 切面

-->

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

 
 

 
 

</beans>

 
 

 
 

 
 

(4)编写一个测试类

 
 

TestAop.java:

 
 

package com.siwuxie095.aop;

 
 

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 
 

public class TestAop {

 

/**

* 手动加上 @Test 以进行单元测试(将自动导入 JUnit 4 的 jar 包)

*

* 选中方法名,右键->Run As->JUint Test

*/

@Test

public
void testAop() {

 

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

 

Book book=(Book) context.getBean("book");

 

book.add();

}

 

}

 
 

 
 

 
 

(5)运行一览:

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

【made by siwuxie095】

基于AspectJ的注解方式进行AOP开发的更多相关文章

  1. 基于AspectJ的XML方式进行AOP开发

    -------------------siwuxie095                                 基于 AspectJ 的 XML 方式进行 AOP 开发         1 ...

  2. AOP——基于AspectJ的注解来实现AOP操作

    1.使用注解方式实现AOP操作 第一步:创建对象 <!-- 创建对象 --> <bean id="book" class="com.bjxb.aop.B ...

  3. Spring的AOP基于AspectJ的注解方式开发2

    参考自黑马培训机构 上一篇博客提到了在配置文件中开启aop的注解开发,以及简单使用了@Before,@Aspect 这是为了告诉spring为前置通知和切面类 接下来介绍aop的注解的通知类型,和切入 ...

  4. Spring的AOP基于AspectJ的注解方式开发1

    参考自黑马培训机构 创建项目,引入jar包 编写目标类,切面类并完成配置 package spring.day2_aop2; /* * 编写目标类 */ public class OrderDao { ...

  5. Spring的AOP基于AspectJ的注解方式开发3

    上上偏博客介绍了@Aspect,@Before 上篇博客介绍了spring的AOP开发的注解通知类型:@Before,@AfterThrowing,@After,@AfterReturning,@Ar ...

  6. spring AOP (使用AspectJ的注解方式 的aop实现) (6)

    目录 一.在 Spring 中启用 AspectJ 注解支持 二.AspectJ 支持 5 种类型的通知注解: 2.1.使用之前的 计算器接口和实现类 ArithmeticCalculator.jav ...

  7. Spring框架的事务管理之基于AspectJ的注解方式(重点掌握,最简单的方式)

    1. 步骤一:恢复转账的开发环境(具体开发环境实现见:https://www.cnblogs.com/wyhluckdog/p/10137283.html)2. 步骤二:applicationCont ...

  8. 【AOP】操作相关术语---【Spring】的【AOP】操作(基于aspectj的xml方式)

    [AOP]操作相关术语 Joinpoint(连接点):类里面哪些方法可以被增强,这些方法称为连接点. Pointcut(切入点):在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面add ...

  9. (转)使用Spring的注解方式实现AOP的细节

    http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...

随机推荐

  1. poj 3069 Saruman's Army(贪心)

    Saruman's Army Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  2. HUD 1175 连连看

    连连看 Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submiss ...

  3. Fragment之介绍(转)

    http://www.cnblogs.com/plokmju/p/3239265.html 前言 开门见山开篇名义,本篇博客将讲解一下Android中Fragment的内容,必要的地方会提供相应的演示 ...

  4. Win10巧用自带输入法轻松打出特殊字符

    给电脑输入信息时,如果要用上键盘上没有的特殊符号,那就为难了.其实,在Win10中,自带的微软拼音就能让你轻松输入键盘上没有的符号.下面来看看Win10如何输入特殊符号. 微软拼音不但中文输入智能化做 ...

  5. linux下一个启动和监测多个进程的shell脚本程序

    #!/bin/sh# Author:tang# Date:2017-09-01 ProcessName=webcrawlerInstanceCount=6RuntimeLog='runtime.log ...

  6. 并发基础(三) java线程优先级

      在不同的JVM中(JVM也算是一个操作系统),有着不同的CPU调度算法,对于大部分的JVM来说,优先级也是调度算法中的一个参数.所以,线程优先级在一定程度上,对线程的调度执行顺序有所影响,但不能用 ...

  7. tornado-简单的服务器非阻塞

    1.服务器 非阻塞 import tornado.ioloop import tornado.web import tornado.httpserver # 非阻塞 import tornado.op ...

  8. MYSQL体系结构-来自期刊

    MySQL三层体系结构 |-----------------------------------------------------------------------------------| | ...

  9. IP 别名和辅助 IP 地址

    https://blog.csdn.net/xiewen99/article/details/54729112?utm_source=itdadao

  10. SQL 2008 启用和禁用xp_cmdshell

    xp_,cmd,cmdshell -- 允许配置高级选项EXEC sp_configure 'show advanced options', 1GO-- 重新配置RECONFIGUREGO-- 禁用x ...