BeanFactory
documentation
- The root interface for accessing a Spring bean container.
This is the basic client view of a bean container;
further interfaces such as {@link ListableBeanFactory} and
{@link org.springframework.beans.factory.config.ConfigurableBeanFactory}
are available for specific purposes.
BeanFactory
是spring容器的根接口。
它有兩個實現更多功能的子接口: ListableBeanFactory
和ConfigurableBeanFactory
。前者可以羅列出所有的bean實例,後者是容器內部用的,用來配置工廠。
The point of this approach is that the BeanFactory is a central registry of application components, and centralizes configuration of application components (no more do individual objects need to read properties files,for example)
BeanFactory
可以說是一個配置中心,所有組件的配置信息的管理者。於是,不會出現單個組件去讀配置文件的情況了(統一管理)。
Bean factory implementations should support the standard bean lifecycle interfaces as far as possible. The full set of initialization methods and their standard order is:
- BeanNameAware's {@code setBeanName}
- BeanClassLoaderAware's {@code setBeanClassLoader}
- BeanFactoryAware's {@code setBeanFactory}
- EnvironmentAware's {@code setEnvironment}
- EmbeddedValueResolverAware's {@code setEmbeddedValueResolver}
- ResourceLoaderAware's {@code setResourceLoader} (only applicable when running in an application context)
- ApplicationEventPublisherAware's {@code setApplicationEventPublisher} (only applicable when running in an application context)
- MessageSourceAware's {@code setMessageSource} (only applicable when running in an application context)
- ApplicationContextAware's {@code setApplicationContext} (only applicable when running in an application context)
- ServletContextAware's {@code setServletContext} (only applicable when running in a web application context)
- {@code postProcessBeforeInitialization} methods of BeanPostProcessors
- InitializingBean's {@code afterPropertiesSet}
- a custom init-method definition
- {@code postProcessAfterInitialization} methods of BeanPostProcessors
On shutdown of a bean factory, the following lifecycle methods apply:
- {@code postProcessBeforeDestruction} methods of DestructionAwareBeanPostProcessors
- DisposableBean's {@code destroy}
- a custom destroy-method definition
經常有人說bean的生命週期,在BeanFactory
的文檔中就講明白了bean的生命週期。
首先一個bean要經歷一堆xxxAware,來設置bean的初始化環境。
在初始化之前之後都要調用BeanPostProcessors
。
銷燬之前也要調用BeanPostProcessors
。
BeanFactory
只有一個屬性:
/**
* Used to dereference a {@link FactoryBean} instance and distinguish it from
* beans <i>created</i> by the FactoryBean. For example, if the bean named
* {@code myJndiObject} is a FactoryBean, getting {@code &myJndiObject}
* will return the factory, not the instance returned by the factory.
*/
String FACTORY_BEAN_PREFIX = "&";
它與FactoryBean
有關,我們後續文章將。
BeanFactory
的方法也很簡單:
API
從這些API我們可以知道,BeanFactory
是一個最基礎的容器。
由於BeanFactory
不支持註解,所以我們用xml的方式演示它的API。
bean:
package com.ocean.testBeanFactory.bean;
public class Employee {
private String name;
private Integer age;
public Employee(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
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-3.0.xsd">
<bean id="employee" class="com.ocean.testBeanFactory.bean.Employee">
<constructor-arg name="name" value="Ocean Chou"/>
<constructor-arg name="age" value="18"/>
</bean>
<alias name="employee" alias="codeman"/>
</beans>
測試:
@Test
public void createBeanfactory() {
Resource res = new ClassPathResource("factory-example.xml");
BeanFactory factory = new XmlBeanFactory(res);
Employee emp = (Employee) factory.getBean("employee");
System.out.println("does the factory contains employee codeman? : " + factory.containsBean("codeman"));
System.out.println("bean : " + factory.getBean("employee"));
System.out.println("is employee singleton? : " + factory.isSingleton("employee"));
System.out.println("is the bean the type of Employee? : " + factory.isTypeMatch("employee", Employee.class));
System.out.println("Alias : " + Arrays.asList(factory.getAliases("employee")));
結果:
does the factory contains employee codeman? : true
bean : Employee{name='Ocean Chou', age=18}
is employee singleton? : true
is the bean the type of Employee? : true
Alias : [codeman]