Factory bean in Spring

1. Ordinary bean and factory bean

Spring has two types of beans, one is ordinary bean, and the other is factory bean (FactoryBean)

  • Ordinary bean: Defining the bean type in the configuration file is the return type
  • Factory bean: The bean type defined in the configuration file can be different from the return type

2. Steps of factory bean

  1. Create a class, let this class as a factory bean, implement the interface FactoryBean
public class MyFactoryBean implements FactoryBean<Student> {
    
    

    //定义返回 bean
    @Override
    public Student getObject() throws Exception {
    
    
        Student student = new Student();
        student.setName("工厂bean");
        return student;
    }

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

    @Override
    public boolean isSingleton() {
    
    
        return false;
    }
}
  1. Implement methods in the interface and define the returned bean type in the implemented method
	//定义返回 bean
    @Override
    public Student getObject() throws Exception {
    
    
        Student student = new Student();
        student.setName("工厂bean");
        return student;
    }

Code example:
Student class:

public class Student {
    
    
    private String name;
    private int age;
    private String gender;

    public Student() {
    
    
    }

    public Student(String name, int age, String gender) {
    
    

        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }

    public String getGender() {
    
    
        return gender;
    }

    public void setGender(String gender) {
    
    
        this.gender = gender;
    }

    @Override
    public String toString() {
    
    
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }
}

xml file:

<?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">
    <bean id="myFactoryBean" class="iocbean.byxml.factorybean.MyFactoryBean">

    </bean>
</beans>

Test category:

public class TestDemo {
    
    
    @Test
    public void test(){
    
    
        ApplicationContext context = new ClassPathXmlApplicationContext("iocbean/byxml/factorybean/bean.xml");

        Student bean = context.getBean("myFactoryBean", Student.class);

        System.out.println(bean);
    }
}

Output result:

Student{name='工厂bean', age=0, gender='null'}

Process finished with exit code 0

Guess you like

Origin blog.csdn.net/MrYushiwen/article/details/110818537