spring aop 使用xml方式的简单总结
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方式的简单总结的更多相关文章
- Spring AOP(三)--XML方式实现
本文介绍通过XML方式实现Spring AOP,在上一篇中已经介绍了通过注解+java配置的方式,这篇文章主要是看XML中怎么配置,直接上代码了: 一.创建一个连接点 1⃣️定义接口 注意⚠️:可以定 ...
- spring aop 使用注解方式总结
spring aop的注解方式:和xml的配置方式略有区别,详细如下: 1.首先还是建立需要的切面类:切面类里面定义好切点配置,以及所有的需要实现的通知方法. /** * */ package com ...
- 【Spring AOP】Spring AOP的使用方式【Q】
Spring AOP的三种使用方式 经典AOP使用方式 改进XML配置方式 基于注解的方式 第1种方式可以作为理解spring配置AOP的基础,是最原始的配置方式,也体现了spring处理的过程. 使 ...
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- Spring AOP 不同配置方式产生的冲突问题
Spring AOP的原理是 JDK 动态代理和CGLIB字节码增强技术,前者需要被代理类实现相应接口,也只有接口中的方法可以被JDK动态代理技术所处理:后者实际上是生成一个子类,来覆盖被代理类,那么 ...
- 菜鸟学习Spring——60s配置XML方法实现简单AOP
一.概述. 上一篇博客讲述了用注解的形式实现AOP现在讲述另外一种AOP实现的方式利用XML来实现AOP. 二.代码演示. 准备工作参照上一篇博客<菜鸟学习Spring--60s使用annota ...
- spring-第十八篇之spring AOP基于XML配置文件的管理方式
1.在XML配置文件中配置切面.切入点.增强处理.spring-1.5之前只能使用XML Schema方式配置切面.切入点.增强处理. spring配置文件中,所有的切面.切入点.增强处理都必须定义在 ...
- 6.Spring【AOP】XML方式
1.AOP术语 1. Joinpoint(连接点):所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点 2. Pointcut(切入点):所谓切 ...
- Spring AOP 的实现方式(以日志管理为例)
一.AOP的概念 AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充,流行的AOP框架有Sping AOP.Aspect ...
随机推荐
- delphi调用web service出现 Unable to retrieve the URL endpoint for Service/Port .....
delphi调用web service出现 Unable to retrieve the URL endpoint for Service/Port, 错误截图如下 查了很长时间, 发现在DataM ...
- vs 中无法加载项目的解决方案
有时,我们在vs 中打开工程项目时,会出现这样的错误,项目无法加载,解决方案如下: 错误提示: 解决方案: 在vs 中单独打开 工程配置文件 ,然后修改配置,将红圈处的内容注释掉: 重新加载项目:
- Spring Boot导出jar包发布
一:事由 现在的项目组开发项目使用的是Spring Boot的技术,开发的时候是直接通过一个入口主函数来启动项目的.如果将项目交给客户,怎样才能正确的发布运行呢?百度了一下有关的知识,大概了解到是通过 ...
- ECSHOP如何解决Deprecated: preg_replace()报错 Home / 开源程序 / ECSHOP / ECSHOP如何解决Deprecated: preg_replace()报错
Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in D:\w ...
- VBS创建数据表
'创建数据表'参数:strDBPath 字符串型 数据库路径'参数:strTableName 字符串型 需要创建的数据表的名称'参数:strColumnName 字符串型 初始化的字段名称,其实可以算 ...
- 【Linux】inode_针对MySQL读写操作在系统层的进一步学习【转】
转自http://www.cnblogs.com/itech/archive/2012/05/15/2502284.html 一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘 ...
- C#多线程案例基础
C#多线程案例基础(转) 在学习多线程之前,我们先来看几个概念: 1,什么是进程? 当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源,当然一个程序也可能开 ...
- fluentd正则表达式
一: 匹配上则取否则不取 匹配规则为 \[sv=(?<sv>[^\]]*?)\].*?\[os=(?<os>[^\]]*?)\].*?\[net=(?<net>[^ ...
- poj1005 I Think I Need a Houseboat
这题目只要读懂了意思就好做了,先求出来(0.0)到(x.y)的距离为r,然后求出来以r为半径的半圆的面积,然后再用这个面积除以50,再向上取整就可以啦. #include <stdio.h> ...
- JavaScript探秘系列
此文章所在专题列表如下: 我们应该如何去了解JavaScript引擎的工作原理 JavaScript探秘:编写可维护的代码的重要性 JavaScript探秘:谨慎使用全局变量 JavaScript探秘 ...