前置准备工作
新建配置文件supplier.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.xsd">
<bean id="user" class="com.mashibing.supplier.User"></bean>
<bean class="com.mashibing.supplier.SupplierBeanFactoryPostProcessor"></bean>
</beans>
public class User {
private String username;
public User() {
}
public User(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
'}';
}
}
**新建SupplierBeanFactoryPostProcessor **
实现BFPP接口,在invokeBfpp调用这个方法修改bean定义信息,设置InstanceSupplier回调。
public class SupplierBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
BeanDefinition user = beanFactory.getBeanDefinition("user");
GenericBeanDefinition genericBeanDefinition = (GenericBeanDefinition) user;
genericBeanDefinition.setInstanceSupplier(CreateSupplier::createUser);
genericBeanDefinition.setBeanClass(User.class);
}
}
public class CreateSupplier {
public static User createUser(){
return new User("zhangsan");
}
}
方法执行
public class TestSupplier {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("supplier.xml");
User bean = ac.getBean(User.class);
System.out.println(bean.getUsername());
}
}
开始创建对象
执行调用链
obtainFromSupplier:1517, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createBeanInstance:1431, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
doCreateBean:675, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createBean:630, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
lambda$doGetBean$0:417, AbstractBeanFactory (org.springframework.beans.factory.support)
getObject:-1, 1989184704 (org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$8)
getSingleton:370, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support)
doGetBean:414, AbstractBeanFactory (org.springframework.beans.factory.support)
getBean:260, AbstractBeanFactory (org.springframework.beans.factory.support)
preInstantiateSingletons:993, DefaultListableBeanFactory (org.springframework.beans.factory.support)
finishBeanFactoryInitialization:1024, AbstractApplicationContext (org.springframework.context.support)
refresh:614, AbstractApplicationContext (org.springframework.context.support)
<init>:150, ClassPathXmlApplicationContext (org.springframework.context.support)
<init>:87, ClassPathXmlApplicationContext (org.springframework.context.support)
main:9, TestSupplier (com.mashibing.supplier)
因为这段配置,在执行到上面这里会跳转到这里执行:
genericBeanDefinition.setInstanceSupplier(CreateSupplier::createUser);
实例化对象到这里执行结束!