Five ways Spring instantiates Bean

I. Overview

For our Java, creating objects is nothing more than new or through reflection.

The instantiation of objects in Spring is generally achieved through reflection, but the power of Spring is not only that it provides great convenience for Java developers, but also that its open architecture allows users to have the ability to maximize Spring. .

We can roughly summarize the following five ways to instantiate beans:

  1. Implement the InstantiationAwareBeanPostProcessor interface
  2. Implement the FactoryBean interface
  3. Via Supplier interface
  4. Factory-method
  5. By reflection

Second, implement the InstantiationAwareBeanPostProcessor interface

MyInstantiationAwareBeanPostProcessor.java

public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
    
    
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
    
    
        if (beanName.equals("a")) {
    
    
            return new B();
        }
        return null;
    }
}

applicationContext.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="a" class="com.bobo.A"></bean>
    <bean class="com.bobo.MyInstantiationAwareBeanPostProcessor"></bean>
</beans>

Test code:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("boboTest.xml");
        System.out.println(context.getBean("a"));
    }
}

Run output:

com.bobo.B@490ab905

Third, implement the FactoryBean interface

MyFactoryBean.java

@Component
public class MyFactoryBean implements FactoryBean<A> {
    
    
    @Override
    public A getObject() throws Exception {
    
    
        return new A();
    }

    @Override
    public Class<?> getObjectType() {
    
    
        return A.class;
    }
}

Test code

public class Test {
    
    
    public static void main(String[] args) {
    
    
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("boboTest.xml");
        System.out.println(context.getBean("myFactoryBean"));
    }
}

Run output:

com.bobo.A@61230f6a

Principle: Please move here to view:

The truth about FactoryBean in Spring

Four, through the Supplier interface

CreateSupplierB.java

public class CreateSupplierB {
    
    
    public static B createB(){
    
    
        return new B();
    }
}

MyBeanFactoryPostProcessor.java

@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    
    
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    
    
        BeanDefinition beanDefinition = beanFactory.getBeanDefinition("a");
        GenericBeanDefinition genericBeanDefinition = (GenericBeanDefinition) beanDefinition;
        genericBeanDefinition.setInstanceSupplier(CreateSupplierB::createB);
    }
}

Test code:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("boboTest.xml");
        System.out.println(context.getBean("a"));
    }
}

Run output:

com.bobo.B@cd3fee8

Principle: please move here

Spring instantiation (createBeanInstance) source code analysis

Five, factory method factory-method

1. Static factory

MyStaticFactory.java

public class MyStaticFactory {
    
    
    public static A getA() {
    
    
        return new A();
    }
}

applicationContext.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="a" class="com.bobo.MyStaticFactory" factory-method="getA"></bean>
</beans>

test:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("boboTest.xml");
        System.out.println(context.getBean("a"));
    }
}

Run output:

com.bobo.A@5702b3b1

2. Example factory

MyInstanceFactory.java

public class MyInstanceFactory {
    
    
    public A getA() {
    
    
        return new A();
    }
}

aoolicationContext.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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="myInstanceFactory" class="com.bobo.MyInstanceFactory"></bean>
    <bean id="a" class="com.bobo.MyStaticFactory" factory-bean="myInstanceFactory" factory-method="getA"></bean>
</beans>

Test code:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("boboTest.xml");
        System.out.println(context.getBean("a"));
    }
}

Run output:

com.bobo.A@5702b3b1

3. Principle

Please move here:

Spring instantiation (createBeanInstance) source code analysis

Six, through reflection

<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="a" class="com.bobo.A"></bean>
</beans>

Test code:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("boboTest.xml");
        System.out.println(context.getBean("a"));
    }
}

Run output:

com.bobo.A@5bcab519

Guess you like

Origin blog.csdn.net/u013277209/article/details/109804892