这可能很明显,但我很难理解为什么我们需要定义bean的类在两个地方....为什么需要在xml文件中和Spring中的getBean()方法中指定该类
从春天参考手册... ...
<bean id="petStore"
class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="itemDao" ref="itemDao"/>
<!-- additional collaborators and configuration for this bean go here -->
</bean>
// retrieve configured instance
PetStoreServiceImpl service = context.getBean("petStore", PetStoreServiceImpl.class);
不应该XML罚款是不够的容器知道petStore的类?
尽管如此,从铸造的getBean(返回值),我们只是移动类从构造函数到演员定义。 – zerayaqob
不知道你的构造函数是什么意思,但是由于Java编译器不解析应用程序上下文xml,所以需要通过调用getBean()的结果告诉它需要什么类型的对象。 – beny23