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 Spring interview questions in Java

2025-04-03 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 Spring interview questions in Java". 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 Spring interview questions in Java"?

Tell me about your understanding of Spring?

Spring is an open source framework designed to simplify enterprise application development. Spring can be a function that makes a simple JavaBean implementation previously available only to EJB. Spring is an IOC and AOP container framework.

The main core of the Spring container is:

Inversion of control (IOC), in traditional java development mode, when we need an object, we use new or getInstance to directly or indirectly call the constructor to create an object. In the spring development pattern, the spring container uses the factory pattern to create the objects we need. We don't need to create them ourselves, but just call the objects provided by spring. This is the idea of inversion of control.

Dependency injection (DI), the process that spring uses the set method of the javaBean object or the constructor with parameters to automatically set the required value for the properties of the object we need when we create the desired object is the idea of dependency injection.

Aspect-oriented programming (AOP), in the idea of object-oriented programming (oop), we draw things vertically into objects. In aspect-oriented programming, we draw some similar aspects of each object horizontally into a section, and carry out some aspects such as access control, transaction management, logging and so on. The process of common operation processing is the idea of aspect-oriented programming. The bottom layer of AOP is dynamic proxy, if the interface uses JDK dynamic proxy, if the class uses CGLIB to achieve dynamic proxy.

What are the design patterns in Spring?

Factory mode:

BeanFactory is the embodiment of the simple factory pattern, which is used to create instances of objects.

Singleton mode:

Bean defaults to singleton mode.

Agent mode:

The AOP function of Spring uses JDK's dynamic proxy and CGLIB bytecode generation technology.

Template method:

Used to solve the problem of code duplication. such as。 RestTemplate, JmsTemplate, JpaTemplate .

Observer mode:

Defining an object key is an one-to-many dependency. When the state of an object changes, all objects that depend on it will be notified to be updated, such as the implementation ApplicationListener of listener in Spring.

What are the common notes of Spring?

@ Required: this annotation is applied to the setting method

Autowired: this annotation applies to value-setting methods, non-value-setting methods, constructors, and variables.

@ Qualifier: this annotation is used in conjunction with @ Autowired to disambiguate specific bean autoassemblies.

The life cycle of Spring bean?

Bean definition: used to define in the configuration file.

Bean initialization: there are two ways to initialize:

① does this by specifying the init-method property in the configuration file

Implementing org.springframwork.beans.factory.InitializingBean Interface with ②

Bean call: there are three ways to get a bean instance and call it

Bean destruction: there are two ways to destroy

① uses the destroy-method properties specified by the configuration file

Implementing org.springframwork.bean.factory.DisposeableBean Interface with ②

How does Spring manage transactions?

Programmatic transactions, hard-coded in the code.

Declarative transactions, configuring in configuration file

Declarative transactions are divided into:

① declarative transactions based on XML

② annotation-based declarative transaction

What is the transaction propagation behavior of Spring?

1. PROPAGATION_REQUIRED: if there is no transaction currently, create a new transaction, and join the transaction if there is a transaction. This setting is the most commonly used setting.

2. PROPAGATION_SUPPORTS: supports the current transaction. If there is a transaction, join it. If no transaction exists, it will be executed as a non-transaction.

3. PROPAGATION_MANDATORY: supports the current transaction. If there is a transaction, join the transaction. If no transaction exists, an exception is thrown.

4. PROPAGATION_REQUIRES_NEW: create a new transaction, regardless of whether the current transaction exists or not.

5. PROPAGATION_NOT_SUPPORTED: perform the operation in a non-transactional manner, suspending the current transaction if there is a current transaction.

6. PROPAGATION_NEVER: executes in a non-transactional manner, and throws an exception if a transaction exists.

7. PROPAGATION_NESTED: if a transaction currently exists, it is executed within a nested transaction. If there is no transaction currently, press the REQUIRED property to execute.

What is the isolation level of Spring transactions?

1. ISOLATION_DEFAULT: this is the default isolation level for PlatfromTransactionManager, using the default transaction isolation level for the database.

2. ISOLATION_READ_UNCOMMITTED: read uncommitted, allowing another transaction to see the uncommitted data of this transaction.

3. ISOLATION_READ_COMMITTED: read has been committed to ensure that the data modified by one transaction can only be read by another transaction, and you can see the update of the transaction to the existing record.

4. ISOLATION_REPEATABLE_READ: it can be read repeatedly to ensure that the data modified by one transaction can only be read by another transaction after it is committed, but the update of the existing record by the transaction cannot be seen.

5. ISOLATION_SERIALIZABLE: during the execution of a transaction, you can't see any updates made to the database by other transactions.

What is the notice of Spring? What types do you have?

A notification is an action to be done before or after a method is executed, and it is actually a code snippet that is triggered by the SpringAOP framework when the program is executed.

Five types of notifications can be applied to the Spring section:

Before: pre-notification, called before a method is executed.

After: a notification that is called after a method executes, regardless of whether the method execution was successful or not.

After-returning: notification that is executed only when the method completes successfully.

After-throwing: the notification that is executed when the method throws an exception to exit.

Around: notifications called before and after method execution.

What is Spring IOC? What are the advantages of IOC?

IOC:

Control reversal, Spring IOC is responsible for creating objects and managing objects. Through dependency injection (DI), objects are assembled, configured, and managed throughout their lifecycle.

Advantages:

IOC or dependency injection minimizes the amount of code in the application. It makes applications easy to test, and unit testing no longer requires singletons and JNDI lookup mechanisms. Loose coupling can be achieved with minimum cost and minimum intrusiveness. The IOC container supports hungry initialization and lazy loading when loading services.

