spring BeanFactory 实现类的getBean方法

这个方法是返回一个对象实例根据一个id或者name,这个id 或者 name 将会在配置文件里出现如:

系统会根据给出的class属性构造一个实例出来,然后强制转换:

BeanFactory factory=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
Abc abc=(Abc)factory.getBean("abc");
但有看过这样的代码:

<bean id="bean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces"> 
   <value>com.ascenttech.springaop.test.Bean</value> 
  </property> 
  <property name="target"> 
   <ref local="beanTarget"/> 
  </property> 
  <property name="interceptorNames"> 
   <list> 
    <value>theAdvisor</value> 
   </list> 
  </property>
</bean>
<bean id="theBeforeAdvice" class="com.ascenttech.springaop.test.TestBefore Advice"/>
<!--CLASS-->
<bean id="beanTarget" class="com.ascenttech.springaop.test.BeanImpl"/>
<!--ADVISOR-->
<!--Note: An advisor assembles pointcut and advice-->
<bean id="theAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> 
  <property name="advice"> 
   <ref local="theBeforeAdvice"/> 
  </property> 
  <property name="pattern"> 
   <value>com\\.ascenttech\\.springaop\\.test\\.Bean\\.theMethod</value> 
  </property>
</bean>

Bean 为自定义接口, 以及其实现类省略未给出
Bean x = (Bean) ctx.getBean("bean");
从 配置文件可以看出 id 为 bean 的是 org.springframework.aop.framework.ProxyFactoryBean ,但却试着往 Bean 转型,从一般来看这样势必产生 ClassCastException 类型转换错误,但事实却不会出错,因为getBean 方法不是简单的有构造器构造一个对象实例,而是这样: 如果 bean 的class 继承了 spring框架中的 FactoryBean 接口,那么getBean 先构造一个 bean 中class指定的对象,然后调用这个对象的 getObject 方法,getObject 方法返回一个  Object ,这个Object 就是 getBean 实际返回的东西。