Spring's dependency injection and how it works

Source: Internet
Author: User
Tags reflection

1. Set Injection
The IOC container uses setter methods to inject dependent instances
2. Construction Injection
Constructs an instance to complete the initialization of the dependent instance.
-
Create instances with different timing
Set injection, first create a caller instance through the parameterless constructor, and inject the dependency in the call to the corresponding setter method.
Constructs the injection, the direct call has the parameter constructs the method, the caller creates the completion, also completes the dependency relation injection.
Advantages and Disadvantages
Set injection, JavaBean similar, easy to understand, construction injected, bloated, difficult to read, low performance
Construct injection to determine the sequencing of dependencies, without worrying about subsequent code damage to dependencies, conforming to the cohesion clustering principle
3. Working principle

Spring's IOC dynamic injection, so that an object is created without new, can be automated production, which is actually the use of class reflection in Java, Reflection is actually in the runtime to create, invoke the object dynamically, spring is at runtime, with the XML The spring configuration file is used to dynamically create objects and invoke methods in the object.

One of the core aspects of spring is AOP, which is the aspect-oriented programming (like Hamburg) that can be supervised and controlled for a class of objects (that is, calling the module you specify before and after invoking the specific method of such an object) to extend the functionality of a module. These are achieved through the configuration class.

Spring purpose: The relationship between objects and objects (modules and modules) is not associated with code, but is managed through the Configuration class description (spring based on these configurations, internally by reflection to the dynamic assembly object)
Keep in mind that spring is a container in which the objects in the container will have these services and functions provided by spring.

One of the most classic design patterns used in spring is the template method pattern.

You can do it in layman's. Spring as a large parcel, inside can be new object, can integrate Hibernate, STRUTS2 and other framework configuration files.

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.