Summary of Java classic interview questions: Spring


1. What is spring? What are the advantages?

Spring is an open source development framework for Java enterprise applications, which is mainly used to develop java applications, but some extensions are aimed at building web applications on J2EE platform. The goal is to simplify Java enterprise application development and promote good programming habits through POJO based programming model.

It has the following advantages:

  • IOC technology is provided, and the container will help you manage the dependent objects, so you don’t need to create and manage the dependent objects by yourself, and it is easier to realize the decoupling of the program.
  • Transaction support is provided, which makes transaction operation more convenient.
  • Slice oriented programming is provided, which can deal with a certain kind of problems more conveniently. More convenient framework integration,
  • It can easily integrate other frameworks, such as mybatis, hibernate, etc.

2. What is AOP?

AOP: aspect oriented programming is a technology of aspect oriented programming, which realizes the unified maintenance of program functions through precompiled mode and runtime dynamic agent. In short, it is the programming idea of unified processing of a certain “aspect” (class), such as unified processing of logs, exceptions, etc.

3. What is IOC?

IOC: inversion of control is the core of spring. For the spring framework, spring is responsible for controlling the life cycle of objects and the relationship between objects. In short, control refers to the control of the current object over its internal members; Inversion of control means that this control right is not managed by the current object, but by others (classes, third-party containers).

4. What is di?

Di (dependency injection) is a simpler interpretation of IOC. The basic principle of dependency injection is that application components should not be responsible for finding resources or other dependent collaboration objects.

5. What are the core modules of spring?

  • Spring core: the most basic part of the framework, which provides IOC and dependency injection features.
  • Spring context: the context encapsulation package based on the core encapsulation package provides a framework object access method.
  • Spring dao:data access object provides an abstraction layer of JDBC.
  • Spring AOP: provides aspect oriented programming implementation, allowing you to customize interceptors, pointcuts, etc.
  • Spring Web: provides integration features for web development, such as file upload, IOC container initialization with servlet listeners, and ApplicationContext for the web.
  • Spring web MVC: the MVC package in spring provides the implementation of model view controller (MVC) for web applications.
  • Spring ORM: support for the existing ORM framework;

6. What are the common injection methods of spring?

  • Setter property injection
  • Construction method injection
  • Annotation mode injection

7. Are beans in spring thread safe?

The beans in spring are in singleton mode by default, and the spring framework does not encapsulate singleton beans in multithreading. In fact, most of the time, spring beans are stateless (such as Dao class), so beans are also safe to some extent. But if beans are stateful (such as view model object), it is up to the developer to ensure thread safety. The simplest thing is to change the scope of the bean and change “Singleton” to “prototype”, so that the request bean is equivalent to new bean (), Therefore, thread safety can be guaranteed. Statefulness means data storage. Stateless means that data will not be saved.

8. What are the scopes of beans in spring?

  • Singleton: there is only one bean instance in the spring IOC container. Beans exist in singleton mode, which is the default value of the system;
  • Prototype: each time a bean is called from the container, a new example will be created, that is, each getBean () is equivalent to executing a new bean () operation;
  • Request: each HTTP request will create a bean;
  • Session: the same HTTP session shares a bean instance;
  • Global session: used for portlet containers, because each portlet has a separate session. Globalsession provides a global HTTP session. Note: using prototype scope requires careful thinking, because frequent creation and destruction of beans will bring great performance overhead.

9. @qualifier notes

When there are multiple beans of the same type but only one needs to be assembled automatically, the @qualifier annotation and the @autowire annotation are used together to eliminate this confusion and specify the exact beans that need to be assembled.

10. What is the difference between @resource and @autowired annotations?

  • @Resources match beans by name by default, and support specifying types.
  • @Autowired matches beans by type by default. If we want to assemble by name, we can use it together with the @qualifier annotation.

11. What are the implementation principles and differences between the two dynamic agents at the bottom of spring?

  • The proxy object needs to implement the dynamic proxy interface provided by JDK.
  • The dynamic proxy is implemented through the jar package of cglib. This method only needs to inherit the target object

12. What are the implementation methods of spring transactions?

  • Declarative transaction: declarative transaction can also be implemented in two ways, based on XML configuration file and annotation (add @transaction annotation on the class).
  • Coding method: provides coding to manage and maintain transactions.

13. What are the characteristics of spring transactions?

Acid for short:

  • Atomicity: transactions are the smallest execution unit, and segmentation is not allowed. The atomicity of transactions ensures that actions are either completely completed or completely ineffective;
  • Consistency: the data is consistent before and after the transaction is executed;
  • Isolation: when accessing the database concurrently, a user’s affairs are not disturbed by other affairs, and the database is independent between concurrent transactions;
  • Durability: after a transaction is committed. Its changes to the data in the database are persistent, and even if the database fails, it should not have any impact on it.

14. Talk about the transaction isolation of spring?

Spring transactions have five isolation levels:

  • ISOLATION_ Default: by default, I use the isolation level set by the underlying database, and I use whatever is set by the database;
  • Isolationreaduncommitted: uncommitted reads. Before the lowest isolation level and transaction are committed, they can be read by other transactions (unreal reads, dirty reads, and non repeatable reads will occur);
  • Isolationreadcommitted: a transaction can only be read by other transactions after it is committed (which will cause unreal reading and non repeatable reading). The default level of SQL Server;
  • Isolationrepeatableread: it can be read repeatedly to ensure that when the same data is read many times, its value is consistent with the content at the beginning of the transaction. It is forbidden to read uncommitted data of other transactions (which will cause unreal reading). The default level of MySQL;
  • ISOLATION_ Serializable: serialization, the most expensive and reliable isolation level, which can prevent dirty reads, unrepeatable reads, and phantom reads.

15. Spring and dirty reading, unreal reading, non repeatable reading?

  • Dirty read: indicates that one transaction can read uncommitted data in another transaction. For example, a transaction attempts to insert record a, which has not yet been committed, and then another transaction attempts to read record a.
  • Non repeatable reading: refers to reading the same data multiple times in a transaction.
  • Unreal reading: refers to that the result sets returned by multiple queries in the same transaction are different. For example, the first query of the same transaction a has n records, but the second query under the same conditions has n+1 records, which seems to have an illusion. The reason for phantom reading is that another transaction adds or deletes or modifies the data in the first transaction result set. The data content of the same record is modified, and the records of all data rows become more or less.

16. What are the propagation behaviors of spring transactions?

Spring transactions have seven propagation behaviors:



That’s all for this article. I hope it can help you, and I hope you can pay more attention to developeppaer!