Bean 的实例化方式

2021-01-26

Bean 的实例化方式

  • 构造方法
  • 静态工厂
  • Bean 工厂
  • FactoryBean
  • SPI,org.springframework.beans.factory.serviceloader.ServiceLoaderFactoryBean
  • AutowireCapableBeanFactory#createBean
  • BeanDefinitionRegistry#registerBeanDefinition

...

 

xml 方式,演示实例化方式

spring 配置文件

<?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="user1" class="constxiong.User">
    	<constructor-arg name="id" value="1"></constructor-arg>
    	<constructor-arg name="name" value="constructor"></constructor-arg>
    </bean>
    
    <!-- 静态工厂方法 -->
    <bean id="user3" class="constxiong.User" factory-method="getUser"></bean>
    
    <!-- bean 工厂方法 -->
    <bean id="user2" factory-bean="userFactory" factory-method="getUser"></bean>
    <bean id="userFactory" class="constxiong.UserFactory"></bean>
    
    <!-- FactoryBean -->
    <bean id="user4" class="constxiong.UserFactoryBean"></bean>
    
    <!-- ServiceLoader -->
    <bean id="userFactoryServiceLoader" class="org.springframework.beans.factory.serviceloader.ServiceLoaderFactoryBean">
        <property name="serviceType" value="constxiong.UserFactoryInterface" />
    </bean>

</beans>

 

constxiong.UserFactoryInterface 文件放在 META-INF/services/ 目录下

constxiong.UserFactoryImpl

 

测试类:

package constxiong;

import java.util.ServiceLoader;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 测试 Bean 的实例化
 * @author ConstXiong
 */
public class Test {
	
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring-bean-instantiation.xml");
		User user1 = context.getBean("user1", User.class);
		System.out.println(user1);
		
		User user2 = context.getBean("user2", User.class);
		System.out.println(user2);
		
		User user3 = context.getBean("user3", User.class);
		System.out.println(user3);
		
		User user4 = context.getBean("user4", User.class);
		System.out.println(user4);
		
		User user5 = (User) context.getBeanFactory().createBean(User.class, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
		System.out.println(user5);
		
		// BeanDefinition
		((DefaultListableBeanFactory)context.getBeanFactory()).registerBeanDefinition("user6", BeanDefinitionBuilder.rootBeanDefinition(User.class).addPropertyValue("name", "BeanDefinition").getBeanDefinition());
		System.out.println(context.getBean("user6", User.class));
		
		// spi 方式
		ServiceLoader<UserFactoryInterface> serviceLoader = context.getBean("userFactoryServiceLoader", ServiceLoader.class);
		System.out.println(serviceLoader.iterator().next().getUser());
		// spi JDK 原生代码
		ServiceLoader<UserFactoryInterface> loader = ServiceLoader.load(UserFactoryInterface.class, Thread.currentThread().getContextClassLoader());
		System.out.println(loader.iterator().next().getUser());
	}

}

 

ConstXiong 备案号:苏ICP备16009629号-3