BeanFactory:
其實可以理解爲工廠,他的作用就是把所有的對象,統一交給spring管理。
如果你模擬過xml方式實現spring的ioc功能,就知道我們把所有需要spring管理的類,都需要配置到xml中;
例如:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="demoController" class="com.wqm._02annotation.controller.DemoController">
<property name="demoService" ref="demoService"></property>
</bean>
<bean id="demoService" class="com.wqm._02annotation.service.impl.DemoServiceImpl"></bean>
</beans>
如果你模擬過用註解的方式,實現spring的ioc功能,也就會知道註解的作用,其實就只是標識
@Serivce
@Controller
@Component
等等
他們最終都調用了BeanFactory接口,提供的getBean方法;
不管是xml還是註解的方式,他們都是把需要管理的類,先獲取到,然後將這些類存儲起來,再通過靜態代理的方式,爲這些類,提供一個統一的方法。而提供統一的規範的這個類,就叫BeanFactory。