spring ioc bean實(shí)例化的三種方式(xml配置)

bean實(shí)例化有三種方式

1、使用無參數(shù)構(gòu)造創(chuàng)建(重點(diǎn))

User.java

package work.zhangdoudou.ico;

public class User {
    public void add(){
        System.out.println("add······");
    }
}

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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 無參數(shù)構(gòu)造創(chuàng)建 -->
    <bean id="user" class="work.zhangdoudou.ico.User"></bean>
</beans>

TestUser.java

package work.zhangdoudou.test;

import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import work.zhangdoudou.ico.User;

public class TestUser {

    @Test
    public void test() {
        //1加載配置文件,根據(jù)創(chuàng)建對(duì)象
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //2得到創(chuàng)建的對(duì)象
        User user=(User) context.getBean("user");
        user.add();
        System.out.println(user);
        
    }
}

運(yùn)行結(jié)果


image.png
  • 類不寫構(gòu)造默認(rèn)無參構(gòu)造
  • 寫了有參數(shù)構(gòu)造,無參數(shù)構(gòu)造被覆蓋,必須手動(dòng)重寫
  • 類里面沒有無參構(gòu)造,會(huì)出現(xiàn)異常
發(fā)生異常
2、使用靜態(tài)工廠創(chuàng)建
  • 創(chuàng)建靜態(tài)方法,返回類對(duì)象
    user2.java
package work.zhangdoudou.ico;

public class User2 {
    private String username;
    
    public User2(){
        
    }
    public User2(String username) {
        this.username = username;
    }

    public void add(){
        System.out.println("user2······");
    }
}

靜態(tài)工廠類Bean2Factory .java

package work.zhangdoudou.BeanFactory;

import org.springframework.context.annotation.Bean;
import work.zhangdoudou.ico.User2;

public class Bean2Factory {
    //靜態(tài)方法返回Bean2對(duì)象
    public static User2 getBean2(){
        return new User2();
    }

}

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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 使用靜態(tài)工廠創(chuàng)建bean2 -->
    <bean id="user2" class="work.zhangdoudou.BeanFactory.Bean2Factory" factory-method="getBean2" ></bean>
</beans>

TestUser2.java

package work.zhangdoudou.test;

import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import work.zhangdoudou.ico.User;
import work.zhangdoudou.ico.User2;

public class TestUser2 {

    @Test
    public void test() {
        //1加載配置文件,根據(jù)創(chuàng)建對(duì)象
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //2得到創(chuàng)建的對(duì)象
        User2 user2=(User2) context.getBean("user2");
        user2.add();
        System.out.println(user2);
        
    }
}

運(yùn)行結(jié)果


image.png
3、使用實(shí)例工廠創(chuàng)建
  • 創(chuàng)建不是靜態(tài)的方法,返回類對(duì)象

User3.java

package work.zhangdoudou.ico;

public class User3 {
    private String username;
    
    public User3(){
        
    }
    public User3(String username) {
        this.username = username;
    }

    public void add(){
        System.out.println("user3······");
    }
}

user3的實(shí)例工廠 Bean3Factory.java

package work.zhangdoudou.BeanFactory;

import org.springframework.context.annotation.Bean;
import work.zhangdoudou.ico.User3;

public class Bean3Factory {
    //普通的方法, 返回Bean3對(duì)象
    public User3 getBean3(){
        return new User3();
    }
}

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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!-- 實(shí)例工廠創(chuàng)建對(duì)象 -->
    <!-- 創(chuàng)建工廠對(duì)象 -->
    <bean id="bean3Factory" class="work.zhangdoudou.BeanFactory.Bean3Factory"></bean>
    <bean id="user3" factory-bean="bean3Factory" factory-method="getBean3"></bean>
</beans>

TestUser3.java

package work.zhangdoudou.test;

import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import work.zhangdoudou.ico.User3;

public class TestUser3 {

    @Test
    public void test() {
        //1加載配置文件,根據(jù)創(chuàng)建對(duì)象
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //2得到創(chuàng)建的對(duì)象
        User3 user3=(User3) context.getBean("user3");
        user3.add();
        System.out.println(user3);
        
    }
}

運(yùn)行結(jié)果


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容