spring aop的 xml的配置方式的简单实现:

1、编写自己的切面类:配置各个通知类型

/**
*
*/
package com.lilin.maven.service.aop; import org.aspectj.lang.ProceedingJoinPoint; /**
* @author lilin aop的切面类 配置各种通知类型
*
*/
public class InterceptorAop {
/**
* 前置通知
*/
public void doBefore() {
System.out.println("========doBefore advice==========");
} /**
* 返回后通知
*/
public void doAferReturning() {
System.out.println("========sdoAferReturning advice================");
} /**
* 后置通知
*/
public void doAfter() {
System.out.println("========doAfter advice==========");
} /**
* 抛出异常后通知
*/
public void doAferThrowing() {
System.out.println("=========doAferThrowing advice================");
} /**
* 环绕通知 环绕通知的第一个参数必须是ProceedingJoinPoint pjp.proceed()执行原业务方法
*
* @param pjp
* @return
* @throws Throwable
*/
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("=========doAround start===========");
Object result = pjp.proceed();
System.out.println("==========doAround end==========");
return result;
} }

2、基于xml的aop配置如下:

<?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-2.0.xsd">
//业务类,需要被AOP拦截的类
<bean id="userService" class="com.lilin.maven.service.aop.UserService"/>
   //切面类
<bean id="aopAspect" class="com.lilin.maven.service.aop.InterceptorAop"/>
//aop配置
<aop:config>
<aop:aspect ref="aopAspect">
//切点的配置 匹配那些类和那些方法需要aop拦截
<aop:pointcut expression="execution (* com.lilin.maven.service.aop.UserService.addUser(..))" id="pointCut"/>
//各个不同的advice的配置对应的执行方法
<aop:before pointcut-ref="pointCut" method="doBefore"/>
<aop:after-returning pointcut-ref="pointCut" method="doAferReturning"/>
<aop:after-throwing pointcut-ref="pointCut" method="doAferThrowing"/>
<aop:after pointcut-ref="pointCut" method="doAfter"/>
<aop:around pointcut-ref="pointCut" method="doAround"/>
</aop:aspect>
</aop:config>
</beans>

3、编写业务类的接口和实现:

/**
*
*/
package com.lilin.maven.service.aop; /**
* @author lilin
*
*/
public interface IUserService {
void addUser();
}
/**
*
*/
package com.lilin.maven.service.aop; /**
* @author lilin
*
*/
public class UserService implements IUserService { @Override
public void addUser() {
System.out.println("增加用户信息");
} }

4、编写测试类,这里采用的是testNG的测试方案,具体测试编写如下:

/**
*
*/
package com.lilin.maven.service.aop; import javax.annotation.Resource; import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.Test; import com.lilin.maven.service.BaseTest; /**
* @author lilin
*
*/
@ContextConfiguration(locations = { "classpath:/config/spring/spring-aop.xml" })
public class AopTest extends BaseTest { @Resource
private ApplicationContext cxt; @Test
public void aopXmlTest() {
IUserService userService = (IUserService) cxt.getBean("userService");
userService.addUser();
} }

其中baseTest主要是用于继承AbstractTestNGSpringContextTests 开启spring的注入测试。

/**
*
*/
package com.lilin.maven.service; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; /**
* @author lilin
*
*/
public abstract class BaseTest extends AbstractTestNGSpringContextTests { }

5、测试结果展示如下:从结果可以看到:在调用业务的addUser方法时,aop的配置已经可以正常工作了,各个advice也能正常执行。

[TestNG] Running:
C:\Users\lilin\AppData\Local\Temp\testng-eclipse-606080275\testng-customsuite.xml

