春季介绍人 [英] Introducer aspect in spring

查看:64
本文介绍了春季介绍人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过介绍"方面向我的bean注入行为-但到目前为止仍未成功.
任何帮助表示赞赏.

I am trying to inject behaviour to my bean through the 'Introduction' aspect - but unsuccessful so far.
Any help is appreciated.

介绍"行为:

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)

推荐答案

尝试<aop:aspectj-autoproxy />

这篇关于春季介绍人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