spring切点切面aop——xml非注解

aop代理就是到了某个方法(被称为“切点”),在执行这个方法之前干什么、之后干什么、异常了有什么等(这些在“切面”中定义)

<!-- 增加aop头,共五个 -->

<beans xmlns=http://www.springframework.org/schema/beans

           xmlns:context="http://www.springframework.org/schema/context"     

           xmlns:aop=http://www.springframework.org/schema/aop

           xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance

           xmlns:tx="http://www.springframework.org/schema/tx" 

           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/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">

 <!-- 在spring-context.jar的meta-inf//spring-context.jar中确定xsd是哪个版本 -->

<!-- <context:annotation-config></context:annotation-config> -->

 

 <!-- 告诉spring扫描哪个包,我让他扫描proxy.spring.agent所有的类-->

 

            <context:component-scan base-package="proxy.spring.agent.*"></context:component-scan>

<!-- 定义两个bean类 -->

            <bean id="myAgent" class="proxy.spring.agent.AgentProxy">

            </bean>

            <bean id="singer" class="proxy.spring.agent.MySinger">

            </bean>

<!-- 这才是正题,spring aop切点切面就要有切点和切面,下面就是切点pointcut 和切面aspect-->

            <aop:config>

 <!-- 定义一个切点 -->

<!-- execution 表达式第一个表示任意返回类型(public..),第二个表示proxy.spring.agent.MySinger下的任意方法,*(..)表示任意方法任意参数也就是这个类下的所有方法都会被代理

execution(* proxy.spring.agent.*.*(..))"表示proxy.spring.agent包下任意类的任意方法 -->                   

                 <aop:pointcut expression="execution(* proxy.spring.agent.MySinger.*(..))" id="point1"/>

 <!-- 定义一个切面 -->

                 <aop:aspect id="aspect1" ref="myAgent">

<!-- 这是定义切点之前、之后、抛异常该干什么 -->

                     <aop:before method="before" pointcut-ref="point1"/>

                     <aop:after method="after" pointcut-ref="point1" />

                     <aop:after-returning method="afterReturn" pointcut-ref="point1"/>

                     <aop:after-throwing method="afterThrow" pointcut-ref="point1"/>

                     <aop:around method="around" pointcut-ref="point1"/>

               </aop:aspect>

         </aop:config>

</beans>


 

-----------------------------------------源码例子(歌手做例子)----------------------------------

-----------------------------------------xml----------------------(歌手和经纪人之间的约定)--------------------------------

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3.   
  4. <beans xmlns="http://www.springframework.org/schema/beans"  
  5.         xmlns:context="http://www.springframework.org/schema/context"  
  6.         xmlns:aop="http://www.springframework.org/schema/aop"  
  7.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  8.          xmlns:tx="http://www.springframework.org/schema/tx"  
  9.          xsi:schemaLocation="http://www.springframework.org/schema/beans   
  10.             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  11.            
  12.            http://www.springframework.org/schema/context   
  13.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  14.              
  15.             http://www.springframework.org/schema/aop   
  16.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  17.             ">  
  18.             <!-- 在spring-context.jar的meta-inf//spring-context.jar中确定xsd是哪个版本 -->  
  19.       
  20.     <!-- <context:annotation-config></context:annotation-config> -->  
  21.     <context:component-scan base-package="proxy.spring.agent.*"></context:component-scan>  
  22.   
  23.     <bean id="myAgent" class="proxy.spring.agent.AgentProxy"></bean>  
  24.     <bean id="singer" class="proxy.spring.agent.MySinger"></bean>  
  25.   
  26.     <aop:config>  
  27.         <aop:pointcut expression="execution(* proxy.spring.agent.MySinger.*(..))" id="point1"/>  
  28.         <aop:aspect id="aspect1" ref="myAgent">  
  29.               
  30.             <aop:before method="before" pointcut-ref="point1"/>  
  31.             <aop:after method="after"   pointcut-ref="point1" />  
  32.             <aop:after-returning method="afterReturn"  pointcut-ref="point1"/>  
  33.             <aop:after-throwing method="afterThrow"   pointcut-ref="point1"/>  
  34.             <aop:around method="around"  pointcut-ref="point1"/>  
  35.         </aop:aspect>  
  36.     </aop:config>  
  37.   
  38. </beans>  


---------------------------------------------------代理类proxy.spring.agent.AgentProxy-----------------(歌手的经纪人,管着歌手的吃饭拉。。)------------------------------

[java]  view plain copy
  1. package proxy.spring.agent;  
  2.   
  3. import javax.sound.midi.SysexMessage;  
  4.   
  5. import org.aspectj.lang.ProceedingJoinPoint;  
  6.   
  7. public class AgentProxy {  
  8.       
  9.     public void before(){  
  10.         System.out.println("before()........");  
  11.     }  
  12.       
  13.     public void after(){  
  14.         System.out.println("after()......");  
  15.     }  
  16.       
  17.     public void afterThrow(){  
  18.           
  19.         System.out.println("after()......");  
  20.     }  
  21.       
  22.       
  23.     public void around(ProceedingJoinPoint p){  
  24.         //得到参数  
  25.         Object[] o= p.getArgs();  
  26.         for (int i = 0; i < o.length; i++) {  
  27.             Object object = o[i];  
  28.             System.out.println(object);  
  29.         }  
  30.           
  31.         try {  
  32.             //必须加,不加就不执行原函数  
  33.             p.proceed();  
  34.         } catch (Throwable e) {  
  35.             // TODO Auto-generated catch block  
  36.             e.printStackTrace();  
  37.         }  
  38.         System.out.println("around()....");  
  39.     }  
  40.       
  41.     public void afterReturn(){  
  42.           
  43.         System.out.println("afterReturn()......");  
  44.     }  
  45. }  


----------------------------------------------被代理类proxy.spring.agent.MySinger-----------------(著名的歌手)--------------------

[java]  view plain copy
  1. package proxy.spring.agent;  
  2.   
  3. public class MySinger {  
  4.   
  5.     public void sing(){  
  6.         System.out.println("我唱歌");  
  7.     }  
  8.       
  9.     public String ss(String name){  
  10.         System.out.println("MySinger"+"...."+name);  
  11.         return name;  
  12.     }  
  13. }  


--------------------------------测试--------main------------------

[java]  view plain copy
  1. package proxy.spring.agent;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class Test {  
  7.       
  8.     public static void main(String[] args) {  
  9.         ApplicationContext app =  
  10.             new ClassPathXmlApplicationContext("proxy/spring/agent/application.xml");  
  11.            
  12.         MySinger singer =(MySinger)app.getBean("singer");  
  13.           
  14.         singer.sing();  
  15.           
  16.         System.out.println("main"+"...."+ singer.ss("周杰伦"));  
  17.     }  
  18.           
  19. }  


--------------------结果-------------------

before()........
我唱歌
after()......
afterReturn()......
around()....
before()........
周杰伦
MySinger....周杰伦
after()......
afterReturn()......
around()....
main....null

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值