Spring Common Interview Questions

Source: Internet
Author: User
Tags finally block enterprise integration patterns apache camel

Spring

1. Spring working mechanism and why should it be used?
Spring is an open source framework that is created to address the complexities of enterprise application development. Spring is both an AOP framework and an IOC container.
Springframework's composition: Spring aop,spring dao,spring orm,spring web,spring Context, Spring Web MVC.
The core of spring is IOC and AOP, so spring's working mechanism is simply understood as the working mechanism of IOC and AOP.

The Spring aop,spring IOC makes it easy to use very robust, flexible enterprise-class services, which can reduce coupling between components by using IOC, ultimately improving class reusability, testing, and better integration and configuration of the entire product or system.

2. What is the concept of AOP and IOC and how is it applied in spring?
Aop,aspect oriented program, programming for (aspect) facets;
Ioc,invert of control, inversion of controls.

To put it simply, the IOC is actually a dependency injection, that is, the interface programming, in the program does not appear the new keyword, but the interface to name the reference, and then in some way the interface of an instance of an implementation class injected into the reference, so that the interface and the implementation of the specific class loosely coupled.

The relationship between the container control program (through XML configuration), rather than being manipulated directly by the program code in the traditional implementation, (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, such as when logging in to log in before entering the login page, which is very common, especially in connection with the database, or the program that is related to the payment 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:

Front-facing notifications
Post notification
Exception notification
Surround Notifications

3. There are several ways to do things in spring? Talk about the isolation level and propagation behavior of spring things?

Declarative transactions use spring declarative transactions, and spring uses AOP to support declarative transactions, automatically deciding whether to open a transaction before a method call, based on the transaction properties, and determining the transaction commits or rolls back after the method executes.

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;
· Serializable serialization

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, otherwise a new transaction is created for itself, and the 80% method uses that propagation property;
· not-supported
· RequiresNew
· Mandatoky
· Supports
· Never
· Nested

4. What are the advantages and disadvantages of spring?
Advantages of ⑴spring:
①spring can effectively organize your middle-tier objects, whether or not you choose to use EJBS;
②spring can eliminate the overuse of singleton that is common in many projects. (because it reduces the testability of the system and the degree of object-oriented);
③ processing configuration files through a consistent approach across applications and projects, spring eliminates the need for properties files in a variety of custom formats. The use of inversion of control has helped to complete this simplification;
④ the cost of programming the interface rather than the class is almost reduced to no, spring can promote good programming habits;
⑤spring is designed to allow applications created with it to rely as little as possible on his APIs. Most of the business objects in the spring application are not dependent on spring;
⑥ applications built using spring are easy to unit test;
⑦spring can make EJB usage an implementation choice, not an inevitable choice for application architectures. You can choose to use POJOs or local EJBS to implement the business interface without affecting the calling code;
⑧spring helps you solve many problems without using EJBS. Spring provides an alternative to EJBS that can be used in many Web applications. For example, spring can use AOP to provide declarative transaction management instead of through EJB containers;
⑨spring provides a consistent framework for data access regardless of whether you are using JDBC or O/R mapping products;

Disadvantages of ⑵spring:
① use a few people, JSP to write a lot of code;
The ② controller is too flexible and lacks a common controller.

http://blog.163.com/qqabc20082006%40126/blog/static/22928525201062491347114/

Q. for the dependency inversion principle (Dependency inversion Principle,dip), Dependency injection (Dependency injection,di) and control inversion (inversion of controls, IoC) container, how do you understand that?

A.

    • dependency Inversion principle (Dependency inversion Principle, DIP). This design guideline is somewhat related to the dependency injection pattern in some way. Dip is the starting point: in application development, high-level modules should not be directly dependent on the lower modules. Dip does not imply dependency injection. This guideline does not describe how the high-level module knows which low-layer module to invoke. However, this can be learned indirectly by implementing a factory-mode interface, or by implementing a dependency injection with a LOC container like the spring Framework, Pico container, Guice, or Apache hivemind to know which low-level module the high-layer module invokes.

Dip means:

    • High-level modules should not rely on low-layer modules, they should all rely on abstraction.
    • Abstractions should not rely on concrete implementations. Concrete implementations should rely on abstraction.

After applying this criterion, the high-level module does not interact directly with the lower-layer modules, but with the lower-level modules through an abstraction. This makes it more flexible and controllable to increase the cost after a change in demand. Here are some sample code snippets to implement the dip.

First define the abstraction layer:

Package Principle_dip2;public interface Animalhandler {public    abstract void handle (); Package Principle_dip2;public interface Animalhelper {public    abstract void Help ();}

This is followed by high-level code that relies on abstract classes rather than concrete implementations.

Package Principle_dip2;public class Circusservice {    Animalhandler handler;    public void SetHandler (Animalhandler handler) {        This.handler = handler;    }    public void Showstarts () {        //code omitted for brevity        handler.handle ();}    } Package Principle_dip2;public class Tigerhandler implements animalhandler{    Animalhelper helper;    public void Sethelper (Animalhelper helper) {        this.helper = helper;    }    public void handle () {        //...        Helper.help ();        //...    }} Package Principle_dip2;public class Tigerhelper implements animalhelper{public    void Help () {        //...    }}

    • Dependency Injection Mode (Dependency injection): Injects the dependency of a class into code at run time. This pattern is implemented by defining the dependency as an interface and injecting the entity class that implements the interface into the constructor of the main class. This allows programmers to switch between different implementations without having to modify the main class. The dependency injection pattern allows the code to high cohesion (high cohesion)via the single -Responsibility PrincipleSRP. Because they are often dependent on objects that perform independent functions, such as data access (through DAO) or business services (implemented through the service and delegate classes).
    • The Control reversal container (inversion of control CONTAINER,IOC) is a container that supports dependency injection. In this way, you can use a central container, such as the spring Framework, Guice, or hivemind, to define which dependency should be used for which entity class. The loose coupling of the IOC provides more flexibility and makes it easier to switch to the right dependent object when the program is running. The basic concept of control reversal mode is that instead of actually generating objects, you define how the objects are generated. Instead of hard-coding the modules and services directly in the code, it describes in the configuration file which module needs which service. containers, such as the spring Framework, the IOC container, are responsible for binding the two together. When the IOC is applied, the dependencies required by an object are passed in at the time of creation through external entities, which are used to coordinate different objects in the system. In other words, the dependency is injected into the object. Therefore, the IOC is a mechanism for reversing the responsibility of how an object obtains a reference to its collaboration object.

The real strength of DI and IoC is that they bind class relationships at runtime and not at compile time. For example, in the seam framework, you can implement two implementations of an interface: a real implementation and a mock (mock) implementation, and at run time, depending on a property, the existence of another file, or a certain priority value to decide which implementation to actually invoke. This is especially useful when you want the program to behave differently in different scenarios. Another benefit of Di and IOC is that it makes it easier to unit test the code. There are, of course, other benefits, such as loose coupling without the use of a factory or singleton mode, which is consistent with the implementation of the inexperienced programmer, and so on. Of course, there is a cost to enjoying these benefits, such as the increased complexity of the system, and the need to be more careful when used, not because the technology is popular and misused, but only where it can truly embody its advantages.

Note : Context-dependent injection (contexts and Dependency injection) is an attempt to describe standard dependency injection. CDI is part of the Java EE 6 stack, which means that any application running on top of a Java EE 6 compliant container can easily use CDI. Weld is a reference to the CDI implementation.

Q. in your experience, why choose to use the spring framework?

A.

    • With more than 20 modules available, spring is structured in a hierarchical structure. This means that you can choose freely according to your needs. Spring simplifies the use of Java EE by programming with a simple (Plain old Java Object,pojo) object. There is nothing special about Java EE programming in spring. Pojo programming provides continuous integration of code and ease of measurement.

  • The core function of the spring framework is Dependency injection (DI). DI makes unit testing of code more convenient, better system maintenance, and more flexible code. The DI code itself is easy to test by constructing a black-box test that implements the "mock" object of the interface that the application needs to function. The DI code is also easier to reuse because its "dependent" functionality is encapsulated in well-defined interfaces, allowing other objects to be inserted into the desired objects as needed, which are configured in other application platforms. The DI code is more flexible and because of its innate loose coupling, it allows programmers to determine how objects are correlated by simply considering the interfaces they need and the interfaces exposed by other modules.
  • Spring supports aspect-oriented programming (Aspect oriented Programming, AOP), allowing for the development of cohesion by separating the application of business logic and system services. AOP supports auditing (auditing), collecting performance and memory metrics, and more.
  • Spring also provides a number of template classes that implement basic functionality, making it easier to develop a Java EE. For example, the JdbcTemplate class and the JDBC, Jpatemplate class, and Jpa,jmstemplate classes and JMS can all be used well together. The Resttemplate class is very concise, and the code that uses this template is very readable and maintainable as well.
  • It is important to try to split the middle tier code out of the business logic. The best way to make remote calls is to take advantage of spring's remote interface invocation, which supports the use of any message or remote technology to complete a remote invocation. Apache Camel is a powerful open-source integration framework based on known enterprise integration patterns including bean integration. The Apache camel design was designed to work well with the spring framework as much as possible.
  • Spring provides declarative transaction processing, job scheduling, identity authentication, a mature MVC web framework, and integration with other frameworks such as Hibernate, IBatis, JasperReports, JSF, Struts, Tapestry, Seam and quartz job scheduler and so on.
  • Spring Bean objects can be shared between different jvms through terracotta. This allows the use of existing beans to be shared in the cluster, the spring application context event into a distributed event, and the ability to export cluster beans through spring jmx, making the spring application highly available and clustered. Spring can also be integrated with other cluster application scenarios, such as Oracle's coherance.
  • Spring prefers to use unchecked exceptions (unchecked exceptions) and reduce improper try,catch and finally block of code (or the Try/catch block in finally). Spring template classes like Jpatemplate are responsible for shutting down or releasing database connections, which avoids potential resource leaks and improves code readability.
  • In the DI framework, which is not spring or Guice, Factory mode and singleton mode can be used to improve the loose coupling of the code. The use of spring can effectively avoid the misuse of these patterns.

2. based on your project experience, what parts of the spring framework do you dislike? Do you think spring is flawed?

A

    • Spring has become too big and bulky. Therefore, my advice is not to use all of its features without thinking about spring's praise. Instead, you should use the features in spring that are really useful to your project. In most cases, the use of a framework such as mature spring is much better than building a similar solution from scratch, in terms of maintainability and non-repetition of wheel-making. For example, all spring templates (JDBC,REST,JPA, and so on) have the following advantages: they are built in a consistent way, so you can skip these general steps to focus on more important business logic.
    • Spring MVC is not necessarily the best web framework. There are other, for example, Struts 2,wicket and JSF. That said, spring can also be well integrated with these frameworks (STRUTS,JSF, etc.).
    • XML files are too bloated, and this can be improved by other means. For example, use Annotations,javaconfig or use a standalone XML configuration file.

2. What types of dependency injection are supported in the IOC?

A. There are three types of dependency injection:

    • Construct sub-injection (for example, Spring Framework): dependencies are provided through constructor parameters.
    • Set Value method injection (for example, Spring Framework): Dependency is injected through the JavaBeans attribute (Ex:setter method)
    • Interface injection (for example, Avalon): injection is done through the interface.

Q. have you used other dependency injection frameworks?

A. used Guice,hivemind and seam.

Http://www.cnblogs.com/Coda/p/4232897.html

Spring Common interview questions (GO)

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.