Home » Java » Spring common interview questions

Spring common interview questions

What are the benefits of using the Spring framework for 1?

Lightweight: the Spring is lightweight, the basic version is about 2MB.

Inversion of control: Spring implements loose coupling through control inversion, where objects give their dependencies instead of creating or locating dependent objects,.

Aspect oriented programming (AOP):Spring supports face oriented programming and separates application business logic and system services from.

Container: Spring contains and manages the object life cycle and configuration.
in the application
The MVC framework: the WEB framework of Spring is an elaborate framework that is a good replacement for the Web framework,.

Transaction management: Spring provides a continuous transaction management interface that can be extended to local transactions (JTA).

Exception handling: Spring provides a convenient API that transforms specific technical related exceptions (such as those thrown by JDBC, Hibernate, or, JDO) into consistent unchecked exception.

2. talk about the concepts of AOP and IOC/DI and how they are used in spring,

AOP, Aspect, Oriented, Program, aspect oriented programming (
IOC, Invert, Of, Control, control inversion. Instances of objects are automatically generated by the container,

Interface programming, in the program does not appear new keyword, but to use the interface to refer to reference,

Then, in some way, the instance of an implementation class of the interface is injected into the reference, so that the interface is loosely coupled with the concrete implementation class.

The relationship between the container control procedures (through the XML configuration), rather than the traditional implementation of the direct manipulation by the program code,

Dependency injection is an aspect of IOC, a common concept that has many interpretations. The idea is that you don't have to create objects,

Instead of just describing how it was created, you do not assemble your components and services directly in the code, but describe what components need to be served in the configuration file,

After that, a container (IOC container) is responsible for assembling them,.

To put it simply, IOC is actually dependency injection,

(when we refer to another Class object in a Class object, we usually pass it directly through new contructor),.

Control rights are transferred from the application code to the external container, and the transfer of control is called reverse.

AOP is very similar to filter, that is, in the normal business flow of the program, like the section, insert many other code that needs to be executed,

For example, when logging in, it is common to log in before entering the login page, especially with the database, or with the payment program, which must insert the log.
before each step
Aspect oriented programming, or AOP, is a programming technique that allows programmers to model.
for crosscutting concerns or crosscutting behaviors typical of duty boundaries (such as logging and transaction management)
The core construct of AOP is aspect, which encapsulates the behavior that affects multiple classes into reusable modules,.

AOP Advice (AOP notification) is divided into:

Pre notification, post notification, exception notification, surround notification,

3:Spring has several ways of doing things. What are the advantages of transaction management in the Spring framework? Are you more inclined to use that type of transaction management? Talk about the isolation level and propagation behavior of spring objects?

Programming transaction management: this means that you manage things programmatically, which gives you great flexibility, but it's hard to maintain.

Declarative transaction management: this means that you can separate business code from transaction management, and you simply manage the transaction.
with annotations and XML configuration
It provides an immutable programming pattern for different transaction API such as JTA, JDBC, Hibernate, JPA, and JDO,.

It provides a simple set of API for programmatic transaction management rather than some complex transactions such as
It supports declarative transaction management,.

It is well integrated with Spring's various data access abstraction layers,.

Most users of the Spring framework choose declarative transaction management because it has minimal impact on application code,

Therefore, it is more in line with the idea of an intrusive lightweight container. Declarative transaction management is better than programmatic transaction management,

Although there is less flexibility than programming transaction management (which allows you to control transactions by code),.

Isolation level of transaction:

The database system provides 4 transaction isolation levels, with the highest isolation level in the 4 isolation levels, and the lowest isolation level for Read Uncommitted;
; Serializable
Read Uncommitted reads uncommitted data; (dirty reads)

Read data submitted by Read Committed;

Repeatable Read can read repeatedly;

Transaction propagation properties include:

The Required business method needs to run in a transaction. If a method is already running in a transaction, add it to the transaction, otherwise create a new transaction for yourself,

80% uses this propagation attribute;

• Not-Supported, Requiresnew, Mandatoky, Supports, Never, Nested

4: explains the bean lifecycle of the Spring framework and several scope Spring support for.

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

Spring populates all attributes according to the definition of bean.

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

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

If there is any BeanPostProcessors associated with bean, Spring will call them in the postProcesserBeforeInitialization () method.

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

If there is a connection between BeanPostProcessors and bean, these bean's postProcessAfterInitialization () methods are called.

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

The Spring framework supports the following five domains of bean:

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

Prototype: a bean definition can have multiple instances,.

Request: each HTTP request creates a bean that is valid only in the case of web based Spring ApplicationContext.

Session: in a HTTP Session, a bean definition corresponds to an instance that is valid only in the case of.
Spring ApplicationContext based on Web
Global-session: in a global HTTP Session, a bean definition corresponds to an instance that is scoped only to web based Spring

in ApplicationContext case

5: the principle of section oriented

Spring provides a good package of AOP technology, AOP is called aspect oriented programming, there are many methods of each class is irrelevant in these systems, numerous methods to add a system function code, for example: add the log, the authority to determine the exception handling, this application become AOP.

AOP function realization using the agent technology, the client program no longer call, and call the proxy, the proxy class and target class has the same external method declaration, there are two ways to realize the method statement the same, one is to achieve the same interface, but as the target subclass in JDK by Proxy from a dynamic proxy for interface implementation class, if you want to subclass a class, you can use the CGLIB. (dynamic proxy it can refer to another article I > add corresponding method system function and call the target class in the generated proxy class in the system function of the agent to provide a Advice object, apparently to create a proxy object, at least the target class and the Advice class.

copyright notice: This article is original article for blogger. Without permission from blogger,.

must not be reprinted