1、Spring 有两种类型 bean,一种普通 bean,另外一种工厂 bean(FactoryBean)
    2、普通 bean:在配置文件中定义 bean 类型就是返回类型
    3、工厂 bean:在配置文件定义 bean 类型可以和返回类型不一样
    第一步 创建类,让这个类作为工厂 bean,实现接口 FactoryBean
    第二步 实现接口里面的方法,在实现的方法中定义返回的 bean 类型

    1. package com.atguigu.spring5.factorybean;
    2. import com.atguigu.spring5.collectiontype.Course;
    3. import org.springframework.beans.factory.FactoryBean;
    4. public class MyBean implements FactoryBean<Course> {
    5. //定义返回 bean
    6. /**
    7. * 定义类型和返回类型不一样是由该方法决定的
    8. * @return
    9. * @throws Exception
    10. */
    11. @Override
    12. public Course getObject() throws Exception {
    13. Course course = new Course();
    14. course.setCname("abc");
    15. return course;
    16. }
    17. @Override
    18. public Class<?> getObjectType() {
    19. return null;
    20. }
    21. @Override
    22. public boolean isSingleton() {
    23. return false;
    24. }
    25. }
    1. <bean id="myBean" class="com.atguigu.spring5.factorybean.MyBean"></bean>
    1. @Test
    2. public void testCollection3(){
    3. ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
    4. Course course = context.getBean("myBean", Course.class);
    5. System.out.println(course);
    6. }