Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the interview questions for Spring?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you what are the contents of the interview questions about Spring. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

What is the Spring framework? what are the main modules of the Spring framework?

Spring framework is a Java platform that provides comprehensive and extensive basic support for the development of Java applications. Spring helps developers solve fundamental problems in development, allowing developers to focus on application development.

The Spring framework itself is carefully built according to the design pattern, which allows us to integrate the Spring framework with peace of mind in the development environment without having to worry about how Spring works in the background.

The Spring framework has integrated more than 20 modules so far. These modules are mainly divided into core containers, data access / integration, Web, AOP (aspect-oriented programming), tools, messages, and test modules as shown in the following figure.

What are the benefits of using the Spring framework?

Here are some of the main benefits of using the Spring framework:

1. The Dependency Injection (DI) method makes the dependency between the constructor and the JavaBean properties file clear at a glance.

2. Compared with EJB containers, IoC containers tend to be more lightweight. This makes it advantageous for the IoC container to develop and distribute applications with limited memory and CPU resources.

3. Spring does not work behind closed doors. Spring uses existing technologies such as ORM framework, logging framework, J2EE, Quartz and JDK Timer, and other view technologies.

4. Spring framework is organized in the form of modules. The modules to which they belong can be seen by the numbers of packages and classes, and developers only need to choose the modules they need.

5. It is easy to test an application developed in Spring, because the test-related environment code is already included in the framework. More simply, using the POJO class in the form of JavaBean, you can easily write test data using dependency injection.

6. Spring's Web framework is also a well-designed Web MVC framework, which provides developers with a powerful choice in the choice of web framework in addition to mainstream frameworks such as Struts, overdesigned and unpopular web framework.

7. Spring provides a convenient transaction management interface, which is suitable for small local transaction processing (such as in a single DB environment) and complex common transaction processing (such as a complex DB environment using JTA).

3. What is IOC? What is dependency injection?

1. Control inversion is a programming skill used in the field of software engineering to bind coupled objects by assembler objects at run time, and the coupling relationship between objects is usually unknown at compile time.

In traditional programming, the flow of business logic is determined by objects in the application that have already been associated.

In the case of control inversion, the flow of business logic is determined by the object diagram, which is instantiated by the assembler, which can also abstract the definition of the relationship between objects. The binding process is implemented through dependency injection.

2. Control inversion is a design paradigm for the purpose of giving more control to the target components in the application, and has played an effective role in our practical work.

Dependency injection is a pattern that instantiates the functional objects that other objects depend on when the class from which the required function comes from is not known at the compilation stage.

This requires a mechanism to activate the corresponding components to provide specific functionality, so dependency injection is the basis for control inversion. Otherwise, if the component is not controlled by the framework, how does the framework know which component to create?

4. There are still three ways to implement injection in Java:

1) Constructor injection

2) Setter method injection

3) Interface injection

4. Please explain the IoC in Spring framework.

The org.springframework.beans package and org.springframework.context package in Spring form the basis of the Spring framework IoC container.

The BeanFactory interface provides an advanced configuration mechanism that makes the configuration of any type of object possible. The ApplicationContex interface extends BeanFactory (which is a subinterface).

Other features have been added to BeanFactory, such as easier integration with AOP with Spring, mechanisms for handling message resource (for internationalization), event propagation, and special configuration of the application layer, such as WebApplicationContext for Web applications.

What's the difference between BeanFactory and ApplicationContext?

BeanFactory can be understood as a factory class that contains bean collections. BeanFactory contains a definition of a bean to instantiate the corresponding bean when a client request is received.

BeanFactory can also generate relationships between collaboration classes when objects are instantiated. This frees up the configuration of bean itself and the bean client. BeanFactory also includes bean lifecycle control, calling the client's initialization method (initialization methods) and destruction method (destruction methods).

On the face of it, application context, like bean factory, has bean definition, bean association setting, and the function of distributing bean according to request. But application context provides other features on top of this:

1) text messages that support internationalization are provided

2) uniform way to read resource files

3) events of bean that have been registered with the listener

6. How many configurations are there for Spring?

There are three ways to configure Spring in application development:

1) XML-based configuration

2) Annotation-based configuration

3) Java-based configuration

7. How to configure Spring based on XML configuration?

In the Spring framework, dependencies and services need to be implemented in a special configuration file, which I often use in XML format. These configuration files are usually formatted at the beginning, followed by a series of bean definitions and specialized application configuration options.

The main purpose of SpringXML configuration is to make all Spring components configurable in the form of xml files. This means that there will be no other Spring configuration types (such as declarative or Java Class-based configuration)

Spring's XML configuration is implemented using a series of XML tags supported by the Spring namespace. Spring has the following main namespaces: context, beans, jdbc, tx, aop, mvc, and aso.

8. How to configure Spring with annotations?

Spring began to support annotated configuration of dependency injection after version 2.5. You can replace the XML bean description with annotations, and you can transfer the bean description to the interior of the component class by using annotations on related classes, methods, or field declarations.

Annotation injection will be processed by the container before XML injection, so the latter will override the former's handling of the same attribute.

Annotation assembly is turned off by default in Spring. So you need to configure it in the Spring file to use the annotation-based assembly mode. If you want to use annotations in your application, please refer to the configuration below.

