SPRING3.0 official website of the Learning Notes document (VI)--3.4.1

Source: Internet
Author: User

3.4 Rely on
3.4.1 Dependency Injection
Dependency Injection two ways: Based on the constructor Di, setter-based method di.
3.4.1.1 based on the constructor di
parameter is the introduction of an object. and lack of parents before-subclass relationship:

 Package x.y;  Public class Foo {public   Foo (bar bar, Baz Baz) {      //...  }}

<Beans> <Bean ID="foo" class="X.y.foo"> <Constructor-arg ref="Bar"/> <Constructor-arg ref="Baz"/> </bean> <Bean ID="Bar" class="X.y.bar"/> <Bean ID="Baz" class="X.y.baz"/></beans>
When a simple type is used. Spring is not good at determining the type of value.

 PackageExamples Public classExamplebean {//No. of years to the calculate the Ultimate Answer  Private intyears;//The Answer to life, the Universe, and everything  PrivateString Ultimateanswer; PublicExamplebean (intYears, String Ultimateanswer) { This. Years = years; This. ultimateanswer = Ultimateanswer; }}

<Bean ID="Examplebean" class="examples. Examplebean "><Constructor-arg type="int" value="7500000"/><Constructor-arg type="Java.lang.String" value=" A"/></bean>
Or you can use index to specify the order in which the parameters are set.

<Bean ID="Examplebean" class="examples. Examplebean "><Constructor-arg Index="0" value="7500000"/><Constructor-arg Index="1" value=" A"/></bean>
For spirng3.0, you can also use name to specify the name of the property you want to set.

<Bean ID="Examplebean" class="examples. Examplebean "><Constructor-arg name="Years" value="7500000"/><Constructor-arg name="Ultimateanswer" value=" A"/></bean>
To get this method to work, remember:Your code must is compiled with the debug flag enabled (What do you mean? ) or to use @constructorproperties:

 PackageExamples Public classExamplebean {//Fields omitted@ConstructorProperties ({"Years","Ultimateanswer"}) PublicExamplebean (intYears, String Ultimateanswer) { This. Years = years; This. ultimateanswer = Ultimateanswer; }}
3.4.1.2 based on the setter method di

3.4.1.3 Dependency Parsing process
After the container is created. Each bean is validated with a valid validation of the property referenced by the Bean. However, the properties that the bean references are only created after the bean is created.

A singleton bean is created when the container is created. Other beans are only practical when they are requested.
Spring sets the properties or resolves dependencies as late as possible.


A depends on B, then B is created first and then A is created.
3.4.1.4 di Example
Use the static factory method to pass the number of references.


<Bean ID="Examplebean" class="examples. Examplebean "    Factory-method="CreateInstance"><Constructor-arg ref="Anotherexamplebean"/><Constructor-arg ref="Yetanotherbean"/><Constructor-arg value="1"/></bean><Bean ID="Anotherexamplebean" class="examples. Anotherbean "/><Bean ID="Yetanotherbean" class="examples. Yetanotherbean "/>
 Public classExamplebean {//A private constructor  PrivateExamplebean (...) {    ...  }//a static factory method; The arguments to this method can be  //Considered the dependencies of the bean is returned,  //Regardless of how those arguments is actually used.   Public StaticExamplebean CreateInstance (Anotherbean anotherbean, Yetanotherbean Yetanotherbean,inti) {Examplebean EB =NewExamplebean (...);//Some other operations ...      returnEb }}

    


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

SPRING3.0 official website of the Learning Notes document (VI)--3.4.1

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.