Chinaunix首页 | 论坛 | 博客
  • 博客访问: 477596
  • 博文数量: 155
  • 博客积分: 2954
  • 博客等级: 少校
  • 技术积分: 1000
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-12 22:00
文章分类

全部博文(155)

文章存档

2014年(2)

2013年(5)

2012年(10)

2011年(33)

2010年(105)

我的朋友

分类: Java

2011-09-25 16:31:59

http://joe5456536.blog.163.com/blog/static/8537477320112119372998/



ApplicationContext.xml 文件


"">

 helloWorld" class="com.demo.spring.test.HelloWorld"/>
 
 myBeforeAdvice" class="com.demo.spring.aop.MyBeforeAdvice"/>
 myAroundAdvice" class="com.demo.spring.aop.MyAroundAdvice"/>
 myThrowsAdvice" class="com.demo.spring.aop.MyThrowsAdvice"/>
 myAfterAdvice" class="com.demo.spring.aop.MyAfterAdvice"/>
 
 
 myBeforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
          patterns">
                   
                            com\.demo\.spring\.test\.IHelloWorld\.execute
                   

         

          advice">
                     myBeforeAdvice" />
         

 

 myAroundAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
            patterns">
                     
                             com\.demo\.spring\.test\.IHelloWorld\.execute
                    

           

           advice">
                      myAroundAdvice" />
          

 

 myThrowsAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
              patterns">
                   
                             com\.demo\.spring\.test\.IHelloWorld\.execute
                   

             

             advice">
                           myThrowsAdvice" />
            

 

 myAfterAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
             patterns">
                      
                                com\.demo\.spring\.test\.IHelloWorld\.execute
                      

            

             advice">
                         myAfterAdvice" />
            

 

 
 proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
              proxyInterfaces" value="com.demo.spring.test.IHelloWorld"/>
              target" ref="helloWorld"/>
              interceptorNames">
                       
                              myBeforeAdvisor
                              myAfterAdvisor
                              myAroundAdvisor
                              myThrowsAdvisor
                      

             

 

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(truethrow 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();
                   } 
          }
}

阅读(996) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~