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]