使用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 ...
随机推荐
- having只用来在group by之后,having不可单独用,必须和group by用。having只能对group by的结果进行操作
having只能对group by的结果进行操作 having只能对group by的结果进行操作 having只能对group by的结果进行操作 having只用来在group by之后,havi ...
- ECSHOP商城网站建设之自定义调用广告方法(二)
原文地址:http://www.cnblogs.com/zgzy/p/3598991.html 使用ecshop进行商城网站建设时,ecshop默认的很多功能对于我们个性化设计之后不太使用.今天我们主 ...
- Command /usr/sbin/chown failed with exit code 1?
问题: 解答: 转自:http://stackoverflow.com/questions/7589771/command-usr-sbin-chown-failed-with-exit-code-1
- uitextfield 设置为密码框显示
uitextfield 设置为密码框显示: 在xib中,将文本secure的复选框选中即可.
- jquery.jCal.js显示日历插件
描述:日历插件jCal用于需要输入日期的表单文本框. 兼容浏览器:IE浏览器/Firefox/Google Chrome 官方链接: http://www.overset.com/2008/05/1 ...
- PL/SQL报错:Initialization error Oracle client not properly installed
安装PL/SQL8.4后,连接数据库 提示错误Initialization error Oracle client not properly installed 解决方案: 1.下载instancec ...
- 《Netty权威指南》
<Netty权威指南> 基本信息 作者: 李林锋 出版社:电子工业出版社 ISBN:9787121233432 上架时间:2014-5-29 出版日期:2014 年6月 开本:16开 页码 ...
- 关于MySQL的行转列的简单应用(二)---group函数
MySQL的行转列.列转行.连接字符串 concat.concat_ws.group_concat函数用法使用方法:CONCAT(str1,str2,…) 返回结果为连接参数产生的字符串.如有任何一 ...
- 【转】PHP笔试题2010年
From : http://www.51projob.com/a/PHP/20120905/602.html 下午,还有一场比较大的面试等着我[虽然接到pps的录用电话,可是心里还是想去verycd试 ...
- 【UOJ Round #3】
枚举/二分 C题太神窝看不懂…… 核聚变反应强度 QwQ很容易发现次小的公约数一定是gcd的一个约数,然后……我就傻逼地去每次算出a[1],a[i]的gcd,然后枚举约数……这复杂度……哦呵呵... ...