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

来源:互联网 发布:拓扑图制作软件 mac 编辑:程序博客网 时间:2024/06/01 04:00

 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----------------------(歌手和经纪人之间的约定)--------------------------------

<?xml version="1.0" encoding="UTF-8"?><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/aop            http://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> --><context:component-scan base-package="proxy.spring.agent.*"></context:component-scan><bean id="myAgent" class="proxy.spring.agent.AgentProxy"></bean><bean id="singer" class="proxy.spring.agent.MySinger"></bean><aop:config><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>


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

package proxy.spring.agent;import javax.sound.midi.SysexMessage;import org.aspectj.lang.ProceedingJoinPoint;public class AgentProxy {public void before(){System.out.println("before()........");}public void after(){System.out.println("after()......");}public void afterThrow(){System.out.println("after()......");}public void around(ProceedingJoinPoint p){//得到参数Object[] o= p.getArgs();for (int i = 0; i < o.length; i++) {Object object = o[i];System.out.println(object);}try {//必须加,不加就不执行原函数p.proceed();} catch (Throwable e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("around()....");}public void afterReturn(){System.out.println("afterReturn()......");}}


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

package proxy.spring.agent;public class MySinger {public void sing(){System.out.println("我唱歌");}public String ss(String name){System.out.println("MySinger"+"...."+name);return name;}}


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

package proxy.spring.agent;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext app =new ClassPathXmlApplicationContext("proxy/spring/agent/application.xml"); MySinger singer =(MySinger)app.getBean("singer");singer.sing();System.out.println("main"+"...."+ singer.ss("周杰伦"));}}


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

before()........
我唱歌
after()......
afterReturn()......
around()....
before()........
周杰伦
MySinger....周杰伦
after()......
afterReturn()......
around()....
main....null
-----------------------------------------------------------下篇文章用注解

原创粉丝点击