BeanFactory簡介

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容器的根接口。

它有兩個實現更多功能的子接口: ListableBeanFactoryConfigurableBeanFactory。前者可以羅列出所有的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:

  1. BeanNameAware's {@code setBeanName}
  2. BeanClassLoaderAware's {@code setBeanClassLoader}
  3. BeanFactoryAware's {@code setBeanFactory}
  4. EnvironmentAware's {@code setEnvironment}
  5. EmbeddedValueResolverAware's {@code setEmbeddedValueResolver}
  6. ResourceLoaderAware's {@code setResourceLoader} (only applicable when running in an application context)
  7. ApplicationEventPublisherAware's {@code setApplicationEventPublisher} (only applicable when running in an application context)
  8. MessageSourceAware's {@code setMessageSource} (only applicable when running in an application context)
  9. ApplicationContextAware's {@code setApplicationContext} (only applicable when running in an application context)
  10. ServletContextAware's {@code setServletContext} (only applicable when running in a web application context)
  11. {@code postProcessBeforeInitialization} methods of BeanPostProcessors
  12. InitializingBean's {@code afterPropertiesSet}
  13. a custom init-method definition
  14. {@code postProcessAfterInitialization} methods of BeanPostProcessors

On shutdown of a bean factory, the following lifecycle methods apply:

  1. {@code postProcessBeforeDestruction} methods of DestructionAwareBeanPostProcessors
  2. DisposableBean's {@code destroy}
  3. 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]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章