[Spring] Denpendency Injection

Source: Internet
Author: User

  

DI Dependency Injection Dependency Injection: A design pattern for removing dependencies from program code.

This makes it easier to manage and test your application.

DI makes our program code loosely coupled. Loose coupling.

To better understand Di, first understand Dependency Lookup (DL):

Dependency Lookup is a way to get resources on demand. (JNDI)

Disadvantages:

Complex, dependent, not easy to test.

Dependency injection is the removal of this dependency on the program.

In this case, we can get the source from an external source, such as an XML file.

This makes the code loosely coupled and easier to test.

Written like this:

You only need to create the object, then the set method and constructor.

However, the constructor is not required for personal real-world testing. Just creating this object from the property element is not necessary. But if classes that rely on this class pass ref attribute or <constructor-arg><ref bean= "*"/ >, the corresponding constructor is required.

class employee{  address address;    Employee (address address) {  this. address=address;  }     Public void setaddress (address address) {  this. address=address;  }    }   

In this case, the instantiation of address is provided by an external source, such as an XML file, rather than by a set method or constructor. (Profile: A set method is required, and the name of the bean in the XML file needs to correspond to the Set method).

The spring framework provides two ways to inject dependencies.

constructor function

Setter method.

--Here's how to construct the function:

The constructor is instantiated with the Constructor-arg child element under the bean element, which requires the constructor in the class to correspond to the bean's parameter list one by one.

One way is to enter only the value and type, allowing the constructor to judge for itself.

One is injected sequentially by index, which automatically transforms int and string.

There is also a need to use name to specify parameter names to avoid confusion, such as when there are two parameters of the same type in a constructor.

If you do not specify a parameter type, there is only one value, and the constructor of type string is called by default.

----for list<t>, use the <list> element.

If the generic is a collection of type string. Use <list> <value> values </value>

If it is not a string, use <ref bean= "Bean_name_1" >

-For Map<string,string>

<constructor-arg>  <map>  <entry key= "Java is a programming Language"  value= "Ajay Kumar" ></entry>  <entry key= "Java is a Platform" value= "John Smith" ></entry>  <entry key= " Java is a island "value=" Raj Kumar "></entry>  </map>  

-For Map<object,object>

<constructor-arg>  <map>  <entry key-ref= "Answer1" value-ref= "user1" ></entry>  <entry key-ref= "Answer2" value-ref= "user2" ></entry>  </map>  </constructor-arg>

The inheritance of the---bean. The parent attribute can be inherited from the bean tag to inherit the properties of the parent bean.

If the parent bean uses the Constructor-arg,child bean, it also needs to be used.

Property in the same vein.

--Setting the bean through the property child element is the setter method injection.

---setter method injected collection<string>:

--Non-string, is to add <ref bean= "" in the label between <list> etc./>

--map<k,v>

Https://www.javatpoint.com/spring-tutorial-setter-injection-with-non-string-map

The difference between-----------------------------------constructor and setter injection---------------

1. Partial dependency: Can be injected through setter, but constructor (must have constructor of corresponding parameter list).

The 2.overriding:setting method can override the constructor injection. If we use both, the IOC container is injected with a setter.

3. Changes:setter injection makes it easy to change the value (and does not create a new bean instance), and constructor injection creates a new instance of the bean.

So the setter is more flexible.

[Spring] Denpendency Injection

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.