-------------------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. [转]LAMP(Linux-Apache-MySQL-PHP)网站架构

    本文转自 http://www.williamlong.info/archives/1908.html LAMP(Linux-Apache-MySQL-PHP)网站架构是目前国际流行的Web框架,该框 ...

  2. 了解java的类加载器

    1.java运行之前,编译后的class文件需要加载到虚拟机内存,这必须用到class的加载器来加载,所以有必要了解加载器原理. 2.加载器采用父类委派机制加载,这样的目的是保证基础类仅仅加载一次(比 ...

  3. margin-top失效

    span标签是行类元素,只能margin-left,right 解决办法: 将span标签改为块级标签

  4. debian新增加用户 拥有ROOT权限

    方案一 : 已经有了 新增加的用户  但是没有ROOT 权限: 首需要切换到root身份$su -(注意有- ,这和su是不同的,在用命令"su"的时候只是切换到root,但没有把 ...

  5. leetcode263

    public class Solution { private bool Judge(int x) { ) { return false; } int bound = Convert.ToInt32( ...

  6. 2.vo传参模式和ModerDriven传参模式

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html Copy上面的myStruts2项目,改名为myStruts2Vo项目.作如 ...

  7. 5.mybatis实战教程(mybatis in action)之五:与spring3集成(附源码)

    转自:https://blog.csdn.net/nnn9223643/article/details/41962097 在 这一系列文章中,前面讲到纯粹用mybatis 连接数据库, 然后 进行增删 ...

  8. php用正则判断是否为数字

    验证数字:^[0-9]*$验证n位的数字:^\d{n}$验证至少n位数字:^\d{n,}$验证m-n位的数字:^\d{m,n}$验证零和非零开头的数字:^(0|[1-9][0-9]*)$验证有两位小数 ...

  9. Dictionary 字典的使用

    Dim a, d, i             '创建几个变量Set d = CreateObject("Scripting.Dictionary")d.Add "a&q ...

  10. 突破MSDE 的2GB数据限制

    Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\ ...