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 ...
随机推荐
- USACO Section 2.2 循环数 Runaround Numbers
OJ:http://www.luogu.org/problem/show?pid=1467 #include<iostream> #include<vector> #inclu ...
- 洛谷P1268 树的重量
P1268 树的重量 85通过 141提交 题目提供者该用户不存在 标签树形结构 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 有这种情况吗!!!! 题意似乎有问题 题目描述 树可以用来表 ...
- 解决问题 “You don't have permission to access /index.html on this server.”
前几天装一个linux 企业版5.0安装了apache,打开测试页面的时候出现如下错误: Forbidden You don't have permission to access /index.ht ...
- 互斥对象 Mutex 和MFC中的CMutex
互斥(Mutex)是一种用途非常广泛的内核对象.能够保证多个线程对同一共享资源的互斥访问.同临界区有些类似,只有拥有互斥对象的线程才具有访问资源的权限,由于互斥对象只有一个,因此就决定了任何情况下此共 ...
- Arrays.asList(数组) 解说
最近在用Arrays的asList()生成的List时,List元素的个数时而不正确. Java代码 一:Arrays.asList(数组)该方法是将数组转化为集合(该方法主要用于Object对象数组 ...
- js控制div颜色
<html><head></head><style>#div1{width:400px;height:400px;background-color:re ...
- Android IOS WebRTC 音视频开发总结(六二)-- 大数据解密国外实时通讯行业开发现状
本文主要介绍国外实时通讯行业现状,文章最早发表在我们的微信公众号上,详见这里,欢迎关注微信公众号blackerteam,更多详见www.blackerteam.com 上篇文章我们采用百度搜索指数来分 ...
- 一款非常简单的android音乐播放器源码分享给大家
一款非常简单的android音乐播放器源码分享给大家,该应用虽然很小,大家常用的播放器功能基本实现了,可能有点还不够完善,大家也可以自己完善一下,源码在源码天堂那里已经有了,大家可以到那里下载学习吧. ...
- State
#include <iostream> using namespace std; #define DESTROY_POINTER(ptr) if (ptr) { delete ptr; p ...
- 新建一个DataTable如何手动给其添加多条数据!
早晨起来,想起昨天利用winform做类似于sqlserver数据库导入数据功能的时候,用到了新建一个DataTable手动给其添加多条数据,平时用不到,需要的时候想不起来了,这次不妨把他记下来.以下 ...