Spring Ioc-several methods of dependency Injection

Source: Internet
Author: User

One setter method Injection

The configuration file is as follows:

<Bean id = "helloAction" class = "org. yoo. action. SpringSetterHelloAction">
<! -- Setter injection using the nested <ref/> element -->
<Property name = "helloservice"> <ref bean = "helloService"/> </property>
<! -- You can choose not to set the type -->
<Property name = "name" value = "yoo"> </property>
</Bean>

Code in the action Implementation class:

Private IHelloService helloservice;
Private String name;
Public void sayHello (){
Helloservice. sayHello ();
System. out. println (this. name );
}

Public void setHelloservice (IHelloService helloservice ){
This. helloservice = helloservice;
}

Public void setName (String name ){
This. name = name;
}

Here, both name and helloservice use the setter method of the attribute for injection. Set a global attribute in the class and set the setter method for the attribute for container injection.

2. constructor Injection

Spring also supports constructor injection, that is, constructor or constructor injection in some materials.

First look at the configuration file:

<! -- Normal constructor injection -->
<Bean id = "helloaction" class = "org. Yoo. Action. constructorhelloaction">
<! -- Type must be Java. Lang. String because it is matched by type, not by order -->

<Constructor-Arg type = "Java. Lang. String" value = "yoo"/>
<! -- You can also use index to match -->
<! -- <Constructor-Arg Index = "1" value = "42"/> -->
<Constructor-Arg> <ref bean = "helloservice"/> </constructor-Arg>
</Bean>

Code in the Action Implementation class:

Private helloserviceimpl helloservice;
Private string name;

Public springconstructorhelloaction (helloserviceimpl helloservice, string name ){
This. helloservice = helloservice;
This. Name = Name;
}

@ Override
Public void sayhello (){
Helloservice. sayhello ();
System. Out. println (this. Name );
}

Set two attributes and inject them in the constructor.

Three static factory Injection

The configuration file is as follows:

<! -- Static factory construction injection -->

<! -- Note that the class here includes the factory-method parameter -->

<! -- The factory-method value is the factory method name createinstance in factoryhelloaction -->
<Bean id = "helloaction" class = "org. Yoo. Di. factoryhelloaction" factory-method = "createinstance">
<Constructor-Arg type = "Java. Lang. String" value = "yoo"/>
<Constructor-Arg> <ref bean = "helloservice"/> </constructor-Arg>
</Bean>

Action Implementation class:

Private helloserviceimpl helloservice;
Private string name = NULL;

Private springfactoryhelloaction (string name, helloserviceimpl helloservice ){
This. helloservice = helloservice;
This. Name = Name;
}

Public static springfactoryhelloaction createinstance (string name, helloserviceimpl helloservice ){
Springfactoryhelloaction FHA = new springfactoryhelloaction (name, helloservice );
// Some other operations
Return FHA;
}

@ Override
Public void sayhello (){
Helloservice. sayhello ();
System. Out. println (this. Name );
}
Iv. No configuration file injection (automatic injection)

The configuration file must be written for all the above three methods. In spring2.5, the IOC implementation without writing the configuration file is also provided. It should be noted that no configuration file refers to the dependencies between beans, not based on the configuration file, rather than the absence of the spring configuration file.

The configuration file is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd>
<Bean class = "org. springframework. beans. factory. annotation. AutowiredAnnotationBeanPostProcessor"/>
<Bean id = "helloService" class = "org. yoo. service. HelloServiceImpl">
</Bean>
<Bean id = "helloAction" class = "org. yoo. action. AutowiredHelloAction">
</Bean>
</Beans>

As shown in the preceding figure, only two beans, helloService and helloAction, are set, and the helloAction dependency on helloService is not set.

In addition, <bean class = "org. springframework. beans. factory. annotation. AutowiredAnnotationBeanPostProcessor"/> is required to support automatic injection.

Action Implementation class:

/*
* Automatic attribute Loading
*/
/*
@ Autowired
Private HelloService helloservice;

@ Override
Public void sayHello (){
Helloservice. sayHello ();
.
The above code is very simple. Add @ Autowired to the attribute, and spring will automatically inject HelloService.

It also supports constructor and setteer injection, as shown below:

Automatic constructor injection:

/*
* You can also use constructor settings.
*/
@ Autowired
Public SpringAutowiredHelloAction (HelloServiceImpl helloservice ){
This. helloservice = helloservice;
}

Setter method automatic injection:

/*
* You can also set it using the set method.
*/
/*
@ Autowired
Public void setHelloservice (HelloService helloservice ){
This. helloservice = helloservice;
}

Finally, it is mentioned in the spring reference document that If Automatic injection is not used, use the setter method whenever possible. Generally, the setter method is used.

Automatic injection or configuration file. If jdk is lower than 1.5 or spring is not 2.5, you can only use the configuration file. The other items are determined by the actual project selection.

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.