基于基于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" 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>



准备工作:

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" class="com.liyong.serviceBean.Imp.PersonServiceBean"/>
<!-- <bean id="myItercepter" class="com.liyong.Itecepter.MyItercepter"/> -->
<bean id="myItercepter" class="com.liyong.Itecepter.MyItercepterByXml"/>
<aop:config>
<aop:aspect id="myaspect" 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");
}

}

第五步:运行单元测试

..................
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青年IT男

您的打赏就是对我的肯定!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值