spring aop 使用xml方式的简单总结

时间:2024-01-02 16:36:26

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
===============================================