[Interview Question] Why does getBean() in Spring source code need to process "&"?

Personal homepage:Golden Scales Stepping on the Rain

Personal introduction: Hello everyone, I amJinlin, a fledgling Java novice< /span>

Current situation: A 22nd ordinary undergraduate graduate. After many twists and turns, he now works for a large and well-known domestic daily chemical company, engaged in Java development

My blog: This is CSDN, where I learn technology and summarize knowledge. I hope to communicate with you and make progress together ~

Case

Let’s first customize a HarmonyFactoryBean and let it implement the FactoryBean interface

@Component
public class HarmonyFactoryBean implements FactoryBean {

	@Override
	public Object getObject() throws Exception {
		return new UserService();
	}

	@Override
	public Class<?> getObjectType() {
		return UserService.class;
	}
}
@ComponentScan("com.harmony")
public class AppConfig {
}
public class Test {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		System.out.println(context.getBean("&harmonyFactoryBean"));
		System.out.println(context.getBean("harmonyFactoryBean"));
    }
}

operation result

com.zhouyu.service.HarmonyFactoryBean@6b419da
com.zhouyu.service.UserService@636be97c 

What is the difference between adding "&" in getBean() and not adding it? Why is it necessary to design like this?

The getBean() method in Spring is one of the main entrances for obtaining Bean instances. When a getBean() method contains the & symbol, it behaves slightly differently. This is because Spring supports two types of beans:

  1. Common Bean: This is a regular Java object instantiated and managed by the Spring container. When calling the getBean() method, Spring will return an instance of the Bean object.

  2. FactoryBean: This is a special type of Bean that implements the FactoryBean interface. FactoryBeanAllows you to define a factory that is responsible for creating and returning other beans. In this case, the getBean() method returns an instance of FactoryBean, not the Bean instance created by FactoryBean.

When you use the notation in the getBean() method, Spring will tell the container not to return the instance, but Is returneditself. This allows you to access the configuration and properties of instead of getting the beans created by it. This is useful because sometimes you may need to access a property or method directly, rather than getting the bean it created. &FactoryBeanFactoryBeanFactoryBeanFactoryBean

For example, if you have a DataSource of FactoryBean, you can get it via getBean("&dataSourceFactoryBean")< a factory instance of i=4>, and then call the method of the factory instance to access the configuration and properties. And if you call , it will return the instance, not the factory itself. DataSourcegetBean("dataSourceFactoryBean")DataSource

In short, the & notation tells the Spring container to return a FactoryBean instance instead of a Bean instance created by FactoryBean, in order to provide more flexible control and access. If you do not use the & symbol, the getBean() method returns the Bean instance created by FactoryBean by default.

The article ends here. If you have any questions, you can point them out in the comment area~

I hope to work hard with the big guys and see you all at the top

Thank you again for your support, friends! ! !

Guess you like

Origin blog.csdn.net/weixin_43715214/article/details/133782036