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:
-
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. -
FactoryBean: This is a special type of Bean that implements the
FactoryBean
interface.FactoryBean
Allows you to define a factory that is responsible for creating and returning other beans. In this case, thegetBean()
method returns an instance ofFactoryBean
, not the Bean instance created byFactoryBean
.
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. &
FactoryBean
FactoryBean
FactoryBean
FactoryBean
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. DataSource
getBean("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! ! !