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]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章