一、引入Jar包

        <!--测试1使用-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--测试2、3、4、5、6使用-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--测试Aop使用-->
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>

注意,如果不引入aspectjweaver包,会报找不到类的错误。

二、测试步骤

1、新建切入点类(JoinPoint):

package aoptest1;

public class MyWorker {
public void aopPointMethod1() {
System.out.println("this is aopPointMethod1 executed.");
}
}

2、建立增强类(Advice)

package aoptest1;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyWorkerExtension {
public void aopInspectAtBefore() {
System.out.println("this is aopInspectAtBefore method execute.");
} public void aopInspectAtAfter() {
System.out.println("this is aopInspectAtAfter method execute.");
} public void aopAround(ProceedingJoinPoint proceedingJoinPoint) {
try {
System.out.println("aopAround1");
proceedingJoinPoint.proceed();
System.out.println("aopAround2");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}

3、建立配置文件在resources下:applicationContextAopTest1.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="aoptest1"/>
<!--配置实体-->
<bean id="myworker1" class="aoptest1.MyWorker"/>
<bean id="myworkerExtension1" class="aoptest1.MyWorkerExtension"/>
<!--配置AOP-->
<aop:config>
<!--配置切入点-->
<aop:pointcut expression="execution(* aoptest1.MyWorker.aopPointMethod1(..))" id="aopPointMethod1PointCut"/>
<!--配置切面-->
<aop:aspect ref="myworkerExtension1">
<aop:before method="aopInspectAtBefore" pointcut-ref="aopPointMethod1PointCut"/>
<aop:after method="aopInspectAtAfter" pointcut-ref="aopPointMethod1PointCut"/>
<aop:around method="aopAround" pointcut-ref="aopPointMethod1PointCut"/>
</aop:aspect> </aop:config>
</beans>

4、测试

import aoptest1.MyWorker;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AopTest {
@Test
public void aopTest1() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextAopTest1.xml");
MyWorker mywoker = context.getBean(MyWorker.class);
mywoker.aopPointMethod1();
}
}

5、测试结果

this is aopInspectAtBefore method execute.
aopAround1
this is aopPointMethod1 executed.
aopAround2
this is aopInspectAtAfter method execute.

使用Spring框架入门三:基于XML配置的AOP的使用的更多相关文章

  1. Spring框架入门之基于xml文件配置bean详解

    关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...

  2. Spring框架入门之基于Java注解配置bean

    Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...

  3. Spring3.0 入门进阶(三):基于XML方式的AOP使用

    AOP是一个比较通用的概念,主要关注的内容用一句话来说就是"如何使用一个对象代理另外一个对象",不同的框架会有不同的实现,Aspectj 是在编译期就绑定了代理对象与被代理对象的关 ...

  4. spring框架之AspectJ的XML方式完成AOP的开发

    1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开发包 * spring的传统AOP的开发的包 * sp ...

  5. Spring使用AspectJ注解和XML配置实现AOP

    本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...

  6. 基于XML配置的AOP实现日志打印

    Spring中可以使用注解或XML文件配置的方式实现AOP.1.导入jar包 com.springsource.net.sf.cglib -2.2.0.jar com.springsource.org ...

  7. Spring依赖注入:基于xml配置

    基础接口 BeanFactory.ApplicationContext. BeanFactory用于创建并管理.获取各种类的对象. ApplicationContext从BeanFactory派生而来 ...

  8. 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP

    上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. package com.yan ...

  9. Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析

    Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析 本文简要介绍了基于 Spring 的 web project 的启动流程,详细分析了 Spring 框架将开发人员基于 XML ...

随机推荐

  1. Android 数据存储02之文件读写

    Android文件读写 版本 修改内容 日期 修改人 V1.0 原始版本 2013/2/25 skywang Android文件读写的有两种方式.一种,是通过标准的JavaIO库去读写.另一种,是通过 ...

  2. iReport数据库连接找不到驱动

  3. Hive学习之自己定义聚合函数

    Hive支持用户自己定义聚合函数(UDAF),这样的类型的函数提供了更加强大的数据处理功能. Hive支持两种类型的UDAF:简单型和通用型.正如名称所暗示的,简单型UDAF的实现很easy,但因为使 ...

  4. ios之网络异常与正常视图的切换

    1. xib中创建两个View 2. View的视图大概如下第一个:View View 第二个:View 3. 代码切换: [self.view addSubview:_redView];  // 会 ...

  5. Android 开发:由模块化到组件化(一)

    在Android SDK一文中,我们谈到模块化和组件化,现在我们来聊聊组件化开发背后的哪些事.最早是在广告SDK中应用组件化,但是同样适用于普通应用开发 以下高能,请做好心理准备,看不懂请发私信来交流 ...

  6. CUDA报错: Cannot create Cublas handle. Cublas won't be available. 以及:Check failed: status == CUBLAS_STATUS_SUCCESS (1 vs. 0) CUBLAS_STATUS_NOT_INITIALIZED

    Error描述: aita@aita-Alienware-Area-51-R5:~/AITA2/daisida/ssd-github/caffe$ make runtest -j8 .build_re ...

  7. [转]Infobright是一个与MySQL集成的开源数据仓库

    [文章作者:张宴 本文版本:v1.1 最后修改:2010.05.18 转载请注明原文链接:http://blog.zyan.cc/infobright/] Infobright是一个与MySQL集成的 ...

  8. cat /proc/net/sockstat

    cat /proc/net/sockstat [root@VP-CS-243 ~]# !cat cat /proc/net/sockstat sockets: used 294 TCP: inuse ...

  9. POJ训练计划2528_Mayor&#39;s posters(线段树/成段更新+离散化)

    解题报告 id=2528">地址传送门 题意: 一些海报,覆盖上去后还能看到几张. 思路: 第一道离散化的题. 离散化的意思就是区间压缩然后映射. 给你这么几个区间[1,300000] ...

  10. RateLimiter

    RateLimiter是Guava的concurrent包下的一个用于限制访问频率的类. 限流算法 常用的更平滑的限流算法有两种:漏桶算法和令牌桶算法. 很多传统的服务提供商如华为中兴都有类似的专利, ...