ApplicationContext ctx=new FileSystemXmlApplicationContext("WebContent\WEB-INF\spring.xml");
Vt vtb=(Vt)ctx.getBean("vt");
Mr mrb=(Mr) ctx.getBean("mr");
两个类Mr和Vt是镜像的
public class Mr implements MindReader
{
private String thoughts;
public void interceptThoughts(String thoughts)
{System.out.println("hahaha");
this.thoughts=thoughts;}
public String getThoughts()
{return thoughts;}
}
public class Vt implements Thinker{
private String thoughts;
public void thinkOfSomething(String thoughts)
{
this.thoughts=thoughts;}
public String getThoughts()
{return thoughts;}
}
而且它们的接口也是镜像的
我实际调用的时候
Vt vtb=(Vt)ctx.getBean("vt"); ClassCastException报错,查了下资料代理默认创建必须要用接口类,换成Thinker vtb=(Thinker)ctx.getBean("vt");
问题解决了。
但是Mr mrb=(Mr) ctx.getBean("mr");却没有任何异常可以正常使用,这是怎么回事?