preface

Instantiated by constructors instantiated by static factories instantiated by instance factories instantiated by FactoryBeans

RumenzA entity class

package com.rumenz;
public class RumenzA {

    private String id;
    private String name;
    
    public static RumenzA createRumenzA(){
        RumenzA rumenzA=new RumenzA();
        rumenzA.setId("123");
        rumenzA.setName(The Gateway Station);
        return rumenzA;
    }
    
    public  RumenzA() {
        System.out.println("RumenzA parametric construction method");
    }
    public RumenzA(String id) {
        this.id = id;
        System.out.println("ID constructor"); } / /setGet to omit}Copy the code

A constructor

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="rumenz" class="com.rumenz.RumenzA" />
<bean id="rumenz1" class="com.rumenz.RumenzA">
     <constructor-arg name="id" value="1"/>
</bean>
</beans>
Copy the code

DemoApplication.java

package com.rumenz;

import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DemoApplication {
    public static  void main(String[] args) {
        ClassPathXmlApplicationContext  ca=new ClassPathXmlApplicationContext("beans.xml");
        RumenzA rumenzA=(RumenzA)ca.getBean("rumenz"); }}Copy the code

The output

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz'RumenzA a no-parameter constructor XXX. DefaultListableBeanFactory - Creating Shared instance of singleton beans'rumenz1'ID constructorCopy the code

Static factory

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="rFactory" class="com.rumenz.RumenzFactory" factory-method="rumenzFactory"/>
<bean id="rFactory1" class="com.rumenz.RumenzFactory" factory-method="rumenzFactory">
    <constructor-arg name="id" value="111"/>
</bean>
</beans>
Copy the code

RumenzFactory factory class

package com.rumenz; Public class RumenzFactory {// static method public static RumenzArumenzFactory() {return new RumenzA();
    }
    public static RumenzA rumenzFactory(String id){
        returnnew RumenzA(id); }}Copy the code

DemoApplication.java

package com.rumenz;

import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DemoApplication {
    public static  void main(String[] args) {
        ClassPathXmlApplicationContext  ca=new ClassPathXmlApplicationContext("beans.xml"); }}Copy the code

The output

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rFactory'RumenzA a no-parameter constructor XXX. DefaultListableBeanFactory - Creating Shared instance of singleton beans'rFactory1'ID constructorCopy the code

Instance factory instantiation

<? 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="rFactory" class="com.rumenz.RumenzFactory" />
    <bean id="rumenz" factory-bean="rFactory" factory-method="rumenzFactory"></bean>
    <bean id="rumenz1" factory-bean="rFactory" factory-method="rumenzFactory">
        <constructor-arg name="id" value="666"></constructor-arg>
    </bean>
</beans>
Copy the code

RumenzFactory.java

package com.rumenz; Public class RumenzFactory {// Static method public RumenzA cannot be usedrumenzFactory() {return new RumenzA();
    }
    public  RumenzA rumenzFactory(String id){
        returnnew RumenzA(id); }}Copy the code

DemoApplication.java

package com.rumenz;

import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DemoApplication {
    public static  void main(String[] args) {
        ClassPathXmlApplicationContext  ca=new ClassPathXmlApplicationContext("beans.xml");
        //RumenzA rumenzA=(RumenzA)ca.getBean("rFactory1"); }}Copy the code

The output

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz'RumenzA a no-parameter constructor XXX. DefaultListableBeanFactory - Creating Shared instance of singleton beans'rumenz1'ID constructorCopy the code

Instantiation via FactoryBean

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="rumenz-by-factoryBean"  class="com.rumenz.RumenzAFactoryBean"/>
</beans>
Copy the code

RumenzAFactoryBean.java

package com.rumenz;

import org.springframework.beans.factory.FactoryBean;

public class RumenzAFactoryBean implements FactoryBean {
    @Override
    public Object getObject() throws Exception {
        returnRumenzA.createRumenzA(); } @Override public Class<? >getObjectType() {
        returnRumenzA.class; }}Copy the code

DemoApplication.java

package com.rumenz;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DemoApplication {
    public static  void main(String[] args) {
        ClassPathXmlApplicationContext  ca=new ClassPathXmlApplicationContext("beans.xml");
        RumenzA rumenzA=(RumenzA)ca.getBean("rumenz-by-factoryBean"); }}Copy the code

Output/asynchronously load beans

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz-by-factoryBean'RumenzA parametric construction methodCopy the code

The last

You can leave a comment below to discuss what you don’t understand. You can also pay attention to my private letter and ask me. I will answer it after I see it. Also welcome everyone to pay attention to my official account: bright future, Golden Three silver four job-hunting interview season, sorted out more than 1000 nearly 500 pages of PDF documents of Java interview questions, articles will be updated in it, sorted out the information will be placed in it. Thank you for watching, think the article is helpful to you remember to pay attention to me a thumb-up support!