"Interview" "Spring FAQ" "07"

Source: Internet
Author: User

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, 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. The dependencies here refer to the dependencies between the beans, which can be thought of as the "association", "aggregation", "combination" relationship between the traditional classes and the classes.

Why should we apply dependency injection and how can application dependency injection benefit us?

Dynamically replace bean -dependent objects with a more flexible program: Replace bean dependent objects without modifying the source file: After applying dependency injection, it can be implemented by configuration file so that the bean's dependent object can be replaced dynamically at any time without modifying 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 of dependent object completion, through the container injection dependency implementation;

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

Using object composition, the Bean's function may be composed of several dependent bean functions, the bean itself may provide only a little or no function, all entrusted to the dependent bean, the object combination is dynamic, it is more convenient to replace the dependent bean, thereby changing the bean function;

In the case of class inheritance, the bean does not rely on the bean, but inherits the new functionality, and the function is determined at compile time, not dynamic, and class inheritance causes the bean to be highly coupled with the child bean, which is difficult to reuse.

Increase the reusability of the bean: Depending on the object combination, the bean is more reusable and easier to reuse;

Reduce coupling between beans: Since we are fully programming with interfaces, we do not directly refer to the bean dependency implementation in code, fully reference the interface, and do not appear to create dependent object code, and these dependencies are injected by the container, it is easy to replace the dependent implementation class, thus reducing the coupling between the bean and the dependency ;

The code structure is clearer: to apply dependency injection, the code structure is written in accordance with the specification to better apply some best practices, so the code structure is clearer.

63. What is cyclic dependence?

A cyclic dependency is a circular reference in which two or more beans hold each other, such as Circlea reference Circleb,circleb reference Circlec,circlec reference Circlea, and they are eventually 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 only 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 done through the spring container exposing beans that have just completed the constructor injection but not completing other steps (such as setter injection), and can only resolve the bean loop dependency of the singleton scope. Dependency injection cannot be completed for the "prototype" Scope bean,spring container because the bean,spring container of the "prototype" scope is not cached, so it is not possible to expose a created bean in advance. For the "singleton" Scope bean, you can pass "setallowcircularreferences (false);" To disable circular references.

65. Defer initialization of beans

Lazy initialization, also known as lazy initialization, is to create and initialize a bean only when it is actually used, rather than initializing the bean in advance.

Configuration is simple 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 for the "singleton" scope 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. Spring container pre-initialization beans often help us to detect configuration errors in advance, so if nothing is suggested to be turned on, unless there is a bean that might need to load a lot of resources and is likely to be unused throughout the application life cycle, it can be set to deferred initialization.

A bean that is lazily initialized is usually initialized on the first use, or is initialized when the bean is initialized with a non-deferred initialization bean as a dependent object because the deferred initialization bean is used at this time.

The container management initialization bean eliminates the programmatic implementation of lazy initialization, which is completely controlled by the container, simply configured on a bean definition that requires lazy initialization, simpler than programming, and without intrusive code.

66. Using Depends-on

Depends-on refers to the order in which the bean is initialized and destroyed, and the bean specified with the depends-on attribute initializes the current bean after initialization, since only the "singleton" Bean 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 good is "depends-on"? The main is to give a clear initialization and destruction sequence, to reduce the occurrence of errors.

67. Not all types can be assembled automatically:

Data types that cannot be automatically assembled: 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 attribute will not be used as a candidate for automatic assembly, for example, "*service,*dao", Only the beans that match these patterns will be candidates, and the mismatches will not be candidates;

By setting the Autowire-candidate property of the "<bean>" tag to False, the bean will not be a candidate for dependency injection.

68, array, set, dictionary type automatic assembly according to type and ordinary type of automatic assembly is different

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

dictionary (MAP) interface type: also according to the 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 Helloapi type beans are selected and injected into the map, and for specific dictionary types such as "hashmap<string,helloapi> map", only bean injections of type HashMap are selected. Instead of selecting all HELLOAPI type beans for injection.

69, the advantages and disadvantages of automatic assembly?

First, automatic assembly does reduce the amount of configuration files, and secondly, "bytype" automatic assembly can be updated automatically when the corresponding bean changes the field type, that is, modifying the Bean class does not need to modify the configuration, it is really simple.

Automatic assembly is also a disadvantage, the most important drawback is that there is no configuration, in the search for injection errors are very troublesome, as well as basic types can not complete the automatic assembly, so may often occur some inexplicable errors, I recommend you do not use this way, it is best to specify a clear injection method, Or use the latest java5+ annotation injection method. So when you use automatic assembly, you should consider the complexity of your own project to measure whether or not to choose an automatic assembly method.

70, automatic assembly injection method and configuration injection method to work together?

Of course, configuring injected data overrides automatic assembly injection.

Interview, Spring FAQ Summary, "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.