Spring Bean初始化的幾種常規方式

  • 經過構造方法實例化
  • 經過靜態工廠實例化
  • 經過實例工廠實例化
  • 經過FactoryBean實例化

RumenzA實體類html

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.xmljava

<?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>

DemoApplication.javagit

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

}

輸出github

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz'
RumenzA 無參構造方法
xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz1'
ID構造方法

靜態工廠

beans.xmlspring

<?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>

RumenzFactory工廠類segmentfault

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

}

輸出ide

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rFactory'
RumenzA 無參構造方法
xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rFactory1'
ID構造方法

實例工廠實例化

<?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>

RumenzFactory.javathis

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

DemoApplication.javaurl

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

<?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>

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 無參構造方法

圖片alt

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

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

相關文章
相關標籤/搜索