基于基于XML配置方式声明切面
知识点:
【
//普通的java 类
public class LogPrint {
public void doAccessCheck() {}定义前置通知
public void doReturnCheck() {}定义后置通知
public void doExceptionAction() {}定义例外通知
public void doReleaseAction() {}定义最终通知
//环绕通知必须要有ProceedingJoinPoint pjp 参数和调用pjp.proceed();方法
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
return pjp.proceed();环绕通知
}
}
xml文件的配置:
<bean id="orderservice" 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>
】
准备工作:
【
dist\spring.jar
lib\jakarta-commons\commons-logging.jar
如果使用了切面编程(AOP),还需要下列jar文件
lib/aspectj/aspectjweaver.jar和aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
】
照样实现步骤如下:
第一步:编写业务层代码
PersonServiceBean。java类 IPersonServiceBean。java接口
public interface IPersonServiceBean {
public abstract void save(String name);
public abstract String update(String name);
}
public class PersonServiceBean implements IPersonServiceBean {
public void save(String name)
{
throw new IllegalArgumentException("抛出异常");
//System.out.println("save is invoke");
}
public String update(String name)
{
System.out.println("update is invoke");
return "Sueccess";
}
}
第二步: 编写拦截器类代码 (这里是基于xml配置文件进行拦截)
public class MyItercepterByXml {
public void doAccessCheck() {
// 在执行拦截方法前调用可得到输入参数
System.out.println("exctution 前置通知");
}
public void doReturnCheck() {
// 在执行拦截方法后调用可得到返回参数
System.out.println("exctution 后置通知");
}
public void doExceptionAction() {
System.out.println("exctution 异常通知");
}
public void doReleaseAction() {
System.out.println("exctution 最终通知");
}
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("exctution 开始环绕测试");
// 必须调用下面的方法
Object object = pjp.proceed();
System.out.println("exctution 结束环绕测试");
return object;
}
}
第三步:编写配置文件(这里使用xml文件把bean交个spring管理也可以同个类路径扫描)
<?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" ref="myItercepter">
<aop:pointcut id="myAnyMethod" expression="execution (* com.liyong.serviceBean.Imp.PersonServiceBean.*(..))"/>
<!--
注意:不要写成下面的形式
<aop:before pointcut="myAnyMethod" method="doAccessCheck"/>
-->
<aop:before pointcut-ref="myAnyMethod" method="doAccessCheck"/>
<aop:after-throwing pointcut-ref="myAnyMethod" method="doExceptionAction"/>
<aop:after-returning pointcut-ref="myAnyMethod" method="doReturnCheck"/>
<aop:after pointcut-ref="myAnyMethod" method="doReleaseAction"/>
<aop:around pointcut-ref="myAnyMethod" method="doBasicProfiling"/>
</aop:aspect>
</aop:config>
</beans>
第四步:编写单元测试
public class JunitTest {
@Test
public void TestAOP()
{
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
IPersonServiceBean personServiceBean =(IPersonServiceBean)context.getBean("personService");
//personServiceBean.save("liyong");
personServiceBean.update("xxx");
}
}
第五步:运行单元测试
..................