spring aop编程——基于XML

spring aop编程——基于XML开发
基于xml
1.目标类:接口 + 实现
      接口代码:
      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 "张三";
}
    }
2.切面类:编写多个通知,采用aspectj 通知名称任意(方法名任意)
     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("最终通知..............");
}
}

3.aop编程,将通知应用到目标类
      3.1Spring配置
      <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">
    <!-- 1 创建目标类 -->
    <bean id="userService" class="cn.ithuplion.spring_aop_xml.UserServiceImpl"></bean>
    <!-- 2 创建切面类(通知) -->
    <bean id="myAspect" class="cn.ithuplion.spring_aop_xml.MyAspect"></bean>
     <!-- 3 aop编程
<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"/>

          <!-- 3.1 前置通知 :目标方法前执行
<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"/>
        <!-- 3.2后置通知 :目标方法后执行,获得返回值
             <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"/>
        <!-- 3.3 环绕通知
     <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"/>
       <!-- 3.4 抛出异常
    <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"/>
        <!-- 3.5 最终通知 -->
        <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();
}

猜你喜欢

转载自ithuplion.iteye.com/blog/2373004