NaiveWaiter继承Waiter接口
配置文件内容是:
<aop:aspectj-autoproxy />
<bean id="waiter" class="com.baobaotao.NaiveWaiter"/>
<bean class="com.baobaotao.aspectj.aspectj.PreGreetingAspect"/>测试代码是:
String configPath="com/baobaotao/aspectj/aspectj/AspectJProxyXML.xml";
ApplicationContext ctx=new ClassPathXmlApplicationContext(configPath);
Waiter temp=(Waiter)ctx.getBean("waiter");为什么测试代码的第三行一定要返回Waiter类型呢? 为什么不能用NaiveWaiter类型。
而且Waiter类型不是一个接口嘛?

解决方案 »

  1.   

    NaiveWaitertemp=(NaiveWaiter)ctx.getBean("waiter");
    这样不行?
      

  2.   

    jdk代理的是接口,返回的类型看你注入的bean
      

  3.   

    本人测试:以下情况都可以:
    Waiter temp = ctx.getBean("waiter",Waiter.class); //得到接口的实现类。即配置的那个bean
    NaiveWaiter temp = ctx.getBean("waiter",NaiveWaiter.class);
    NaiveWaiter temp = (NaiveWaiter) ctx.getBean("waiter");不过之前也遇到过这样的情况。