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. 使用 Environment Indicator 模块区分不同的 Drupal 环境

    每个 Drupal 网站建设人员到了某个时期,都会有误将线上站点当做本地站点进行修改的经历.尤其是在浏览器中打开了几十个页面时,很容易忘记究竟哪个是哪个. Environment Indicator ...

  2. 【教程】【FLEX】#003 自定义事件、模块间通讯

    本篇笔记,主要阐明 事件是如何创建 和 如何使用自定义事件达到模块之间通讯 的效果. 句子解释: 什么叫做模块之间的通讯呢?? 简单点说,就是两个模块之间可以互相传数据. A模块 可以接收到 B模块的 ...

  3. 【练习】sqlnet.ora

    在SQLNET.ora文件中设置以下参数可以实现IP访问限制: $ pwd/u01/app/oracle/product/10.2.0/db_1/network/admin$ vi sqlnet.or ...

  4. 能在手机播放的Flash代码

    有些使用Flash的广告图片变换代码在手机不支持,在网上搜到了一个解决的方法: Flash嵌入处: <iframe style="width:474px;height:276px; b ...

  5. C# ref和out的区别

    首先:两者都是按地址传递的,使用后都将改变原来参数的数值. 其次:ref可以把参数的数值传递进函数,但是out是要把参数清空,就是说你无法把一个数值从out传递进去的,out进去后,参数的数值为空,所 ...

  6. 第2章 linux文件系统

    1.用户和用户组管理 1.1 用户管理常用命令 1.用户账号添加命令useradd或adduser 命令格式:useradd [option] [username] 其中[option]为userad ...

  7. UVa11054 Gergovia的酒交易 Wine trading in Gergovia-递推

    https://vjudge.net/problem/UVA-11054 As you may know from the comic “Asterix and the Chieftain’s Shi ...

  8. linux 下安装 搭建 svn服务器

    1.下载svn http://subversion.apache.org/download 下载完成后解压,执行 ./configure --prefix=/usr/svn 提示 configure: ...

  9. CTest

    一.初识CTest CTest是CMake集成的一个测试工具,在使用CMakeLists.txt文件编译工程的时候,CTest会自动configure.build.test和展现测试结果 CTest有 ...

  10. Data Mover Script Templates

    Datamover is probably the best way to export and import data between PeopleSoft databases and the sc ...