spring认为beans是应用程序系统的骨架,且使用容器来实例,组装和管理这些beans. 这样,使得application developer专注于业务逻辑的开发和实现,无需关心系统的基础设施以及这些设施是如何通信交互的。
容器相关packages如下:
org.springframework.beans
org.springframework.context
那么,容器是怎么创建beans呢?
1). 通过xml配置文件来记录class full qualified name 以及 id
<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>
2). 通过注解的方式
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}