zoukankan      html  css  js  c++  java
  • Spring的xml文件配置方式实现AOP

    配置文件与注解方式的有很大不同,多了很多配置项。

    beans2.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 />
        <bean id="personService" class="test.spring.service.impl.PersonServiceBean"></bean>
        <bean id="myInterceptor" class="test.spring.aop.MyInterceptor2"></bean>
        <aop:config>
               <aop:aspect id="myAspect" ref="myInterceptor">
                        <aop:pointcut  id="myPointCut"  expression="execution(* test.spring.service.impl.PersonServiceBean.*(..))" />
                        <aop:before pointcut-ref="myPointCut" method="doAccessCheck" />
                        <aop:after-returning pointcut-ref="myPointCut"  method="doAfterReturning" />
                        <aop:after-throwing pointcut-ref="myPointCut"  method="doAfterThrowing" />
                        <aop:around pointcut-ref="myPointCut" method="doAround" />
                        <aop:after pointcut-ref="myPointCut" method="doAfter" />
               </aop:aspect>
        </aop:config>
    </beans> 

          切面的切入点语法定义

    • 拦截test.spring.service.impl.PersonServiceBean下的所有方法
      expression="execution(* test.spring.service.impl.PersonServiceBean.*(..))"
    • 拦截test.spring.service.impl子包下的所有类的所有方法
      expression="execution(* test.spring.service.impl..*.*(..))"
    • 拦截test.spring.service.impl.PersonServiceBean下的所有返回值为String类型的方法
      expression="execution(java.lang.String test.spring.service.impl.PersonServiceBean.*(..))"
    • 拦截test.spring.service.impl.PersonServiceBean下的所有方法中第一个参数类型为String的方法
      expression="execution(* test.spring.service.impl.PersonServiceBean.*(java.lang.String,..))"
    [java] view plain copy
     
    1. package test.spring.service.impl;  
    2.   
    3. import test.spring.service.PersonService;  
    4.   
    5. //代理对象实现目标对象所有接口  
    6. public class PersonServiceBean implements PersonService {  
    7.   
    8.     public PersonServiceBean() {  
    9.   
    10.     }  
    11.   
    12.     @Override  
    13.     public void save(String name) {  
    14.         System.out.println("save()->>" + name);  
    15.         throw new RuntimeException(">>----自定义异常----<<");  
    16.     }  
    17.   
    18.     @Override  
    19.     public String getResult() {  
    20.         return "getResult()==>>返回结果";  
    21.     }  
    22.   
    23. }  

    [java] view plain copy
     
    1. package test.spring.aop;  
    2.   
    3. import org.aspectj.lang.ProceedingJoinPoint;  
    4.   
    5. public class MyInterceptor2 {  
    6.   
    7.     public void doAccessCheck() {  
    8.         System.out.println("前置通知-->>");  
    9.     }  
    10.   
    11.     public void doAfterReturning() {  
    12.         System.out.println("后置通知-->>");  
    13.     }  
    14.   
    15.     public void doAfter() {  
    16.         System.out.println("最终通知");  
    17.     }  
    18.   
    19.     public void doAfterThrowing() {  
    20.         System.out.println("异常通知-->");  
    21.     }  
    22.   
    23.     public Object doAround(ProceedingJoinPoint pJoinPoint) throws Throwable {  
    24.         System.out.println("环绕通知");  
    25.         // 这里如果pJoinPoint.proceed()不执行,后面拦截到的方法都不会执行,非常适用于权限管理  
    26.         Object result = pJoinPoint.proceed();  
    27.         System.out.println("退出");  
    28.         return result;  
    29.     }  
    30.   
    31. }  

    [java] view plain copy
     
    1. package test.spring.junit;  
    2.   
    3. import org.junit.Test;  
    4. import org.springframework.context.support.AbstractApplicationContext;  
    5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    6.   
    7. import test.spring.service.PersonService;  
    8.   
    9. public class AOPTest3 {  
    10.   
    11.     @Test  
    12.     public void test() {  
    13.         AbstractApplicationContext aContext = //  
    14.         new ClassPathXmlApplicationContext("beans2.xml");  
    15.         PersonService pService = (PersonService) aContext  
    16.                 .getBean("personService");  
    17.         pService.save("LinDL");  
    18.         pService.getResult();  
    19.         aContext.close();  
    20.     }  
    21.   
    22. }  

     
     
  • 相关阅读:
    Jedis测试redis
    jedis池的作用
    错误
    Ceph剖析:数据分布之CRUSH算法与一致性Hash
    drools规则引擎初探
    Techniques for HA IT Management
    django_simple_captcha使用笔记
    微服务架构的理论基础
    分布式系统服务的稳定性
    四层、七层负载均衡的区别
  • 原文地址:https://www.cnblogs.com/hoobey/p/6106272.html
Copyright © 2011-2022 走看看