What's the difference between BeanFactory and AppliacationContext?

BeanFactory

A basic type of IOC container that provides completed IOC service support. If there is no special specification, the deferred initialization policy is used by default. Relatively speaking, the initial start-up speed of the container is fast, and the resources required are limited.

ApplicationContext

ApplicationContext is built on the basis of BeanFactory and is a relatively advanced container implementation. In addition to all the support of BeanFactory, ApplicationContext also provides functions such as event publishing and internationalization support. All objects managed by ApplicationContext are initialized and bound by default after the container is started.

What are the scope of several bean supported by Spring?

The Spring framework supports the following five bean scopes:

Singleton: bean has only one instance in each Spring ioc container.

Prototype: a definition of a bean can have multiple instances.

Request: a bean is created for each http request, and this scope is valid only in the web-based Spring ApplicationContext case.

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

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.

Is the singleton bean in the Spring framework thread safe?

It is not thread safe.

When there is a shared variable, everyone can call it, and there is a thread safety problem.

What is the automatic assembly of bean? What are the ways of automatic assembly?

Automatic assembly:

There is no need to describe the dependencies between javaBean (such as configuration,) in the Spring configuration file. The IOC container automatically establishes an association between the javabean.

Five ways of automatic assembly:

No: the default is not to auto-assemble, but to assemble by explicitly setting the ref property.

ByName: through the automatic assembly of parameter names, the Spring container finds that the autowire property of bean is set to byname in the configuration file.

The container then attempts to match and assemble a bean with the same name as the property of the bean.

ByType: through the automatic assembly of parameter types, the Spring container finds that the autowire property of bean is set to byType in the configuration file

The container then attempts to match, assemble, and have the same type of bean as the properties of the bean. If more than one bean meets the criteria, an error is thrown.

Constructor: this is similar to byType, but provides the constructor parameters, if there is no definite constructor parameter class with parameters

Type, an exception will be thrown

Autodetect: first try to use constructor for automatic assembly, and if it doesn't work, use byType.

What is spring's AOP?

AOP: full name Aspect Oriented Programming, that is: aspect-oriented programming.

AOP is the continuation of OOP (Object Oriented Programming, object-oriented programming). It is a hot spot in software development, an important content in Spring framework, and a derivative paradigm of functional programming.

Each part of the business logic can be isolated by using AOP, which reduces the coupling between the various parts of the business logic, improves the reusability of the program, and improves the efficiency of development at the same time.

Extract the repeated code of our program, and use the technology of dynamic proxy to enhance our existing method without modifying the source code when it needs to be executed.

AOP adopts horizontal extraction mechanism to replace the repetitive code of traditional vertical inheritance system.

AOP technology uses a technique called "crosscutting" to unravel the interior of encapsulated objects, encapsulate common behaviors that affect multiple classes into a reusable module, and name it "Aspect", or section. The so-called "aspect" simply means that the logic or responsibility that has nothing to do with the business but is jointly invoked by the business module is encapsulated in order to reduce the repetitive code of the system and reduce the coupling between the modules. and conducive to future maneuverability and maintainability.

Using crosscutting technology, AOP divides the software system into two parts: core concerns and crosscutting concerns.

The main process of business processing is the core concern, and the less relevant part is the crosscutting concern.

One of the characteristics of crosscutting concerns is that they often occur in multiple places of core concerns, while they are basically similar everywhere, such as authentication, logs, and things.

The function of AOP is to separate the various concerns in the system and to separate the core concerns from the crosscutting concerns.

The core of AOP is aspect, which encapsulates the common behavior of multiple classes into a reusable module, which contains a set of API to provide crosscutting functions. For example, a log module can be called the AOP aspect of a log. Depending on the requirements, an application can have several aspects. In Spring AOP, aspects are implemented through classes with @ Aspect annotations.

Classic applications: transaction management, performance monitoring, security checking, caching, logging, etc.

How to implement AOP?

The bottom layer of aop is implemented by proxy mechanism.

Interface + implementation class: spring adopts jdk's dynamic proxy Proxy.

Implementation class: spring uses cglib bytecode enhancement.

What are the concepts in AOP?

① Joinpoint: the point that is intercepted. In spring, these points refer to methods, because spring only supports join points of method types.

② Pointcut (pointcut): which Joinpoint to intercept is the enhanced join point.

③ Advice (Notification / Enhancement): what to do after intercepting Joinpoint, and enhance the code.

④ Introduction (introduction): introduction is a special notification that Introduction can dynamically add methods or Field to a class at run time without modifying the class code.

⑤ Target: the target class, the class that needs to be proxied

⑥ Weaving: the process of applying enhancements to the target object to create a new proxy object. Spring uses dynamic proxy weaving, while AspectJ uses compile-time weaving and class loading-time weaving.

⑦ Proxy: a proxy class, when a class is enhanced by AOP weaving, produces a result proxy class.

⑧ Aspect: a combination of pointcuts and notifications (introductions).

How do you define the scope of a class in Spring?

When defining one in Spring, you can declare a scope for the bean. It can be defined by the scope attribute in the bean definition.

When Spring is to produce a new bean instance each time it is needed, the scope property of bean is specified as prototype.

A bean must return the same instance each time it is used, and the scope property of this bean is specified as singleton.

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

At this point, I believe you have a deeper understanding of "what are the Spring interview questions in Java?" 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.

Share To

Development

Wechat

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

12
Report