After the tag configuration is complete, you can automatically assemble variables to properties, methods, and constructors in Spring using annotations.

Here are some of the more important types of annotations:

1) @ Required: this annotation applies to the setting method.

2) @ Autowired: this annotation applies to value-setting methods, non-value-setting methods, construction methods, and variables.

3) @ Qualifier: this annotation is used in conjunction with the @ Autowired annotation to disambiguate a particular bean autoassembly.

4) JSR-250 Annotations:Spring supports the following annotations based on JSR-250 annotations, @ Resource, @ PostConstruct and @ PreDestroy.

Please explain the life cycle of Spring Bean

The life cycle of Spring Bean is simple and easy to understand. When an bean instance is initialized, a series of initialization operations need to be performed to achieve the available state.

Similarly, when a bean is not being called, it needs to be destructed and removed from the bean container.

Spring bean factory is responsible for managing the lifecycle of the bean created in the spring container. The life cycle of a Bean consists of two sets of call back methods.

1) callback method called after initialization.

2) destroy the callback method previously called.

The Spring framework provides the following four ways to manage bean lifecycle events:

1) InitializingBean and DisposableBean callback APIs

2) other Aware interfaces for special behavior

3) Custom init () method and destroy () method in Bean configuration file

4) @ PostConstruct and @ PreDestroy annotation methods

The code example for managing the bean lifecycle using the customInit () and customDestroy () methods is as follows:

What is the difference between the scope of Spring Bean?

The bean in the Spring container can be divided into five ranges. The names of all ranges are self-explanatory, but to avoid confusion, let's explain:

1) singleton: this bean scope is the default, which ensures that no matter how many requests are received, there is only one instance of bean in each container, and the singleton mode is maintained by bean factory itself.

2) prototype: the prototype scope is the opposite of the singleton scope, providing an instance for each bean request.

3) request: within the scope of the request bean, an instance is created for every network request from the client. After the request is completed, the bean is invalidated and reclaimed by the garbage collector.

4) Session: similar to the request scope, make sure that there is an instance of bean in each session, and when the session expires, the bean will expire.

5) global-session:global-session is related to Portlet application. 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.

What is Spring beans?

Spring beans are the java objects that form the backbone of Spring applications. They are initialized, assembled, and managed by Spring IOC containers. These beans are created from the metadata configured in the container. For example, it is defined in the form in the XML file.

The beans defined by the Spring framework is a single beans. There is a property "singleton" in bean tag, which is a singleton if it is assigned a TRUE,bean, otherwise it is a prototype bean. The default is TRUE, so all beans in the Spring framework are singleton by default.

Explain the scope of several bean supported by Spring

The Spring framework supports the following five bean scopes:

1) singleton: bean has only one instance in each Spring ioc container.

2) prototype: there can be multiple instances of a bean definition.

3) request: a bean is created for each http request, which is valid only in the case of web-based Spring ApplicationContext.

4) session: in a HTTP Session, a bean definition corresponds to an instance. This scope is valid only in web-based Spring ApplicationContext cases.

5) global-session: in a global HTTP Session, a bean definition corresponds to an instance. This scope is valid only in web-based Spring ApplicationContext cases.

The default Spring bean scope is Singleton.

XIII. Explain the life cycle of bean in the Spring framework

The Spring container reads the definition of bean from the XML file and instantiates the bean.

Spring populates all attributes according to the definition of bean.

If bean implements the BeanNameAware interface, Spring passes the ID of bean to the setBeanName method.

If Bean implements the BeanFactoryAware interface, Spring passes beanfactory to the setBeanFactory method.

If there are any BeanPostProcessors,Spring associated with bean, they are called within the postProcesserBeforeInitialization () method.

If bean implements IntializingBean, call its afterPropertySet method, and if bean declares an initialization method, call this initialization method.

If there is a BeanPostProcessors and bean association, the postProcessAfterInitialization () method of these bean will be called.

If bean implements DisposableBean, it calls the destroy () method.

What are the important bean lifecycle approaches? Can you reload them?

There are two important bean lifecycle methods. The first is setup, which is called when the container loads bean. The second method is teardown, which is called when the container unloads the class.

The The bean tag has two important attributes (init-method and destroy-method). With them you can customize your own initialization and logout methods. They also have corresponding annotations (@ PostConstruct and @ PreDestroy).

What is the internal bean of Spring?

When a bean is used only as an attribute of another bean, it can be declared as an internal bean. In order to define inner bean, elements can be used within or elements in Spring's XML-based configuration metadata, internal bean is usually anonymous, and their Scope is usually prototype.

16. How to inject a java collection into Spring?

Spring provides configuration elements for the following collections:

Type is used to inject a list of values, allowing the same value.

Type is used to inject a set of values and the same values are not allowed.

Type is used to inject a set of key-value pairs, both of which can be of any type.

Type is used to inject a set of key-value pairs, and both keys and values can only be of type String.

What is bean assembly?

Assembling, or bean assembling, refers to assembling bean together in a Spring container, provided that the container needs to know the bean dependencies and how to assemble them together through dependency injection.

What is the automatic assembly of bean?

Spring containers can automatically assemble cooperative bean, which means that containers do not need and configuration, and can automatically handle collaboration between bean through the Bean factory.

Thank you for reading! This is the end of this article on "what are the interview questions for Spring?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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: 256

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report