BeanFactory and FactoryBean difference (Detailed)

BeanFactory

BeanFactory is a container interface is also the most basic IOC, responsible for the production and management of bean, which provide the most basic norms of other specific IOC container, such as a specific container DefaultListableBeanFactory, XmlBeanFactory, ApplicationContext BeanFactory interface are all achieved, and then other additional features in its basis.

FactoryBean

FactoryBean is an interface, when the IOC Bean container FactoryBean achieved, not the implementation class objects acquired by FactoryBean Bean object getBean (String beanName), but this implementation class getObject () method returns the object. To get FactoryBean implementation class, it is necessary getBean (& beanNme), plus before BeanName &.

  • Hides the details of some complex examples of the Bean, to the upper application is made easier.
  • And a model of the factory design pattern and a modified mode are similar.
  • getBean (beanName) is obtained by the method getObject () object returned.
  • getBean (& beanName) method to get the object is achieved FactoryBean.
  • getObject () can return to how an object inside, so FactoryBean provides a more flexible implementation as a Bean.

FactoryBean case

1. Create an implementation class FactoryBean

  • getBean (beanName) is obtained by the method getObject () object returned.
  • getBean (& beanName) method to get the object is achieved FactoryBean.
  • getObject () method can indeed achieve hide implementation details and functions.
  • getObject () can return to how an object inside, so FactoryBean provides a more flexible implementation as a Bean.
package factorybean;
import org.springframework.beans.factory.FactoryBean;
public class FactoryBeanPojo implements FactoryBean {

    private String type;

    /**
     * getBean获取的是getObject()方法返回的对象
     * @return
     * @throws Exception
     */
    @Override
    public Object getObject() throws Exception {
        if("student".equals(type)){
            Student student = new Student();
            student.setName("学生");
            return student;
        }else{
            Teacher teacher = new Teacher();
            teacher.setName("老司机");
            return teacher;
        }
    }

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

    @Override
    public Class<?> getObjectType() {
        return Teacher.class;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

2. Create two common Bean

package factorybean;
public class Student {

    private String name;

    public Student(){

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package factorybean;
public class Teacher {
    private String name;

    public Teacher(){

    }

    public String getName() {
        return name;
    }

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

3. Test Code

package factorybean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FactoryBeanTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Object student = applicationContext.getBean("factoryBeanPojo");
        System.out.println("student:"+student);
        Object factoryBeanPojo = applicationContext.getBean("&factoryBeanPojo");
        System.out.println("factoryBeanPojo:"+factoryBeanPojo);
    }
}

4.spring profile

<?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="factoryBeanPojo" class="factorybean.FactoryBeanPojo">
        <property name="type" value="student"/>
    </bean>
</beans>

The code execution results

Here Insert Picture Description

  • 1.getBean (beanName) due to the spring so arranged that the student getObject () returns the Student object.
  • 2.getBean (& beanName) is returned FactoryBean implementation interface object itself here also verified.
Published 60 original articles · won praise 1 · views 3327

Guess you like

Origin blog.csdn.net/qq_16438883/article/details/103680167