Spring AOP 之 DelegatingIntroductionDemo

平台:Spring2.5

 

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

 

IntroductionInterceptor继承了MethodInterceptor与DynamicIntroductionAdvice接口,其中implementsInterfaces()方法(继承自DynamicIntroductionAdvice)如果传回true,表示目前的IntroductionInterceptor实现了规定的接口(也就是要额外增加行为的接口),此时您要使用invoke()执行接口上的方法,让目标对象执行额外的行为,您不可使用MethodInvocation的proceed()方法,因为要执行的是对象上原来没有的行为,执行proceed()方法没有意义。

 

org.springframework.aop.support.DelegatingIntroductionInterceptor是SpringAOP中为IntroductionInterceptor接口所提供的实现类。

 

ILockable.java内容:

package onlyfun.caterpillar;

public interface ILockable {
    public void lock();
    public void unlock();
    public boolean isLocked();
}

 

LockIntroduction.java内容:

package onlyfun.caterpillar;

import org.springframework.aop.
             support.DelegatingIntroductionInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.
             framework.AopConfigException;

public class LockIntroduction
              extends DelegatingIntroductionInterceptor
              implements ILockable {
    private boolean locked;

    public Object invoke(MethodInvocation invocation)
                       throws Throwable {
        // locked为true下不能调用set方法
        if (isLocked() &&
            invocation.getMethod().
                       getName().indexOf("set") == 0) {
            throw new AopConfigException(
                                "对象被锁定!!");
        }
       
        return super.invoke(invocation);
    }
   
    public void lock() {
        locked = true;
    }

    public void unlock() {
        locked = false;
    }

    public boolean isLocked() {
        return locked;
    }
}

 

ISome.java内容:

package onlyfun.caterpillar;

public interface ISome {
    public void setSome(String some);
    public String getSome();
}

 

Some.java内容:

package onlyfun.caterpillar;

public class Some implements ISome {
    private String some;
   
    public void setSome(String some) {
        this.some = some;
    }
   
    public String getSome() {
        return some;
    }
}

 

application.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="lockIntroduction"
          class="onlyfun.caterpillar.LockIntroduction"/>
         
    <bean id="lockAdvisor"
       class="org.springframework.aop.support.DefaultIntroductionAdvisor">
          <constructor-arg ref="lockIntroduction"/>
          <constructor-arg value="onlyfun.caterpillar.ILockable"/>
    </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>lockAdvisor</value>
            </list>
        </property>
    </bean>
   
</beans>

 

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

 

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) throws Exception {
        ApplicationContext context =
                new ClassPathXmlApplicationContext(
                        "beans-config.xml");
       
        ISome some =
            (ISome) context.getBean("proxyFactoryBean");
       
        //对象没有被锁定,可以调用set方法
        some.setSome("justin");
        System.out.println(some.getSome());
       
        try {
            //对象被锁定
            ((ILockable) some).lock();
           
            //无法调用set方法,抛出异常
            some.setSome("momor");
            //由于会抛出异常,所以下面的这行程序无法被执行
            System.out.println(some.getSome());
        }
        catch(Throwable e) {
            e.printStackTrace();
        }
       
        // Object is unlocked.
        ((ILockable) some).unlock();
        // It's ok to use setter again.
        some.setSome("momor");
        System.out.println(some.getSome());
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值