Spring AOP 之 IntroductionDemo

Introduction是个特别的Advice,它“影响了目标对象的行为定义,直接增加了目标对象的职责”。可通过org.springframework.aop.IntroductoinInterceptor来实现Introduction。 IntroductionInterceptor继承了MethodInterceptor与DynamicIntroductionAdvice接口,其中implementsInterfaces()方法(继承自DynamicIntroductionAdvice)如果传回true,表示目前的IntroductionInterceptor实现了规定的接口(也就是要额外增加行为的接口),此时您要使用invoke()执行接口上的方法,让目标对象执行额外的行为,您不可使用MethodInvocation的proceed()方法,因为要执行的是对象上原来没有的行为,执行proceed()方法没有意义。

 

Introduction是个特别的Advice,它“影响了目标对象的行为定义,直接增加了目标对象的职责”。可通过org.springframework.aop.IntroductoinInterceptor来实现Introduction。

 

ISome.java内容:

package onlyfun.caterpillar;

public interface ISome {
    public void doSome();
}

 

Some.java内容:

package onlyfun.caterpillar;

public class Some implements ISome {
    public void doSome() {
        System.out.println("原来对象的职责。。。");
    }
}

 

IOther.java内容:

package onlyfun.caterpillar;

public interface IOther { 
    public void doOther();
}

 

OtherIntroduction.java内容:

package onlyfun.caterpillar;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.IntroductionInterceptor;

public class OtherIntroduction
              implements IntroductionInterceptor, IOther {  
    //是否实现自IOther接口
    public boolean implementsInterface(Class clazz) {
        return clazz.isAssignableFrom(IOther.class);
    }
   
    public Object invoke(MethodInvocation methodInvocation)
                                      throws Throwable {
        //如果调用的方法来自IOther接口的定义
        if(implementsInterface(
            methodInvocation.getMethod().getDeclaringClass())) {
            //调用额外加入(mixin)的行为
            return methodInvocation.getMethod().
                    invoke(this, methodInvocation.getArguments());
        }
        else {
            return methodInvocation.proceed();
        }
    }
   
    public void doOther() {
        System.out.println("增加的职责...");
    }
}

 

applicationContext.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="some"
          class="onlyfun.caterpillar.Some"/>

    <bean id="otherIntroduction"
          class="onlyfun.caterpillar.OtherIntroduction"/>
         
    <bean id="otherAdvisor"
          class="org.springframework.aop.support.DefaultIntroductionAdvisor">
          <constructor-arg ref="otherIntroduction"/>
          <constructor-arg value="onlyfun.caterpillar.IOther"/>
    </bean>
  
    <bean id="proxyFactoryBean"
          class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces"
                  value="onlyfun.caterpillar.ISome"/>
        <property name="target" ref="some"/>
        <property name="interceptorNames">
            <list>
                <value>otherAdvisor</value>
            </list>
        </property>
    </bean>
   
</beans>

 

test.java内容:

package onlyfun.caterpillar;

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

public class test{
    public static void main(String[] args) {
        ApplicationContext context =
            new ClassPathXmlApplicationContext(
                    "beans-config.xml");
   
        ISome some =
            (ISome) context.getBean("proxyFactoryBean");
   
        some.doSome();
   
        //看来好像some对象动态增加了职责
        ((IOther) some).doOther();
    }
}

 

log4j.properties

log4j.rootLogger=WARN, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值