一Spring工厂
随着Spring 提供工厂模式的实现,在使用Spring 时,无须自己提供工厂类。因为Spring容器是最大的工厂,而且是个功能超强的工厂。Spring 使用配置文件管理所有的bean ,其配置文件中bean 由Spring 工厂负责生成和管理。
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(255, 0, 0);">仅仅面向Person的接口编程,而无需知道实现类的具体名称</span></span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">。同时,通过修改配置文件来彻底切换底层的具体实现类。</span>
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">由于厂无需多个实例,因此工厂应该采用单态模式设计,其中Spring上下文也就是Spring工厂,已被设计成单态。</span>
</span>
自动解决对象之间依赖注入关系。
简单的说,Spring就是通过工厂+反射将我们的bean放到它的容器中的,当我们想用某个bean的时候,只需要调用getBean("beanID")方法。