In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the common spring interview questions for Java engineers?" interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the common spring interview questions for Java engineers?"
1. Tell me about your understanding of spring.
Spring is an open source lightweight IoC and AOP container framework, it provides a comprehensive infrastructure for Java applications, its purpose is to simplify the development of enterprise applications, it makes developers only need to care about business needs, so as to improve development efficiency and system maintainability.
How to configure 2.spring
Common configuration methods of spring: XML-based configuration, annotation-based configuration, Java-based configuration.
3. Please list the important modules in spring
Spring Core: core class library on which all spring functions are relied on to provide IOC services
Spring Context: provides framework-based Bean access, as well as enterprise-level features (JNDI, scheduled tasks, etc.)
Spring AOP:AOP services that provide aspect-oriented programming implementation
Spring DAO: abstraction of JDBC simplifies the handling of data access exceptions
Spring ORM: support for existing ORM frameworks, such as support for Hibernate
Spring Web: provides comprehensive support for creating Web applications, such as multi-party file upload
Spring MVC: provides Model-View-Controller (model-view-controller) implementation for Web applications
Spring Test: provides support for JUnit and TestNG testing
Spring Aspects: this module provides support for integration with AspectJ.
What are the advantages of 4.spring
(1) spring adopts low intrusive design, which greatly reduces code pollution.
(2) spring transfers the dependencies between objects to the framework through the DI mechanism, which reduces the coupling of components.
(3) Spring AOP technology, which centrally manages some common tasks, such as security, transactions, logs, permissions, etc., so as to provide better reuse.
(4) spring provides good integration support for mainstream application frameworks.
5. Tell me about your understanding of Spring IoC and AOP.
The inversion of IoC control refers to the transfer of control over the creation of objects. In the past, the initiative and timing of creating objects were controlled by themselves, but now this power is transferred to the Spring container, and the container creates instances and manages the dependencies between instances according to configuration files. The loose coupling between objects and objects is also conducive to the reuse of functions. DI dependency injection and control inversion are different descriptions of the same concept, that is, applications rely on IoC containers to dynamically inject external resources needed by objects at run time.
AOP aspect-oriented programming can be said to be a supplement to object-oriented programming (Note: object-oriented abbreviation (OOP): allows developers to define vertical relationships, but also applies to defining horizontal relationships, resulting in a large number of code duplication, which is not conducive to the reuse of various modules) to use common behaviors and logic that are not business-related but affect multiple objects. It is extracted and encapsulated into a reusable module, which is named "Aspect", which reduces the repetitive code in the system, reduces the coupling between modules, and improves the maintainability of the system. Can be used for permission authentication, logging, transaction processing.
The injection mode of 6.Spring IoC
Injection methods: constructor injection, annotation injection, setter method injection
What's the difference between 7.BeanFactory and ApplicationContext?
BeanFactory and ApplicationContext are the two core interfaces of Spring, and both can be used as containers for Spring. Where ApplicationContext is a subinterface of BeanFactory.
(1) BeanFactory: the lowest-level interface in Spring, including various Bean definitions, reading bean configuration documents, managing the loading and instantiation of bean, controlling the life cycle of bean, and maintaining the dependency between bean. As a derivative of BeanFactory, ApplicationContext interface not only provides the functions of BeanFactory, but also provides more complete framework functions:
① inherits MessageSource and therefore supports internationalization.
② unified access to resource files.
③ provides events for registering bean with the listener.
④ loads multiple configuration files at the same time.
⑤ loads multiple (inherited) contexts so that each context focuses on a specific level, such as the web layer of the application.
(2) ① BeanFactroy injects Bean in the form of delayed loading, that is, only when a Bean is used (call getBean ()), the Bean is instantiated. In this way, we cannot find some problems with the configuration of Spring. If one of the properties of Bean is not injected, the BeanFacotry is loaded and the exception is not thrown until the first call to the getBean method is used.
② ApplicationContext, which creates all the Bean at once when the container starts. In this way, when the container starts, we can find configuration errors in the Spring, which helps to check whether the dependent properties are injected. After ApplicationContext starts, preload all single instance Bean. By preloading single instance bean, make sure you don't have to wait when you need it, because they have already been created.
The only drawback of ③ compared to basic BeanFactory,ApplicationContext is its memory footprint. When the application is configured with more Bean, the program starts slowly.
(3) BeanFactory is usually created programmatically, and ApplicationContext can also be created declaratively, such as using ContextLoader.
(4) both BeanFactory and ApplicationContext support the use of BeanPostProcessor and BeanFactoryPostProcessor, but the difference between them is that BeanFactory needs to be registered manually, while ApplicationContext is automatically registered.
What's the difference between 8.Spring AOP and AspectJ AOP?
Spring AOP is a run-time enhancement, while AspectJ is a compile-time enhancement. Spring AOP is based on Proxying, while AspectJ is based on bytecode operation (Bytecode Manipulation).
Spring AOP has integrated AspectJ,AspectJ, which is probably the most complete AOP framework in the Java ecosystem. AspectJ is more powerful than Spring AOP, but Spring AOP is relatively simple.
If we have fewer sections, there is little difference in performance between the two. However, when there are too many aspects, it is best to choose AspectJ, which is much faster than SpringAOP.
Bean Lifecycle in 9.Spring
(1) instantiate Bean:
For the BeanFactory container, when a customer requests an uninitialized bean from the container, or when initializing the bean needs to inject another uninitialized dependency, the container calls createBean for instantiation. For the ApplicationContext container, when the container is started, all bean are instantiated by getting the information in the BeanDefinition object.
(2) set object properties (dependency injection):
The instantiated object is encapsulated in a BeanWrapper object, and Spring then completes dependency injection based on the information in BeanDefinition and the interface provided by BeanWrapper to set properties.
(3) deal with Aware interface:
Next, Spring detects whether the object implements the xxxAware interface and injects the relevant xxxAware instance into Bean:
① if the Bean already implements the BeanNameAware interface, the setBeanName (String beanId) method it implements will be called. Here, the id value of Bean in the Spring configuration file is passed.
② if the Bean already implements the BeanFactoryAware interface, the setBeanFactory () method it implements will be called, passing the Spring factory itself.
③ if the Bean has already implemented the ApplicationContextAware interface, the setApplicationContext (ApplicationContext) method will be called, passing in the Spring context
(4) BeanPostProcessor:
If you want to do some custom processing on Bean, you can have Bean implement the BeanPostProcessor interface, which will call the postProcessBeforeInitialization (Object obj, String s) method.
(5) InitializingBean and init-method:
If Bean configures the init-method property in the Spring configuration file, the initialization method of its configuration is automatically called.
(6) if the Bean implements the BeanPostProcessor interface, the postProcessAfterInitialization (Object obj, String s) method will be called; because this method is called at the end of Bean initialization, it can be applied to memory or caching technology.
After the above steps are completed, the Bean has been created correctly, and then you can use the Bean.
(7) DisposableBean:
When Bean is no longer needed, it goes through the cleanup phase. If Bean implements the interface DisposableBean, the destroy () method that it implements will be called.
(8) destroy-method:
Finally, if the destroy-method attribute is configured in the Spring configuration of this Bean, the destroy method of its configuration is automatically called.
The life cycle is shown in the following figure:
How 10.Spring MVC works
Process description:
(1)。 The client (browser) sends a request directly to the DispatcherServlet.
(2) .DispatcherServlet calls HandlerMapping according to the request information to parse the corresponding Handler of the request.
(3)。 Parse to the corresponding Handler (also known as the Controller controller).
(4) .HandlerAdapter invokes the real processor according to the Handler to process the request and execute the corresponding business logic.
(5)。 After the processor has finished processing the business, it returns a ModelAndView object, Model is the returned data object, and View is the logical View.
(6) .ViewResolver will find the actual View according to the logical View.
(7) .DispatcherServlet passes the returned Model to View (view rendering).
(8)。 Return the View to the requestor (browser).
As shown in the following figure:
What design patterns are used in the 11.Spring framework
(1)。 Factory pattern: BeanFactory in Spring is the embodiment of the simple factory pattern, which is used to create bean objects.
(2)。 Proxy mode: the realization of Spring AOP function uses JDK dynamic proxy and CGLIB bytecode generation technology.
(3)。 Singleton mode: bean in Spring is singleton by default.
(4)。 Template method: jdbcTemplate, hibernateTemplate and other classes in Spring that operate on the database ending with Template, they use the template pattern, which is mainly used to solve code duplication.
(5)。 Wrapper mode: our project needs to connect to multiple databases, and different customers will access different databases as needed during each visit. This mode allows us to dynamically switch different data sources according to the needs of our customers.
(6)。 Observer pattern: the Spring event-driven model is a classic application of the Observer pattern.
(7)。 Adapter pattern: the enhancement or Advice of Spring AOP uses adapter pattern, and adapter pattern adaptation Controller is also used in Spring MVC.
The scope of bean in 12.spring
The bean in the Spring container can be divided into five ranges:
(1)。 Singleton: by default, there is only one instance of bean in each container, and the singleton schema is maintained by BeanFactory itself.
(2) .prototype: provides an instance for each bean request.
(3) .request: create an instance for each network request, and after the request is completed, the bean is invalidated and reclaimed by the garbage collector.
(4) .session: similar to the request scope, ensure that there is an instance of bean in each session, and when the session expires, the bean expires.
(5) .global-session: global scope, global-session and Portlet applications related. When your application is deployed to work in a Portlet container, it contains a lot of portlet. If you want to declare that all portlet share global storage variables, then the global variables need to be stored in global-session. The global scope has the same effect as the session scope in Servlet.
Several ways of injecting bean into 13.Spring based on xml
(1) Set method injection
(2) Constructor injection: ① sets the position of the parameter through index; ② sets the parameter type through type
(3) static factory injection
(4) example factory
What are several kinds of automatic assembly in the xml configuration of 14.Spring framework
There are 5 types of autoassembly in the Spring framework xml configuration:
(1) no: the default method is not automatic assembly, by manually setting the ref property to assemble bean.
(2) byName: automatic assembly is carried out by the name of bean. If the property of one bean is the same as the name of another bean, it is automatically assembled.
(3) byType: automatic assembly based on the data type of the parameter.
(4) constructor: the constructor is used for assembly, and the parameters of the constructor are assembled by byType.
(5) autodetect: automatic detection, if there is a construction method, automatic assembly by construct, otherwise automatic assembly by byType.
What are the common notes of bean in 15.spring
(1). @ Component comment. General annotations that can mark any class as a Spring component. If a Bean doesn't know which layer it belongs to, you can annotate it with @ Component.
(2). @ Repository comment. The corresponding persistence layer, namely Dao layer, is mainly used for database-related operations.
(3). @ Service note. The corresponding service layer, namely the Service layer, mainly involves some complex logic and needs to use the Dao layer (injection).
(4). @ Controller note. The control layer of the corresponding Spring MVC, namely the Controller layer, is mainly used to accept user requests and call the methods of the Service layer to return data to the front-end page.
At this point, I believe you have a deeper understanding of "what are the common spring interview questions for Java engineers?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.