Spring源码-Bean实例化
程序员文章站
2022-05-21 19:18:24
...
Bean实例化
常规方式:
- 通过构造器(配置元信息:XML,Java注解和JavaAPI)
- 静态工厂方法(XML和JavaAPI)
- Bean工厂(XML和JavaAPI)
- 通过FactoryBean (XML,Java注解和JavaAPI)
特殊方式:
- ServiceLoaderFactoryBean 配置元信息:XML,Java注解和JavaAPI)
- AutowireCapableBeanFactory#createBean
- BeanDefinitionRegistry#registerBeanDefinition
静态方法XML例子
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean-instance.xml");
User bean = applicationContext.getBean(User.class);
System.out.println(bean);
}
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" name="@3assdf;asd" class="org.example.beans.domain.User"
factory-method="createUser"
/>
</beans>
静态工厂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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user_by_factory" factory-bean="userFactory" factory-method="createUser"/>
<bean id="userFactory" class="org.example.beans.factory.DefaultUserFactory"/>
</beans>
public interface UserFactory {
default User createUser (){
return User.createUser();
}
}
public class DefaultUserFactory implements UserFactory{
}
FactoryBean 例子
<bean id="userFactoryBean" class="org.example.beans.factory.UserFactoryBean"/>
public class UserFactoryBean implements FactoryBean {
@Override
public Object getObject() throws Exception {
return User.createUser();
}
@Override
public Class<?> getObjectType() {
return User.class;
}
}
Bean 的特殊实例化方式:
ServiceLoader:
资源目录配置该文件
META-INF/services/org.example.beans.factory.UserFactory
org.example.beans.factory.DefaultUserFactory
<bean id="userFactoryServiceLoader" class="org.springframework.beans.factory.serviceloader.ServiceLoaderFactoryBean">
<property name="serviceType" value="org.example.beans.factory.UserFactory"/>
</bean>
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean-instance.xml");
ServiceLoader<UserFactory> bean = applicationContext.getBean("userFactoryServiceLoader", ServiceLoader.class);
displayServiceLoader(bean);
}
public static void displayServiceLoader(ServiceLoader<UserFactory> factories) {
Iterator<UserFactory> iterator = factories.iterator();
while (iterator.hasNext()) {
UserFactory userFactory = iterator.next();
System.out.println(userFactory.createUser());
}
}
AutowireCapableBeanFactory 例子
AutowireCapableBeanFactory autowireCapableBeanFactory = applicationContext.getAutowireCapableBeanFactory();
autowireCapableBeanFactory.createBean(DefaultUserFactory.class);
推荐阅读
-
Bean实例化的三种方式
-
Spring bean 加载执行顺序实例解析
-
spring5 源码深度解析----- 被面试官给虐懵了,竟然是因为我不懂@Configuration配置类及@Bean的原理
-
基于Spring注解的上下文初始化过程源码解析(一)
-
通过实例解析spring bean之间的关系
-
spring源码分析系列5:ApplicationContext的初始化与Bean生命周期
-
spring源码分析6: ApplicationContext的初始化与BeanDefinition的搜集入库
-
Spring启动后获取所有拥有特定注解的Bean实例代码
-
Spring MVC源码(一) ----- 启动过程与组件初始化
-
02Spring基于xml的IOC配置--实例化Bean的三种方式