Spring 3 Dependency injection

Source: Internet
Author: User

[Html]
DI exists in two major variants, Constructor-based dependency injection and Setter-based dependency injection.
1. Constructor-based dependency injection)

The parameters in the constructor can be native data type or reference type.

Reference Type:

[Html]
Public class SimpleMovieLister {
 
// The SimpleMovieLister has a dependency on a MovieFinder
Private MovieFinder movieFinder;
 
// A constructor so that the Spring container can be 'inobject' a MovieFinder
Public SimpleMovieLister (MovieFinder movieFinder ){
This. movieFinder = movieFinder;
}
 
// Business logic that actually 'uses 'the injected MovieFinder is omitted...
}
[Html]
Package x. y;
Public class Foo {
Public Foo (Bar bar, Baz baz ){
//...
}
 
}
[Html]
<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>


Simple Type:

[Html]
Package examples;
 
Public class ExampleBean {
 
// No. of years to the calculate the Ultimate Answer
Private int years;
 
// The Answer to Life, the Universe, and Everything
Private String ultimateAnswer;
 
Public ExampleBean (int years, String ultimateAnswer ){
This. years = years;
This. ultimateAnswer = ultimateAnswer;
}
}

Method 1 pass type:
[Html]
<Bean id = "exampleBean" class = "examples. ExampleBean">
<Constructor-arg type = "int" value = "7500000" type = "codeph" text = "codeph"/>
<Constructor-arg type = "java. lang. String" value = "42"/>
</Bean>
Method 2 Index:

[Html]
<Bean id = "exampleBean" class = "examples. ExampleBean">
<Constructor-arg index = "0" value = "7500000"/>
<Constructor-arg index = "1" value = "42"/>
</Bean>

After Spring3.0, the @ ConstructorProperties annotation method is also provided to inject constructor parameters.

[Html]
<Bean id = "exampleBean" class = "examples. ExampleBean">
<Constructor-arg name = "years" value = "7500000"/>
<Constructor-arg name = "ultimateanswer" value = "42"/>
</Bean>
[Html]
Package examples;
 
Public class ExampleBean {
 
// Fields omitted
 
@ ConstructorProperties ({"years", "ultimateAnswer "})
Public ExampleBean (int years, String ultimateAnswer ){
This. years = years;
This. ultimateAnswer = ultimateAnswer;
}
}

2. Setter-based DI (Constructor-based dependency injection)
Setter-based DI: the setter method is used to complete the injection.

[Html]
<Bean id = "exampleBean" class = "examples. ExampleBean">
 
<! -- Setter injection using the nested <ref/> element -->
<Property name = "beanOne"> <ref bean = "anotherExampleBean"/> </property>
 
<! -- Setter injection using the neater 'ref 'attribute -->
<Property name = "beanTwo" ref = "yetAnotherBean"/>
<Property name = "integerProperty" value = "1"/>
</Bean>
 
<Bean id = "anotherExampleBean" class = "examples. AnotherBean"/>
<Bean id = "yetAnotherBean" class = "examples. YetAnotherBean"/>
[Html]
Public class ExampleBean {
 
Private AnotherBean beanOne;
Private YetAnotherBean beanTwo;
Private int I;
 
Public void setBeanOne (AnotherBean beanOne ){
This. beanOne = beanOne;
}
 
Public void setBeanTwo (YetAnotherBean beanTwo ){
This. beanTwo = beanTwo;
}
 
Public void setIntegerProperty (int I ){
This. I = I;
}
}

Constructor-based DI:

[Html]
<Bean id = "exampleBean" class = "examples. ExampleBean">
 
<! -- Constructor injection using the nested <ref/> element -->
<Constructor-arg>
<Ref bean = "anotherExampleBean"/>
</Constructor-arg>
 
<! -- Constructor injection using the neater 'ref 'attribute -->
<Constructor-arg ref = "yetAnotherBean"/>
 
<Constructor-arg type = "int" value = "1"/>
</Bean>
 
<Bean id = "anotherExampleBean" class = "examples. AnotherBean"/>
<Bean id = "yetAnotherBean" class = "examples. YetAnotherBean"/>

[Html]
Public class ExampleBean {
Private AnotherBean beanOne;
Private YetAnotherBean beanTwo;
Private int I;
Public ExampleBean (AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int I ){
This. beanOne = anotherBean;
This. beanTwo = yetAnotherBean;
This. I = I;
}
}

Inject through static factory Method
[Html]
<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"/>
[Html]
Public class ExampleBean {
 
// A private constructor
Private ExampleBean (...){
...
}

// A static factory method; the arguments to this method can be
// Considered the dependencies of the bean that is returned,
// Regardless of how those arguments are actually used.
Public static ExampleBean createInstance (
AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int I ){
 
ExampleBean eb = new ExampleBean (...);
// Some other operations...
Return eb;
}
}

Arguments to the static factory method are supplied via <constructor-arg/> elements, exactly the same as if a constructor had actually been used. the type of the class being returned by the factory method does not have to be of the same type as the class that contains the static factory method, although in this example it is. an instance (non-static) factory method wocould be used in an essential identical fashion (aside from the use of the factory-bean attribute instead of the class attribute ), so details will not be discussed here.

Dependency resolution process
The ApplicationContext is created and initialized with configuration metadata that describes all the beans. Configuration metadata can be specified via XML, Java code or annotations.

For each bean, its dependencies are expressed in the form of properties, constructor arguments, or arguments to the static-factory method if you are using that instead of a normal constructor. these dependencies are provided to the bean, when the bean is actually created.

Each property or constructor argument is an actual definition of the value to set, or a reference to another bean in the container.

Each property or constructor argument which is a value is converted from its specified format to the actual type of that property or constructor argument. by default Spring can convert a value supplied in string format to all built-in types, such as int, long, String, boolean, etc.

 

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.