Spring Bean初始化的几种常规方式

  • 通过构造方法实例化
  • 通过静态工厂实例化
  • 通过实例工厂实例化
  • 通过FactoryBean实例化

RumenzA实体类

package com.rumenz;
public class RumenzA {

    private String id;
    private String name;
    public  RumenzA() {
        System.out.println("RumenzA 无参构造方法");
    }
    public RumenzA(String id) {
        this.id = id;
        System.out.println("ID构造方法");
    }
    // set get省略
}

构造方法

beans.xml





     

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");
    }

}

输出

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz'
RumenzA 无参构造方法
xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz1'
ID构造方法

静态工厂

beans.xml






    

RumenzFactory工厂类

package com.rumenz;
public class RumenzFactory {
    
    //静态方法
    public static RumenzA rumenzFactory(){
        return new RumenzA();
    }
    public static RumenzA rumenzFactory(String id){
        return new RumenzA(id);
    }

}

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");
    }

}

输出

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rFactory'
RumenzA 无参构造方法
xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rFactory1'
ID构造方法

实例工厂实例化



    
    
    
        
    

RumenzFactory.java


package com.rumenz;
public class RumenzFactory {
    //不能用静态方法
    public  RumenzA rumenzFactory(){
        return new RumenzA();
    }
    public  RumenzA rumenzFactory(String id){
        return new RumenzA(id);
    }
}

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");
    }

}

输出

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz'
RumenzA 无参构造方法
xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz1'
ID构造方法

通过FactoryBean实例化

beans.xml



    
    

RumenzAFactoryBean.java

package com.rumenz;

import org.springframework.beans.factory.FactoryBean;

public class RumenzAFactoryBean implements FactoryBean {
    @Override
    public Object getObject() throws Exception {
        return RumenzA.createRumenzA();
    }
    @Override
    public Class getObjectType() {
        return RumenzA.class;
    }
}

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");
    }

}

输出/异步加载bean

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz-by-factoryBean'
RumenzA 无参构造方法

Spring Bean初始化的几种常规方式_第1张图片

源码 : https://github.com/mifunc/Spr...

原文: https://rumenz.com/rumenbiji/Spring-Bean-common-instantiate.html

你可能感兴趣的:(java,spring)