注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

清风幻影的博客

Where there is love, I will be there.

 
 
 

日志

 
 
关于我

同是天涯沦落人,相逢何必曾相识. 天生我材必有用,千金散尽还复来. 天若有情天亦老,人间正道是沧桑. 月影西斜人已去, 堤上梅花情依旧, 此情故已成追忆, 美人如玉夜留香

网易考拉推荐

Spring AOP 示例二  

2013-05-20 09:21:44|  分类: spring_1资料 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
http://joe5456536.blog.163.com/blog/static/8537477320112119372998/

ApplicationContext.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
 <bean id="helloWorld" class="com.demo.spring.test.HelloWorld"/>
 
 <bean id="myBeforeAdvice" class="com.demo.spring.aop.MyBeforeAdvice"/>
 <bean id="myAroundAdvice" class="com.demo.spring.aop.MyAroundAdvice"/>
 <bean id="myThrowsAdvice" class="com.demo.spring.aop.MyThrowsAdvice"/>
 <bean id="myAfterAdvice" class="com.demo.spring.aop.MyAfterAdvice"/>
 
 
 <bean id="myBeforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
          <property name="patterns">
                   <list>
                            <value>com\.demo\.spring\.test\.IHelloWorld\.execute</value>
                   </list>
          </property>
          <property name="advice">
                     <ref local="myBeforeAdvice" />
          </property>
 </bean>
 <bean id="myAroundAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
            <property name="patterns">
                      <list>
                             <value>com\.demo\.spring\.test\.IHelloWorld\.execute</value>
                     </list>
           </property>
           <property name="advice">
                      <ref local="myAroundAdvice" />
           </property>
 </bean>
 <bean id="myThrowsAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
              <property name="patterns">
                    <list>
                             <value>com\.demo\.spring\.test\.IHelloWorld\.execute</value>
                    </list>
             </property>
             <property name="advice">
                           <ref local="myThrowsAdvice" />
             </property>
 </bean>
 <bean id="myAfterAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
             <property name="patterns">
                       <list>
                                <value>com\.demo\.spring\.test\.IHelloWorld\.execute</value>
                       </list>
             </property>
             <property name="advice">
                         <ref local="myAfterAdvice" />
             </property>
 </bean>
 
 <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
              <property name="proxyInterfaces" value="com.demo.spring.test.IHelloWorld"/>
              <property name="target" ref="helloWorld"/>
              <property name="interceptorNames">
                        <list>
                              <value>myBeforeAdvisor</value>
                              <value>myAfterAdvisor</value>
                              <value>myAroundAdvisor</value>
                              <value>myThrowsAdvisor</value>
                       </list>
              </property>
 </bean>
</beans>

Spring 里的通知类

前置通知:

package com.demo.spring.aop;

import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;

public class MyBeforeAdvice implements MethodBeforeAdvice {

              public void before(Method arg0, Object[] args, Object target) throws Throwable {

                             System.out.println("方法名:" + arg0.getName());
                             System.out.println("参数个数:" + arg1.length);
                             System.out.println("目标类名:" + target);
                             System.out.println("Start to say:");

              }

}

环绕通知:

package com.demo.spring.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class MyAroundAdvice implements MethodInterceptor {

             public Object invoke(MethodInvocation arg0) throws Throwable {

                             System.out.println("正在执行!");
                             System.out.println("代理类名:" + arg0.getClass().getName());
                             System.out.println("目标类名:" + arg0.getThis().getClass().getName());
                             System.out.println("参数个数:" + arg0.getArguments().length);
                             System.out.println("目标方法名:" + arg0.getMethod());
                             return arg0.proceed();
             }
}

返回后通知:

package com.demo.spring.aop;

import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;

public class MyAfterAdvice implements AfterReturningAdvice {

               public void afterReturning(Object returnVal, Method arg1, Object[] args, Object target) throws Throwable {
                        System.out.println("END!");
               }
}

异常通知:

package com.demo.spring.aop;

import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;

public class MyThrowsAdvice implements ThrowsAdvice {

               public void afterThrowing(Method m, Object[] args, Object target, Exception ex) throws Throwable{
                              System.out.println("有异常抛出!");
               }
}

接口类:

package com.demo.spring.test;

public interface IHelloWorld {

           public void execute(String name) throws Exception;
}

接口实现类

package com.demo.spring.test;

import java.io.IOException;

public class HelloWorld implements IHelloWorld {

           public void execute(String name) throws Exception{
                       System.out.println("Hello World!");
                       //if(true) throw new IOException();
           }

}

测试类

package com.demo.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Test {

           public static void main(String[] args) {
                   ApplicationContext ctx = new FileSystemXmlApplicationContext("/applicationContext.xml");
                   IHelloWorld hello = (IHelloWorld) ctx.getBean("proxy");
                   try{
                         hello.execute("JOE");
                   }catch(Exception e){
                           e.printStackTrace();
                   } 
          }
}

  评论这张
 
阅读(521)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018