Common face questions in spring

Source: Internet
Author: User

1 What are the benefits of using the spring framework?
Lightweight: Spring is lightweight, with a basic version of about 2MB.
Control inversion: Spring is loosely coupled through control inversion, and objects are given their dependencies rather than creating or locating dependent objects.
Aspect-oriented programming (AOP): Spring supports tangent-oriented programming and separates application business logic from system services.
Containers: Spring contains and manages the life cycle and configuration of objects in your app.
MVC framework: Spring's web Framework is a well-designed framework and a good alternative to web frameworks.
Transaction management: Spring provides a continuous transaction management interface that can be extended up to the local transaction down to the global transaction (JTA).
Exception handling: Spring provides a convenient API for translating specific technology-related exceptions (such as those thrown by jdbc,hibernate or JDO) into a consistent unchecked exception.


2. What is the concept of AOP and Ioc/di and how is it applied in spring?
Aop,aspect oriented program, programming for (aspect) facets;
Ioc,invert of control, inversion of controls. An instance of the object is automatically generated by the container,
That is, interface programming, in the program does not appear the new keyword, but the interface to name the reference,
Then, an instance of an implementation class of an interface is injected into a reference to achieve the loose coupling between the interface and the concrete implementation class.
The relationship between the container control program (through XML configuration), rather than the traditional implementation of the code directly manipulated by the program,
Dependency injection, an aspect of the IOC, is a common concept, and it has many interpretations. The idea is that you don't have to create objects,
You just need to describe how it was created. You do not assemble your components and services directly in the code, but in the configuration file describe which components require which services,
Then a container (the IOC container) is responsible for assembling them together.
To put it simply, the IOC is actually a dependency injection,
(When referencing another class object in a class object, we usually go directly through new contructor).
Control is transferred from the application code to the external container, and the transfer of control is called reversal.
The AOP approach is similar to filter, which is to insert many other code that needs to be executed in the middle of a normal business flow, like a slice.
For example, log in to log in before entering the login page, it is very common, especially with the database, or the payment related programs will definitely insert the log in front of each step.
Aspect-oriented programming, AOP, is a programming technique that allows programmers to modularize the behavior of crosscutting concerns or crosscutting typical lines of responsibility, such as logging and transaction management.
The core structure of AOP is the facet, which encapsulates behaviors that affect multiple classes into reusable modules.
AOP Advice (AOP notifications) are divided into:
Pre-notification post notification exception notification surround notification


3 : There are several ways to do things in spring? What are the benefits of the transaction management of the spring framework? What type of transaction management are you more inclined to use? Talk about the isolation level and propagation behavior of spring things?
Programmatic transaction management: This means that you manage transactions programmatically, giving you great flexibility, but difficult to maintain.
Declarative transaction management: This means that you can separate business code from transaction management, and you only need to manage transactions with annotations and XML configurations.
It provides a constant programming pattern for different transactional APIs such as JTA,JDBC,HIBERNATE,JPA and JDO.
It provides a simple set of APIs for programmatic transaction management rather than some complex transactional APIs such as
It supports declarative transaction management.
It is well integrated with spring's various data access abstraction layers.
Most spring framework users choose declarative transaction management because of its minimal impact on application code,
Thus more in line with the idea of a non-intrusive lightweight container. Declarative transaction management is superior to programmatic transaction management,
Although it is less flexible than programmatic transaction management, which allows you to control transactions through code.


Isolation level of the transaction:
The database system provides 4 transaction isolation levels, with the highest isolation level in the 4 isolation levels and the lowest isolation level of Read uncommitted;
· READ UNCOMMITTED reading uncommitted data; (dirty reads will occur)
· Read Committed reading submitted data;
· REPEATABLE read repeatable reading;
The propagation properties of a transaction include:
· The required business method needs to run in a transaction, and if a method is already in a transaction, it is added to the transaction, or a new transaction is created for itself.
The 80% method uses the propagation attribute;
· Not-supported RequiresNew Mandatoky Supports Never Nested


4: explains the life cycle of beans in the spring framework and several scopes supported by spring.
The spring container reads the bean definition from the XML file and instantiates the bean.
Spring populates all properties based on the Bean's definition.
If the bean implements the Beannameaware interface, Spring passes the Bean's ID to the Setbeanname method.
If the bean implements the Beanfactoryaware interface, spring passes beanfactory to the Setbeanfactory method.
If any beanpostprocessors,spring associated with the bean are called within the Postprocesserbeforeinitialization () method.
If the bean implementation is Intializingbean, call its Afterpropertyset method, and if the bean declares the initialization method, call this initialization method.
If there are beanpostprocessors and bean associations, the Postprocessafterinitialization () method of these beans will be called.
If the bean implements Disposablebean, it will call the Destroy () method.
The spring framework supports the following five types of bean scopes:
Singleton:bean has only one instance in each spring IOC container.
Prototype: the definition of a bean can have multiple instances.
Request: Each HTTP request creates a bean that is only valid in the case of a web-based spring ApplicationContext.
Session: In an HTTP session, a bean definition corresponds to an instance. This scope is valid only in the case of a web-based spring ApplicationContext.
Global-session: In a global HTTP session, a bean definition corresponds to an instance. This scope is only available in the Web-based springApplicationContext the case is valid.


5: Tangent-oriented principle

Spring provides a good encapsulation of AOP technology, AOP is called aspect-oriented programming, is the system has a lot of unrelated classes of methods, in these numerous methods to add some system function code, such as: Log-in, permission to judge, exception processing, this application becomes AOP.
The implementation of the AOP function is the agent technology, the client program no longer calls the target, and the calling proxy class, the proxy class and the target class have the same method declaration, there are two ways to implement the same method declaration, one is to implement the same interface, Instead, the subclass of the target uses the proxy class in the JDK to generate the implementation class for an interface, and if you want to generate subclasses for a class, you can use Cglib. (Dynamic agent In this respect can refer to my other article)
In the method of the generated proxy class to add the system function and call the corresponding method of the target class, the system function agent is provided by the advice object, obviously to create the proxy object, at least the target class and the advice class are required.










Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Common face questions in spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.