前面给大家介绍了怎样使用注解的方式来开发AOP应用,现在学习一下使用XML方式怎样来开发AOP应用。
如果使用XML方式开发AOP应用的话,我们就不再需要提供注解的配置,我们只需要一个普通的Java类对象
MyInterceptor.java
- package cn.itcast.service;
- import org.aspectj.lang.ProceedingJoinPoint;
- /**
- * 切面
- */
- public class MyInterceptor {
- public void doAccessCheck() {
- System.out.println("前置通知");
- }
- public void doAfterReturning() {
- System.out.println("后置通知");
- }
- public void doAfter() {
- System.out.println("最终通知");
- }
- public void doAfterThrowing() {
- System.out.println("例外通知");
- }
- public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
- System.out.println("进入方法");
- Object result = pjp.proceed();
- System.out.println("退出方法");
- return result;
- }
- }
package cn.itcast.service;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* 切面
*/
public class MyInterceptor {
public void doAccessCheck() {
System.out.println("前置通知");
}
public void doAfterReturning() {
System.out.println("后置通知");
}
public void doAfter() {
System.out.println("最终通知");
}
public void doAfterThrowing() {
System.out.println("例外通知");
}
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("进入方法");
Object result = pjp.proceed();
System.out.println("退出方法");
return result;
}
}
可以看到,在这个切面里面,这只是一个普普通通的Java类,里面没有任何的注解。如果我们采用基于XML方式来开发AOP应用的话,我们是要在配置文件中对切面进行配置的。现在看一下切面该如何配置
基于基于XML配置方式声明切面
--------------------------------------------------------------------
- <bean id="orderservice" class="cn.itcast.service.OrderServiceBean"/>
- <bean id="log" class="cn.itcast.service.LogPrint"/>
- <aop:config>
- <aop:aspect id="myaop" ref="log">
- <aop:pointcut id="mycut" expression="execution(* cn.itcast.service..*.*(..))"/>
- <aop:before pointcut-ref="mycut" method="doAccessCheck"/>
- <aop:after-returning pointcut-ref="mycut" method="doReturnCheck "/>
- <aop:after-throwing pointcut-ref="mycut" method="doExceptionAction"/>
- <aop:after pointcut-ref="mycut" method=“doReleaseAction"/>
- <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
- </aop:aspect>
- </aop:config>
首先要把用做切面的类交给Spring管理,然后通过<aop:config>配置元素,首先配置引用这个切面<aop:aspect id="myaop" ref="log">。然后在切面底下,我们可以定义一些像切入点,前置通知,后置通知,例外通知,最终通知和环绕通知。这里有个参数pointcut-ref(切入点引用),那么引入的切入点就是前面定义的切入点<aop:pointcut id="mycut" expression="execution(* cn.itcast.service..*.*(..))"/>。
当方法被拦截到后呢,它就会应用这个method所指定的方法。
beans.xml
- <?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:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <aop:aspectj-autoproxy/>
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
- <bean id="aspetbean" class="cn.itcast.service.MyInterceptor"/>
- <aop:config>
- <aop:aspect id="asp" ref="aspetbean">
- <aop:pointcut id="mycut" expression="execution(* cn.itcast.service.impl.PersonServiceBean.*(..))"/>
- <aop:before pointcut-ref="mycut" method="doAccessCheck"/>
- <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
- <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
- <aop:after pointcut-ref="mycut" method="doAfter"/>
- <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
- </aop:aspect>
- </aop:config>
- </beans>
PersonServiceBean.java
- package cn.itcast.service.impl;
- import cn.itcast.service.PersonService;
- public class PersonServiceBean implements PersonService {
- public String getPersonName(Integer id) {
- System.out.println("我是getPersonName()方法");
- return "xxx";
- }
- public void save(String name) {
- //throw new RuntimeException("我爱例外");
- System.out.println("我是save()方法");
- }
- public void update(String name, Integer id) {
- System.out.println("我是update()方法");
- }
- }
package cn.itcast.service.impl;
import cn.itcast.service.PersonService;
public class PersonServiceBean implements PersonService {
public String getPersonName(Integer id) {
System.out.println("我是getPersonName()方法");
return "xxx";
}
public void save(String name) {
//throw new RuntimeException("我爱例外");
System.out.println("我是save()方法");
}
public void update(String name, Integer id) {
System.out.println("我是update()方法");
}
}
SpringAOPTest.java
- package junit.test;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import cn.itcast.service.PersonService;
- public class SpringAOPTest {
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- }
- @Test public void interceptorTest(){
- ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
- PersonService personService = (PersonService)cxt.getBean("personService");
- personService.save("yyy");
- }
- }
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.service.PersonService;
public class SpringAOPTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test public void interceptorTest(){
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService)cxt.getBean("personService");
personService.save("yyy");
}
}
现在看一下,我们采用基于XML方式开发AOP应用是否能够成功,在这里,目前除了例外通知不会执行外,其它通知都会执行,
运行单元测试代码,控制台输出
前置通知
进入方法
我是save()方法
后置通知
最终通知
退出方法
改动下代码,看下例外通知是否被执行
PersonServiceBean.java
- package cn.itcast.service.impl;
- import cn.itcast.service.PersonService;
- public class PersonServiceBean implements PersonService {
- public String getPersonName(Integer id) {
- System.out.println("我是getPersonName()方法");
- return "xxx";
- }
- public void save(String name) {
- throw new RuntimeException("我爱例外");
- //System.out.println("我是save()方法");
- }
- public void update(String name, Integer id) {
- System.out.println("我是update()方法");
- }
- }
package cn.itcast.service.impl;
import cn.itcast.service.PersonService;
public class PersonServiceBean implements PersonService {
public String getPersonName(Integer id) {
System.out.println("我是getPersonName()方法");
return "xxx";
}
public void save(String name) {
throw new RuntimeException("我爱例外");
//System.out.println("我是save()方法");
}
public void update(String name, Integer id) {
System.out.println("我是update()方法");
}
}
运行单元测试代码:控制台输出
前置通知
进入方法
例外通知
最终通知
在JUnit里,有例外抛出
java.lang.RuntimeException: 我爱例外
at cn.itcast.service.impl.PersonServiceBean.save(PersonServiceBean.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
...................................................
例外通知也被执行了。
大家看到,基于XML方式开发AOP应用也是挺简单的,和前面基于注解方式开发AOP应用不同之处是MyInterceptor.java这个类已经没有了注解的定义,它是一个纯粹的,普通的java bean对象,没有任何注解。 配置信息都是在XML配置文件里定义的。
我们采用AOP编程,一般都是用来做权限系统,运行期监控。。。这些系统
本文介绍如何使用XML配置方式实现AOP(面向切面编程),包括配置切入点、各种通知类型等,并通过示例代码展示了正常流程及异常流程下的通知执行情况。
1673

被折叠的 条评论
为什么被折叠?