2016-1-29 2:08:14 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [config/spring/spring-aop.xml]
2016-1-29 2:08:14 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@10f6d3: startup date [Fri Jan 29 02:08:14 CST 2016]; root of context hierarchy
2016-1-29 2:08:15 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1742700: defining beans [userService,aopAspect,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,org.springframework.aop.aspectj.AspectJPointcutAdvisor#3,org.springframework.aop.aspectj.AspectJPointcutAdvisor#4,pointCut,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy

========doBefore advice==========
=========doAround start===========
增加用户信息
========sdoAferReturning advice================
========doAfter advice==========
==========doAround end==========
PASSED: aopXmlTest

===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================

spring aop 使用xml方式的简单总结的更多相关文章

  1. Spring AOP(三)--XML方式实现

    本文介绍通过XML方式实现Spring AOP,在上一篇中已经介绍了通过注解+java配置的方式,这篇文章主要是看XML中怎么配置,直接上代码了: 一.创建一个连接点 1⃣️定义接口 注意⚠️:可以定 ...

  2. spring aop 使用注解方式总结

    spring aop的注解方式:和xml的配置方式略有区别,详细如下: 1.首先还是建立需要的切面类:切面类里面定义好切点配置,以及所有的需要实现的通知方法. /** * */ package com ...

  3. 【Spring AOP】Spring AOP的使用方式【Q】

    Spring AOP的三种使用方式 经典AOP使用方式 改进XML配置方式 基于注解的方式 第1种方式可以作为理解spring配置AOP的基础,是最原始的配置方式,也体现了spring处理的过程. 使 ...

  4. 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)

    Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式   √ id:标识容器中的bean.id唯一. √ cl ...

  5. Spring AOP 不同配置方式产生的冲突问题

    Spring AOP的原理是 JDK 动态代理和CGLIB字节码增强技术,前者需要被代理类实现相应接口,也只有接口中的方法可以被JDK动态代理技术所处理:后者实际上是生成一个子类,来覆盖被代理类,那么 ...

  6. 菜鸟学习Spring——60s配置XML方法实现简单AOP

    一.概述. 上一篇博客讲述了用注解的形式实现AOP现在讲述另外一种AOP实现的方式利用XML来实现AOP. 二.代码演示. 准备工作参照上一篇博客<菜鸟学习Spring--60s使用annota ...

  7. spring-第十八篇之spring AOP基于XML配置文件的管理方式

    1.在XML配置文件中配置切面.切入点.增强处理.spring-1.5之前只能使用XML Schema方式配置切面.切入点.增强处理. spring配置文件中,所有的切面.切入点.增强处理都必须定义在 ...

  8. 6.Spring【AOP】XML方式

    1.AOP术语 1. Joinpoint(连接点):所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点 2. Pointcut(切入点):所谓切 ...

  9. Spring AOP 的实现方式(以日志管理为例)

    一.AOP的概念 AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充,流行的AOP框架有Sping AOP.Aspect ...

随机推荐

  1. 【caffe-windows】 caffe-master 之Matlab中model的分类应用

    此篇讲述在matlab中,如何将训练好的model用于图像分类.将以mnist为例,主要用到caffe-master\matlab\demo 下的classification_demo.m ,可参考我 ...

  2. only one is important

    今天早上还是去了图书馆,看了一早上的hello world(intel opencl),清楚了程序编写的基本流程,但是,仍和以前一样,貌似看懂了,其实什么也不会,没有发现什么问题.中午,回来取了我的笔 ...

  3. WebView 获取网页点击事件

    网页上的点击按钮 本身绑定了URL,点击的时候webview 会在下面的这个方法中加载URL - (BOOL)webView:(UIWebView*)webView shouldStartLoadWi ...

  4. 数据结构(一)之HelloWord

    最近由于学习上面的需要,要重新的看看数据结构方面的知识!当然,我觉得数据结构也非常的重要,下面是我的学习的一点小小的记录,以备日后的查看! 我的环境: 1:操作系统:windows7 2:编码环境:M ...

  5. 洛谷P2751 [USACO4.2]工序安排Job Processing

    P2751 [USACO4.2]工序安排Job Processing 18通过 78提交 题目提供者该用户不存在 标签 难度普及+/提高 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 一家工 ...

  6. noi2010 能量采集

    2005: [Noi2010]能量采集 Time Limit: 10 Sec  Memory Limit: 552 MB Submit: 3068  Solved: 1820 [Submit][Sta ...

  7. hql语句关联查询(select new )

    在  new bean()中,对象里要有其构造方法,查询参数必须是构造方法中的参数,get set也要同步 类似于 String hql= "select  new A(a.id, a.na ...

  8. javaSE第十五天

    第十五天    117 1. 对象数组(掌握)    117 (1)定义:    117 (2)对象数组的内存图解    117 (3)案例:    117 2. 集合(Collection)(掌握) ...

  9. wpf常见枚举收集

    Icons made by from www.flaticon.com

  10. Permission Lists Assigned to a User

    SQL that I find useful in many occasions. It will return a list of permissions that are assigned to ...