Several implementation methods of Ioc in Spring

Source: Internet
Author: User

<! -- 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>

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

This. helloservice = helloservice;
}

This. name = name;
}

2. 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 -->

<! -- You can also use index to match -->
<! -- <Constructor-arg index = "1" value = "42"/> -->
<Constructor-arg> <ref bean = "helloService"/> </constructor-arg>
</Bean>

Private String name;

Public SpringConstructorHelloAction (HelloServiceImpl helloservice, String name ){
This. helloservice = helloservice;
This. name = name;
}

Public void sayHello (){
Helloservice. sayHello ();
System. out. println (this. name );
}

Three static factory Injection

<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>

 

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)

<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>

* 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.

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

* 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.


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.