Spring中有两种类型bean,一种是普通bean,另外一种是工厂bean(FactoryBean)。
区别:
普通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
我们可以看到输出结果中的Book@376b4233,我们的配置文件中定义的是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。