XML兑现AOP(转)
XML实现AOP(转)代码一:接口和实现类Java代码package?cn.itcast.server??public?interface?IPersonService
XML实现AOP(转)
代码一:接口和实现类Java代码
package?cn.itcast.server;??public?interface?IPersonService?{??????public?void?save(String?name);??????public?String?getPersonName(Integer?id);??????public?void?update(String?name,Integer?id);??}??
Java代码package?cn.itcast.server.impl;??import?cn.itcast.server.IPersonService;??public?class?PersonServiceBean2?implements?IPersonService?{??????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方法");??????}??}??
代码二:拦截器的定义Java代码package?cn.itcast.server;??import?org.aspectj.lang.ProceedingJoinPoint;????public?class?MyInteceptor2?{??????public?void?doAccessCheck(){??????????System.out.println("这里输出的是前置通知???");??????}??????public?void?doAfterReturning(){??????????System.out.println("这里输出的是后置通知????"?);??????}??????public?void?doAfterReturn(){??????????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;??????}??}??
代码三:配置文件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/>??????????<!--AOP开发(基于xml配置进行AOP开发,基于注解方式进行AOP开发)??-->??????????<bean?id="myInteceptor2"?class="cn.itcast.server.MyInteceptor2"></bean>??????????<bean?id="personService2"?class="cn.itcast.server.impl.PersonServiceBean2"></bean>??????????<aop:config>??????????????<aop:aspect?id="asp"?ref="myInteceptor2">??????????????????<!--?expression说明要处理的切入点(方法),下面那个意思是过滤任意返回类型的,cn.itcast.server.impl.PersonServiceBean2类中的任意参数的切入点?-->??????????<!--?????<aop:pointcut?id="mycut"?expression="execution(*?cn.itcast.server.impl.PersonServiceBean2.*(..)?)"/>??-->????????????????<aop:pointcut?id="mycut"?expression="execution(*?cn.itcast.server.impl.PersonServiceBean2.*(java.lang.String,..)?)"/>???????????<!--?????<aop:pointcut?id="mycut"?expression="execution(!void?cn.itcast.server.impl.PersonServiceBean2.*(..)?)"/>?-->??????????????????<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="doAfterReturn"/>??????????????????<aop:around?pointcut-ref="mycut"?method="doBasicProfiling"/>??????????????</aop:aspect>??????????</aop:config>??</beans>??
测试类: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.server.IPersonService;??public?class?SpringAOPTest?{??????@BeforeClass??????public?static?void?setUpBeforeClass()?throws?Exception?{??????}??????@Test?public?void?interceptorTest(){??????????ApplicationContext?ac?=?new?ClassPathXmlApplicationContext("beans.xml");??????????IPersonService?ipersonService?=?(IPersonService)?ac.getBean("personService2");??????????ipersonService.update("xxx",2);??????????ipersonService.save("xx");??????????ipersonService.getPersonName(2);??????}??}?