spring aop 使用xml方式的简单总结
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了spring aop 使用xml方式的简单总结,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5551字,纯文字阅读大概需要8分钟。
内容图文
![spring aop 使用xml方式的简单总结](/upload/InfoBanner/zyjiaocheng/1306/11bd28a9640e4d3d9565d29eba0246ed.jpg)
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=========="); } /** * 返回后通知 */publicvoid doAferReturning() { System.out.println("========sdoAferReturning advice================"); } /** * 后置通知 */publicvoid doAfter() { System.out.println("========doAfter advice=========="); } /** * 抛出异常后通知 */publicvoid 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 publicvoid 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" }) publicclass AopTest extends BaseTest { @Resource private ApplicationContext cxt; @Test publicvoid 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
===============================================
原文:http://www.cnblogs.com/lilin0719/p/5167891.html
内容总结
以上是互联网集市为您收集整理的spring aop 使用xml方式的简单总结全部内容,希望文章能够帮你解决spring aop 使用xml方式的简单总结所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。