1. 程式人生 > 其它 >Spring中的普通bean和工廠bean

Spring中的普通bean和工廠bean

技術標籤:Spring5springbean工廠bean

普通bean和工廠bean


Spring中有兩種型別bean,一種是普通bean,另外一種是工廠bean(FactoryBean)。
區別:

不同點
普通bean在配置檔案中定義bean的型別就是返回型別。
工廠bean在配置檔案定義bean型別可以和返回型別不同。

普通bean

在配置檔案中定義bean的型別就是返回型別。

Book類:

package com.Keafmd.spring5.collectiontype;

import java.util.List;
/** * Keafmd * * @ClassName: Book * @Description: * @author: 牛哄哄的柯南 * @date: 2021-01-15 14:56 */ public class Book { private List<String> list; public void setList(List<String> list) { this.list = list; } public void test(){ System.out.println(list);
} }

bean2.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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--配置檔案中定義的型別是 Book--> <bean id="book" class="com.Keafmd.spring5.collectiontype.Book" ></bean> </beans>

測試程式碼:

package com.Keafmd.spring5.testdemo;

import com.Keafmd.spring5.collectiontype.Book;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Keafmd
 *
 * @ClassName: TestSpring5demo1
 * @Description: 測試類
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 14:30
 */
public class TestSpring5demo1 {

    @Test
    public void test2(){

        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        Book book = context.getBean("book",Book.class);
        Book book2 = context.getBean("book",Book.class);
        System.out.println(book);
    }

}

輸出結果:

com.Keafmd.spring5.collectiontype.Book@376b4233

Process finished with exit code 0

我們可以看到輸出結果中的[email protected],我們的配置檔案中定義的是Book型別,返回的就是Book型別,這就是我們經常見的普通bean。

工廠bean(FactoryBean)

在配置檔案定義bean型別可以和返回型別不同。

建立步驟:
第一步:建立類MyBean(命名隨意),讓這個類作為工廠bean,實現介面FactoryBean。
第二步:實現接口裡面的方法,在實現的方法中定義返回的bean型別。

MyBean類:

package com.Keafmd.spring5.factorybean;

import com.Keafmd.spring5.collectiontype.Course;
import org.springframework.beans.factory.FactoryBean;

/**
 * Keafmd
 *
 * @ClassName: MyBean
 * @Description: 工廠Bean
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 15:38
 */
public class MyBean implements FactoryBean<Course> {

    //定義返回的bean的物件
    @Override
    public Course getObject() throws Exception {
        Course course = new Course();
        course.setCname("abc");
        return course;
    }

    @Override
    public Class<?> getObjectType() {
        return null;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }
}

Course類:

package com.Keafmd.spring5.collectiontype;

/**
 * Keafmd
 *
 * @ClassName: Course
 * @Description: 課程類
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 14:41
 */
public class Course {
    private String cname; // 課程名稱

    public void setCname(String cname) {
        this.cname = cname;
    }

    @Override
    public String toString() {
        return "Course{" +
                "cname='" + cname + '\'' +
                '}';
    }
}

bean3.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">
	<!--配置檔案中定義的型別是 MyBean-->
    <bean id="mybean" class="com.Keafmd.spring5.factorybean.MyBean">

    </bean>

</beans>

測試類:

package com.Keafmd.spring5.testdemo;

import com.Keafmd.spring5.bean.Orders;
import com.Keafmd.spring5.collectiontype.Book;
import com.Keafmd.spring5.collectiontype.Course;
import com.Keafmd.spring5.collectiontype.Stu;
import com.Keafmd.spring5.factorybean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Keafmd
 *
 * @ClassName: TestSpring5demo1
 * @Description: 測試類
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 14:30
 */
public class TestSpring5demo1 {

    @Test
    public void test3(){

        ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
        Course myBean = context.getBean("mybean",Course.class);
        System.out.println(myBean);

    }

}

輸出結果:

Course{cname='abc'}

Process finished with exit code 0

很明顯的可以看出,配置檔案中定義的型別是MyBean,返回的型別是Course。

看完如果對你有幫助,感謝點贊支援!
如果你是電腦端的話,看到右下角的 “一鍵三連” 了嗎,沒錯點它[哈哈]

在這裡插入圖片描述
加油!

共同努力!

Keafmd