梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks
Before Advice:
package com.hwp.aop.adviceDemo;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class BeforeAdviceDemo implements MethodBeforeAdvice {

    
public void before(Method method, Object[] args, Object target)
            
throws Throwable {
        System.out.println(
"在方法运行前,先运行");
    }

    
}

After Advice:
package com.hwp.aop.adviceDemo;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class AfterAdviceDemo implements AfterReturningAdvice{

    
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
            Object arg3) 
throws Throwable {
        System.out.println(
"方法执行后.");
        
    }

    
}

Round Advice:
package com.hwp.aop.adviceDemo;

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

public class AroundAdviceDemo implements MethodInterceptor {

    
public Object invoke(MethodInvocation arg0) throws Throwable {
        System.out.println(
"在round方法里,方法开始前..");
        Object result 
= null;
        
try {
            result 
= arg0.proceed();
        }
 finally {
            System.out.println(
"在round方法里,方法结束后");
        }

        
return result;
    }


}

Throw Advice:
package com.hwp.aop.adviceDemo;

import java.lang.reflect.Method;

import org.springframework.aop.ThrowsAdvice;

public class ThowAdviceDemo implements ThrowsAdvice {
    
public void afterThrowing(Method method, Object[] args, Object target,
            Throwable subclass) 
{
        System.out.println(
"异常抛出后..");
    }


}

beans.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"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
>
    
<bean id="logBeforeAdvice"
        class
="com.hwp.aop.adviceDemo.BeforeAdviceDemo" />
    
<bean id="logAfterAdvice"
        class
="com.hwp.aop.adviceDemo.AfterAdviceDemo" />
    
<bean id="logRoundAdvice"
        class
="com.hwp.aop.adviceDemo.AroundAdviceDemo" />
    
<bean id="throwAdvice"
        class
="com.hwp.aop.adviceDemo.ThowAdviceDemo" />
    
<bean id="helloSpeaker" class="com.hwp.aop.adviceDemo.HelloSpeaker" />
    
<bean id="helloProxy"
        class
="org.springframework.aop.framework.ProxyFactoryBean">
        
<property name="proxyInterfaces"
            value
="com.hwp.aop.adviceDemo.IHello" />
        
<property name="target" ref="helloSpeaker" />
        
<property name="interceptorNames">
            
<list>
                
<value>logBeforeAdvice</value>
                
<value>logAfterAdvice</value>
                
<value>logRoundAdvice</value>
                
<value>throwAdvice</value>
            
</list>
        
</property>
    
</bean>
</beans>
Demo:
package com.hwp.aop.adviceDemo;

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

public class SpringAOPDemo {
    
public static void main(String[] args){
        ApplicationContext context 
= 
            
new ClassPathXmlApplicationContext("beans-beforeAdvice.xml");
        IHello helloProxy 
= (IHello) context.getBean("helloProxy");
        
try{
            helloProxy.hello(
"惠万鹏");
        }
catch(Exception e){
            
//e.printStackTrace();
        }

    }

}

收工....

posted on 2008-04-13 14:37 HUIKK 阅读(941) 评论(0)  编辑  收藏 所属分类: Spring

只有注册用户登录后才能发表评论。


网站导航: