"Interview" "Spring FAQ" "07"

Source: Internet
Author: User

"Common Interview questions Summary folder >>>"

61. The Spring IOC container relies on two layers of meaning:

bean-dependent containers : That is, the bean relies on the container, where the dependency is that the container is responsible for creating the bean and managing the bean's life cycle, precisely because the container controls the creation of the bean and injects the dependency, that is, the control is reversed, which is the origin of the IOC name, Dependency here refers to the dependency between the bean and the container.

the container injects the bean's dependent resources: The container is responsible for injecting the bean's dependent resources, and the dependent resources can be beans, external files, constant data, etc., which are reflected as objects in Java. And the container is responsible for assembling the dependencies between the beans, and the dependencies here refer to the dependencies between the beans. Can be thought of as the "association", "aggregation", "combination" relationship between the traditional class and the class.

Why apply Dependency injection. What are the benefits of applying dependency injection?

Dynamically replace bean dependent objects with a more flexible program: Replace bean dependent objects without altering the source file: after applying dependency injection. Because it can be implemented by the configuration file, so that it can dynamically replace the bean's dependent object at any time, without altering the Java source file;

Better practice interface-oriented programming. The code is clearer: in the bean only need to specify the interface of the dependent object, the interface defines the function that depends on the completion of the object, through the container injection dependency implementation;

Better practice prioritizes object combinations rather than class inheritance: Because the IOC container uses injection dependency, which is the combination of objects, it is better to practice object combinations.

With the combination of objects, the bean function may be composed of several dependent bean functions, the bean itself may only provide a little function or no matter what function, all entrusted to the dependent bean, the object combination is dynamic, it can be more convenient to replace the dependent bean, thereby changing the bean function;

And assuming that the class inherits, the bean does not rely on the bean, but uses the inheritance method to add the new function, and the function is determined at compile time, not dynamic. And the use of class inheritance results in a high degree of coupling between the bean and the child bean, which is difficult to reuse.

Add Bean reusability: Depending on the object combination, the bean is more reusable and easier to reuse.

Reduce coupling between beans: because we are completely using interface-oriented programming, there is no direct reference to the bean dependency implementation in the code, all reference interfaces, and no display creation dependent object code appears. And these dependencies are injected by the container, very easy to replace the dependent implementation class. Thus reducing the coupling between bean and dependence;

The code structure is clearer: you want to apply dependency injection. The code structure should be written in accordance with the Statute. To better apply some best practices, the code structure is clearer.

63. What is cyclic dependence?

A circular dependency is a circular reference in which two or more beans hold each other. For example, Circlea refers to Circleb. Circleb Reference Circlec,circlec Reference Circlea, they are finally reflected as a ring.

64. How spring solves cyclic dependencies

constructor Cyclic dependency: represents a cyclic dependency that is composed of a constructor injection, which cannot be resolved and simply throws a beancurrentlyincreationexception exception to indicate a cyclic dependency.

Setter Cyclic dependency: represents a cyclic dependency that is composed of setter injection methods. The dependency on setter injection is completed by the spring container exposing the bean that has just completed the constructor injection but not completing other steps (such as setter injection), and only resolves the bean loop dependency of the singleton scope. For the "prototype" scope, the Bean,spring container cannot complete dependency injection. Because of the "prototype" scope of the bean. The spring container is not cached, so it is not possible to expose a created bean in advance. For the "singleton" Scope bean. able to pass "setallowcircularreferences (false);" To disable circular references.

65. Defer initialization of beans

Lazy initialization is also called lazy initialization, which means that the bean is created and initialized only when it is actually used, rather than initializing the bean in advance.

Very easy to configure simply specify the "Lazy-init" property value of "true" on the <bean> tab to defer initialization of the bean.

The spring container initializes the bean of the "singleton" scope in advance when the container is created, and "Singleton" is the singleton meaning that the entire container has only one instance per bean, which is described in detail behind it. Spring container pre-initialization beans can often help us to detect configuration errors ahead of time. So let's say there's nothing to suggest, unless there's a bean that might need to load very large resources, and it's very likely that it won't be available throughout the application life cycle. Can be set to deferred initialization.

A bean that is lazily initialized is typically initialized when it is first used, or is initialized when the bean is initialized with a non-deferred initialization bean as a dependency object, because a deferred initialization bean is used at this time.

The container management initialization bean eliminates the programmatic implementation of deferred initialization. Completely controlled by the container. Simply configure on a bean definition that requires lazy initialization, which is simpler than programming and has no intrusive code.

66. Using Depends-on

Depends-on refers to the order in which beans are initialized and destroyed, and the bean specified with the Depends-on property initializes the current bean after initialization, because only "singleton" beans can be destroyed by spring management. So when the specified bean is "singleton", the bean specified with the Depends-on property is destroyed after the specified bean.

What are the advantages of "depends-on"? The main is to give the understanding of the initialization and destruction sequence. Reduce the occurrence of errors.

67, not all types can be self-assembling:

Data types that cannot be actively assembled themselves: Object, base data type (Date, charsequence, number, URI, URL, Class, int), and so on.

The matching pattern specified by the "<beans>" tab Default-autowire-candidates property cannot be used as a candidate for its own active assembly, such as specifying "*service,*dao". Only the beans that match these patterns will be candidates, and the mismatches will not be candidates;

You can set the Autowire-candidate property of the "<bean>" tab to False. As a result, the bean will not be a candidate for dependency injection.

68, array, set, type of dictionary based on the type of their own active assembly and ordinary types of their own active assembly is a difference

array type, collection (set, Collection, List) interface type: All candidates that match are obtained from the generic and injected into the array or collection, such as "list

dictionary (MAP) interface type: the same by generic information injection, the key must be a String type bean name, the value is obtained according to the generic information, such as "Map<string, Helloapi>map" All of the Helloapi type beans are selected and injected into the map, and for a detailed dictionary type such as "hashmap<string,helloapi> map", only bean injection of type HashMap is selected. Instead of selecting all of the Helloapi type beans for injection.

69, the advantages and disadvantages of their own active assembly?

First of all, your own active assembly does reduce the amount of configuration files. Second, the "Bytype" self-assembly can actively update itself when the corresponding bean changes the field type. That is, changing the bean class does not require a configuration change. It's really simple.

Self-assembly is also flawed, the most important drawback is that there is no configuration, in the search for injection errors are troublesome, and for example, the basic type can not complete their own active assembly. So there may be some inexplicable errors, I recommend that you do not use this method, it is best to specify a clear injection method, or with the latest java5+ annotation injection method. So when you use your own active assembly, you should consider the complexity of your own project to measure whether you choose your own active assembly method.

70, their own active assembly injection method and configuration injection method to work together?

Of course I can. Configuring injected data overwrites the data that is injected by the active assembly itself.

"Interview" "Spring FAQ" "07"

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.