Spring Interview Issue Highlights

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

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 {    publicabstractvoid  handle ();}  Package principle_dip2;  Public Interface Animalhelper {    publicabstractvoid help ();}

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

 Packageprinciple_dip2; Public classCircusservice {Animalhandler handler;  Public voidSetHandler (Animalhandler handler) { This. Handler =handler; }     Public voidShowstarts () {//code omitted for brevityHandler.handle (); }} Packageprinciple_dip2; Public classTigerhandlerImplementsanimalhandler{Animalhelper Helper;  Public voidSethelper (Animalhelper helper) { This. Helper =Helper; }     Public voidhandle () {//...Helper.help (); //...    }} Packageprinciple_dip2; Public classTigerhelperImplementsanimalhelper{ Public voidHelp () {//......    }}

    • 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.

Spring Interview Issue Highlights

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.