Spring Learning Path (ii)

Source: Internet
Author: User

What is a dependency relationship?

Throughout all Java applications, from applet-based small applications to multi-tiered enterprise-level applications, they are a typical dependency application, which is made up of a number of collaborative objects, and spring calls this collaborative relationship a dependency, adding a component that calls the B component's method , we can understand that a component depends on the B component.

What is dependency injection (inversion of control)?

The so-called dependency injection, that is, when the program is running, if you need another object to collaborate (call the method of the object, access its properties), you do not need to create the callee in the code, but rely on the injection of the external container. Spring's dependency injection has virtually no requirement for callers and callers to fully support the management of dependencies between Pojo.

The simple understanding is that when a Java instance (the caller) needs another Java instance (callee), in the traditional program design process, there is usually a caller to create an instance of the caller, while in the dependency injection mode, the creation of the callee's work is not done by the caller, so called control reversal The creation of an instance of the callee is usually done by the spring container and then injected dynamically to the caller, and therefore called Dependency injection;

To give a simple example, such as a boy wants to find a girlfriend, if the boy is still in college, he will participate in various clubs, competitions, parties and so on, do not miss any opportunity to contact the sister, if met like sister, the initiative to inquire sister's phone, Weibo, QQ and other sister's contact, personal information, But if the boy has been working, but also in the company (overtime, overtime, or overtime) work, he may not have spare time to find a sister, how to do, work is busy, girlfriend or want to have, a variety of marriage dating site, the site automatically recommended to you the right girlfriend. Looking for sister's work to make a website, this process is called control reversal, dating site is equivalent to a spring container, according to the conditions you fill in the dating, to recommend suitable candidates for you.

How does spring implement dependency injection?

      • Set value injection: The IOC container uses the setter method of the property to inject the dependent instance. (This method is simple and intuitive, in the actual development used more than)

The process of setting value injection----""

Spring will automatically take over each <bean. /> Definition of <property: /> element definition, Spring will invoke the default bean instance after it calls the parameterless constructor, and call the corresponding setter method to inject the property value into the program, <property. The attribute values defined by/> are no longer actively set by the bean, but instead accept the injection of spring (<property name= "..." value= "injected Value" >). This method can not only inject common attributes, but also spirng can inject the bean (<property name= "..." ref= "Dependent Bean ID" >) for the target bean.

      • Construct injection: The IOC container uses constructors to inject dependent instances.

The process of construction injection----""

Construction injection is done by invoking the parameter constructor, and when the bean instance is created, it has completed the injection of the dependency relationship (<constructor-arg ref= "..."/>).

Comparison of two kinds of injection methods

      • The dependency relationship set by setting value injection (setter method injection) is more intuitive and natural.
      • The time to create a dependent component (Bean) is different, the value injection is to create a bean instance through the parameterless constructor, then call the corresponding setter method to inject the dependency, and the second construct is to call the constructor directly, and when the bean instance is created, the injection of the dependency relationship is completed.
      • For complex dependencies, a construction injection can cause the constructor to be bloated, and spring will need to instantiate all of the components it relies on when creating the bean instance, which can result in performance degradation and can be avoided by using Set value injection.
      • Construction injection can determine the injection order of dependencies in the constructor, prioritizing the priority injection of dependency.
      • For beans that do not need to change dependencies, the construction injection works better because there is no setter method, all dependencies are set in the constructor, and there is no need to worry about subsequent code modifications that will damage the dependencies.

Spring Learning Path (ii)

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.