首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring AOP概念学习(2)-单独代理

2012-10-30 
Spring AOP概念学习(二)-单独代理Spring AOPAdvicesadvice1advice BeanAdvice Beanpackage com.javaeye.su

Spring AOP概念学习(二)-单独代理

      这几天一直没有时间写博客,今天正好抽空,现在继续上一篇文章,来整理一下个人心得,希望可以给初学者帮助,即使没有帮助,也希望大家共同交流。

      下面开始进入正题,这篇文章主要讲解的是有关于Spring AOP中的Advices的实现,首先介绍的是通过接口实现的advice,其余实现将在后面几篇文章中介绍。

      1、定义基本的advice Bean以及要被调用的类

 

    定义Advice Bean

package com.javaeye.sunjiesh.aop;import java.lang.reflect.Method;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;import org.springframework.aop.AfterReturningAdvice;import org.springframework.aop.MethodBeforeAdvice;import org.springframework.aop.ThrowsAdvice;public class AdviceTest implements MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor,ThrowsAdvice{/** * 调用方法之前执行 * Before Advice */@Overridepublic void before(Method arg0, Object[] arg1, Object arg2)throws Throwable {System.out.println("调用before");}/** * 调用方法之后执行 * After Advice */@Overridepublic void afterReturning(Object arg0, Method arg1, Object[] arg2,Object arg3) throws Throwable {System.out.println("调用afterReturning");}/** * Around Advice */@Overridepublic Object invoke(MethodInvocation arg0) throws Throwable {Object result=null;System.out.println("around 调用before之前");result=arg0.proceed();System.out.println("around 调用afterReturning之后");return result;}public void afterThrowing(Method method,Object[] args,Object target,Throwable subclass) {    System.out.println("ThrowAdvice 记录异常...........");    }}

 

    接口类

package com.javaeye.sunjiesh.proxy;public interface IHello {void hello();void helloA(String a);void helloB(String b);}

 

    实现类

package com.javaeye.sunjiesh.proxy;public class HelloImpl implements IHello {@Overridepublic void hello() {// TODO Auto-generated method stubSystem.out.println("hello in HelloImpl");}@Overridepublic void helloA(String a) {// TODO Auto-generated method stubSystem.out.println("helloA in HelloImpl,parameter is "+a);}@Overridepublic void helloB(String b) {// TODO Auto-generated method stubSystem.out.println("helloB in HelloImpl,parameter is "+b);}}
 

      2、在配置文件中声明我们的bean(文件名:spring-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"xmlns:context="http://www.springframework.org/schema/context"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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- 代理 --><bean id="helloImpl" /><!-- 基础的通知使用,这里强行给helloSpeaker1加上了advice通知 --><bean id="aaa" ref="helloImpl"></property><property name="interceptorNames"><list><value>adviceTest</value></list></property></bean></beans>

 

    3、在main函数中调用

package com.javaeye.sunjiesh.aop;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.javaeye.sunjiesh.proxy.IHello;public class AopMain {/** 对于给基本的spring bean强行指定一批advice方法的调用展示 **/public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-beans.xml");IHello h = (IHello) ctx.getBean("aaa");h.helloA("测试一");System.out.println("======开始进行测试二=========");try {h.helloB("测试二");} catch (Exception e) {e.printStackTrace();}}}
 

    4、控制台的显示

around 调用before之前调用beforehelloA in HelloImpl,parameter is 测试一调用afterReturningaround 调用afterReturning之后======开始进行测试二=========around 调用before之前调用beforehelloB in HelloImpl,parameter is 测试二调用afterReturningaround 调用afterReturning之后
 

    备注:如果Advice Bean实现ThrowsAdvice接口,则一定要实现afterThrowing方法,否则会报错。

热点排行