The difference between Spring BeanFactory and FactoryBean

First summarize the difference between BeanFactory and FactoryBean

The Bean factory in BeanFactroy spring can produce our classes and obtain our objects. For example, one of our classes is handed over to spring for management, and we can get BeanName directly through this BeanFactroy.

FactroyBean, it is a special Bean in spring. It is an interface that has three methods that need to be implemented. Beans that implement FactroyBean can create a Bean, which is the Bean actually returned by the getObject() method.

They exist in the spring container in different forms. If you need to get a Bean that implements FactroyBean, you need to add an'&' symbol in front of the BeanName to get it. If you need to get the Bean actually returned by the getObject() method, get BeanName directly

Let's look at the code to achieve

Let's first come to an ordinary object as follows

package org.springframework.test.main.entity;

public class Student {

	public void query(){
		System.out.println("org.springframework.test.main.entity.Student.query");
	}

}

Here is another class managed by spring, which is done here using annotations

package org.springframework.test.main.entity;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

@Component
public class Parent implements FactoryBean {
	@Override
	public Object getObject() throws Exception {
		return new Student();
	}

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

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

	public void query(){
		System.out.println("org.springframework.test.main.entity.Parent.query");
	}
}

Let's test the class again

package org.springframework.test.main;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.main.config.ScanConfig;
import org.springframework.test.main.entity.Parent;
import org.springframework.test.main.entity.Student;

public class MainDemo {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ScanConfig.class);
		Parent parent =(Parent) ac.getBean("parent");
		parent.query();
	}

}

Let's focus on this line of code for getBean. Under normal circumstances, if our parent does not implement the FactoryBean interface, we can call the query() method in the parent.

But once the FactoryBean interface is implemented and the getObject() method is overridden, the bean returned by this method is the Bean finally obtained.
With this conclusion, running the above code will obviously report an error, and we see a type conversion exception.
 

D:\java\jdk8\bin\java.exe "
Exception in thread "main" java.lang.ClassCastException: org.springframework.test.main.entity.Student cannot be cast to org.springframework.test.main.entity.Parent
	at org.springframework.test.main.MainDemo.main(MainDemo.java:12)

Process finished with exit code 1

Let's change it. We add the ampersand to getBean("&parent"); and we will find that the query() method in parent is successfully called when running.

Let's go back to the problem of the above type conversion exception. When we change the parent to the Student returned in the getObject() method, the result is a successful call to the Student's query() method. That is, Student parent = (Student) ac.getBean("parent");

 

We know that when the spring container is started, the object annotated with @Component will be handed over to the spring container for management, such as the Parent in this article. When we get it directly, getBean("parent") can be obtained normally. But when our bean implements the FactoryBean interface and rewrites a getObject() method inside, we need to add getBean("&parent") to get this bean, and the object returned by our getObject() method can be GetBean("parent") directly.
For example, the text in the text is normal

Parent parent =(Parent) ac.getBean("&parent");//实现了FactoryBean的bean
Student parent =(Student) ac.getBean("parent");//实现了FactoryBean的bean重写的getObject()方法中返回的Bean

This is a good answer to the above summary

The most common application scenario is the SqlSessionFactory object in Mybaits.
 

Guess you like

Origin blog.csdn.net/qq_38108719/article/details/93489624