0

「紹介」の側面から Bean に動作を注入しようとしていますが、これまでのところ成功していません。
どんな助けでも大歓迎です。

「紹介」する動作:

public interface MinCalculator{
    public double min(double a,double b);
}

public class MinCalculatorImpl implements MinCalculator{
    public double min(double a,double b){
        double result=(a<b)?a:b;
        return result;
    }

}

実装クラス:

public class MathsImpl{

    public void run(){ System.out.println(" This is me ");}

    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/beans-intro.xml");
        MathsImpl test = (MathsImpl) context.getBean("MathBean");
        test.run();
        MinCalculator minI=(MinCalculator)test;
        minI.min(4,2);
    }

}

「紹介」の側面:

@Aspect
public class IntroducerAspect {
    @DeclareParents(
    value="com.aspect.MathsImpl",
    defaultImpl=MinCalculatorImpl.class)
    public MinCalculator minCalculator;
}

構成:

<aop:aspectj-autoproxy />
    <bean id="MathBean" class="com.aspect.MathsImpl" />
    <!-- Aspect -->
    <bean id="introAspect" class="com.aspect.IntroducerAspect" />

結果:

INFO: Loading XML bean definitions from class path resource [META-INF/beans-intr
o.xml]
Jul 30, 2013 10:46:32 PM org.springframework.beans.factory.support.DefaultListab
leBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.
DefaultListableBeanFactory@1d9fd51: defining beans [org.springframework.aop.conf
ig.internalAutoProxyCreator,MathBean,introAspect]; root of factory hierarchy
Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy5 c
annot be cast to com.aspect.MathsImpl
        at com.aspect.MathsImpl.main(MathsImpl.java:13)
4

1 に答える 1

1

試しproxy-target-class=trueてみる<aop:aspectj-autoproxy />

于 2013-07-30T18:20:32.150 に答える