spring aop编程——基于XML

[size=medium][color=black]spring aop编程——基于XML开发[/color][/size]
[color=black]基于xml[/color]
[color=red]1.目标类:接口 + 实现[/color]
接口代码:
public interface UserService {
public String findUser();
public void addUser();
public void updateUser();
public void deleteUser();
}
实现类:
public class UserServiceImpl implements UserService{
public void addUser() {
System.out.println("addUser............");
}
public void updateUser() {
System.out.println("updateUser...........");
}
public void deleteUser() {
System.out.println("deleteUser.............");
}
public String findUser() {
System.out.println("findUser.............");
return "张三";
}
}
[color=red]2.切面类:编写多个通知,采用aspectj 通知名称任意(方法名任意)[/color]
public class MyAspect {
//前置通知
public void myBefore(){
System.out.println("开启事务..........");
}
//后置通知
public void myAfterReturning(Object o){
System.out.println("提交事务..........."+o);
}
//环绕通知
public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("开启事务..........");
//手动执行目标方法
Object obj = joinPoint.proceed();
System.out.println("提交事务...........");
return obj;
}
//抛出异常通知
public void myAfterThrowing(Throwable t){
System.out.println("抛出异常通知..........."+t.getMessage());
}
//最终通知
public void myAfter(){
System.out.println("最终通知..............");
}
}

[b][color=red]3.aop编程,将通知应用到目标类[/color][/b]
[color=blue]3.1Spring配置[/color]
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
[color=green]<!-- 1 创建目标类 -->[/color]
<bean id="userService" class="cn.ithuplion.spring_aop_xml.UserServiceImpl"></bean>
[color=green]<!-- 2 创建切面类(通知) -->[/color]
<bean id="myAspect" class="cn.ithuplion.spring_aop_xml.MyAspect"></bean>
[color=green]<!-- 3 aop编程 [/color]
<aop:aspect> 将切面类 声明“切面”,从而获得通知(方法)
ref 切面类引用
<aop:pointcut> 声明一个切入点,所有的通知都可以使用。
expression 切入点表达式
id 名称,用于其它通知引用
-->
<aop:config>
<aop:aspect ref="myAspect">
<aop:pointcut expression="execution(* cn.ithuplion.spring_aop_xml.UserServiceImpl.*(..))" id="myPointcut"/>

[color=green]<!-- 3.1 前置通知 :目标方法前执行[/color]
<aop:before method="" pointcut="" pointcut-ref=""/>
method : 通知,及方法名
pointcut :切入点表达式,此表达式只能当前通知使用。
pointcut-ref : 切入点引用,可以与其他通知共享切入点。
通知方法格式:public void myBefore(JoinPoint joinPoint){
参数1:org.aspectj.lang.JoinPoint 用于描述连接点(目标方法),获
得目标方法名等
-->
<aop:before method="myBefore" pointcut-ref="myPointcut"/>
[color=green]<!-- 3.2后置通知 :目标方法后执行,获得返回值[/color]
<aop:after-returning method="" pointcut-ref="" returning=""/>
returning 通知方法第二个参数的名称
通知方法格式:public void myAfterReturning(JoinPoint joinPoint,Object
returnValue){
参数1:连接点描述
参数2:类型Object,参数名 returning="returnValue" 配置的
-->
<aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut" returning="o"/>
[color=green] <!-- 3.3 环绕通知 [/color]
<aop:around method="" pointcut-ref=""/>
通知方法格式:public Object myAround(ProceedingJoinPoint joinPoint)
throws Throwable{
返回值类型:Object
方法名:任意
参数:org.aspectj.lang.ProceedingJoinPoint
抛出异常
执行目标方法:Object obj = joinPoint.proceed();
-->
<aop:around method="myAround" pointcut-ref="myPointcut"/>
[color=green] <!-- 3.4 抛出异常[/color]
<aop:after-throwing method="" pointcut-ref="" throwing=""/>
throwing :通知方法的第二个参数名称
通知方法格式:public void myAfterThrowing(JoinPoint joinPoint,Throwable t)
{
参数1:连接点描述对象
参数2:获得异常信息,类型Throwable ,参数名由throwing="t" 配置
-->
<aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointcut" throwing="t"/>
[color=green]<!-- 3.5 最终通知 -->[/color]
<aop:after method="myAfter" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
</beans>

4.测试
@Test
public void test1(){
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("cn/ithuplion/spring_aop_xml/bean.xml");
UserService userService = (UserService)
applicationContext.getBean("userService");
userService.addUser();
userService.updateUser();
userService.deleteUser();
userService.findUser();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值