In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the interview questions of spring in Java". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the interview questions of spring in Java"?
Talk about the scope of Bean in spring
Singleton:
There is only one shared Bean instance in the Spring IoC container, and no matter how many Bean references it, it always points to the same object. The Singleton scope is the default scope in Spring.
Prototype:
Each time you get a prototype-defined bean through the Spring container, the container creates a new Bean instance, each Bean instance has its own properties and state, while the singleton global has only one object.
Request:
In an Http request, the container returns the same instance of the Bean. A new Bean is generated for different Http requests, and the bean is only valid within the current Http Request.
Session:
In a Http Session, the container returns the same instance of the Bean. For different Session requests, a new instance is created, and the bean instance is valid only within the current Session.
Global Session:
In a global Http Session, the container returns the same instance of the Bean, which is valid only when using portlet context.
Talk about the life cycle of Bean in spring
Instantiate a Bean, which is what we usually call new.
The instantiated Bean is configured according to the Spring context, that is, IOC injection.
If the Bean implements the BeanNameAware interface, the setBeanName (String beanId) method it implements is called, and here you pass the ID of the Bean in the Spring configuration file.
If the Bean implements the BeanFactoryAware interface, the setBeanFactory () of its implementation is called, passing the Spring factory itself (other Bean can be obtained in this way).
If the Bean implements the ApplicationContextAware interface, the setApplicationContext (ApplicationContext) method is called, passing in the Spring context.
If the Bean is associated with a BeanPostProcessor interface, the postProcessBeforeInitialization (Object obj, String s) method is called. BeanPostProcessor is often used as a change to the Bean content, and because this is a call to the After method at the end of Bean initialization, it can also be used for memory or caching techniques.
If the Bean is configured with the init-method property in the Spring configuration file, the initialization method of its configuration is automatically called.
If the Bean is associated with a BeanPostProcessor interface, the postAfterInitialization (Object obj, String s) method will be called.
When Bean is no longer needed, it goes through the cleanup phase, and if Bean implements the DisposableBean interface, its implemented destroy method is called.
Finally, if the destroy-method attribute is configured in the Spring configuration of this Bean, the destroy method of its configuration is automatically called.
Understanding of two ways of dependency injection in Spring
The two injection methods are construction method injection and set value injection.
The setting of value injection is more similar to the traditional way of writing JavaBean, and it is easier for programmers to understand and accept it, and it is more intuitive and obvious to set dependency through setter.
For complex dependencies, if you use construction injection, the constructor will be too bloated to read. When Spring creates an Bean instance, it needs to instantiate all the instances it depends on at the same time, which leads to waste. This problem can be avoided by using setting injection
In cases where some attributes are optional, multi-parameter constructors are more clumsy, and the use of setting injection is officially encouraged.
Construction injection can determine the injection order of dependencies in the constructor, and priority injection of dependencies.
Construction injection is more useful for Bean where dependencies do not need to change, because without the setter method, all dependencies are set in the constructor, so you don't have to worry about subsequent code breaking dependencies.
Construction injection allows dependencies to be set only in the constructor, and only the creator of the component can change the dependency of the component. For the caller of the component, the dependency within the component is completely transparent, which is more in line with the principle of high cohesion.
Setting value injection does not override the value of the constructor. If we use both constructor injection and setting method injection for the same variable, the constructor will not be able to override the value injected by the setting method.
It is suggested that the injection strategy should be dominated by setting injection and supplemented by structural injection. For dependency injection without change, construct injection is used as far as possible, while for other dependency injection, set injection is considered.
What design patterns are used in the Spring framework?
Proxy mode: it is often used in AOP and remoting.
Singleton mode: the bean defined in the spring configuration file defaults to singleton mode.
Template method pattern: used to solve the problem of code duplication.
Front-end controller mode: Spring provides DispatcherServlet to distribute requests.
Dependency injection pattern: the core idea that runs through the BeanFactory / ApplicationContext interface.
Factory mode: BeanFactory is used to create an instance of an object.
The difference between BeanFactory and ApplicationContext
BeanFactory and ApplicationContext are both interfaces, and ApplicationContext is a subinterface of BeanFactory.
BeanFactory is the lowest interface in Spring, which provides the simplest container function, and only provides the function of instantiating and fetching objects. ApplicationContext, on the other hand, is a more advanced container for Spring, providing more useful features.
Additional functions provided by ApplicationContext: internationalization, message sending, response mechanism, unified loading of resources, powerful event mechanism, support for Web applications, and so on.
The difference of loading mode: BeanFactory uses the form of delayed loading to inject Bean;ApplicationContext, on the contrary, it creates all the Bean at one time when Ioc starts, the advantage is that you can immediately find errors in the Spring configuration file, the disadvantage is to cause waste.
Thank you for your reading, the above is the content of "what are the interview questions of spring in Java". After the study of this article, I believe you have a deeper understanding of what the interview questions of spring in Java have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.