使用Spring框架入门三:基于XML配置的AOP的使用
一、引入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的使用的更多相关文章
- Spring框架入门之基于xml文件配置bean详解
关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...
- Spring框架入门之基于Java注解配置bean
Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...
- Spring3.0 入门进阶(三):基于XML方式的AOP使用
AOP是一个比较通用的概念,主要关注的内容用一句话来说就是"如何使用一个对象代理另外一个对象",不同的框架会有不同的实现,Aspectj 是在编译期就绑定了代理对象与被代理对象的关 ...
- spring框架之AspectJ的XML方式完成AOP的开发
1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开发包 * spring的传统AOP的开发的包 * sp ...
- Spring使用AspectJ注解和XML配置实现AOP
本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...
- 基于XML配置的AOP实现日志打印
Spring中可以使用注解或XML文件配置的方式实现AOP.1.导入jar包 com.springsource.net.sf.cglib -2.2.0.jar com.springsource.org ...
- Spring依赖注入:基于xml配置
基础接口 BeanFactory.ApplicationContext. BeanFactory用于创建并管理.获取各种类的对象. ApplicationContext从BeanFactory派生而来 ...
- 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP
上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. package com.yan ...
- Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析
Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析 本文简要介绍了基于 Spring 的 web project 的启动流程,详细分析了 Spring 框架将开发人员基于 XML ...
随机推荐
- SpringBoot集成JWT实现token验证
原文:https://www.jianshu.com/p/e88d3f8151db JWT官网: https://jwt.io/ JWT(Java版)的github地址:https://github. ...
- indy10的idHttpServer发送流
indy10的idHttpServer发送流 先看源码: procedure TIdIOHandler.Write(AStream: TStream; ASize: TIdStreamSize = 0 ...
- ios7下UISearchBar UITextField 光标不出现的问题
app支持ios7,在UINavBar 里面加入搜索框,结果光标一直出现不了.在overstackflow网站搜索了一下,竟然有人遇到相同的问题.... 解决办法如下: searchBar.tintC ...
- android中Bitmap的放大和缩小的方法
android中Bitmap的放大和缩小的方法 时间 2013-06-20 19:02:34 CSDN博客原文 http://blog.csdn.net/ada168855/article/det ...
- rtsp实现的开源代码
* live.com C/S C++ http://www.live555.com * darwin S C++ http://www.opensource.apple ...
- SharePoint Online 创建资产库
前言 本文介绍如何在Office 365中创建资产库库,以及资产库的一些基本设置. 正文 通过登录地址登录到Office 365的SharePoint Online站点中,我们可以在右上角的设置菜单中 ...
- 离线环境下使用二进制方式安装配置Kubernetes集群
本文环境 Redhat Linux 7.3,操作系统采用的最小安装方式. Kubernetes的版本为 V1.10. Docker版本为18.03.1-ce. etcd 版本为 V3.3.8. 1. ...
- Keras教程
In this step-by-step Keras tutorial, you’ll learn how to build a convolutional neural network in Pyt ...
- httpd 不带反斜杠 出现 301重定向
[root@VM_64_69_centos httpd]# curl http://localhost:9001/pay <!DOCTYPE HTML PUBLIC "-//IETF/ ...
- storm的一些相关文章
文章可以看这些: https://www.cnblogs.com/zhaojiankai/p/7257617.html https://blog.csdn.net/wangshuminjava/art ...