Home > Java > javaTutorial > body text

Spring Ioc-detailed introduction to several methods of dependency injection

黄舟
Release: 2017-03-03 10:15:22
Original
1326 people have browsed it

A 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>
<!--可以不设置类型 -->
<property name="name" value="yoo"></property>
</bean>
Copy after login

The 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;}
Copy after login

here Both name and helloservice are injected using the property's setter method. That is, set a global property in the class and have a setter method for the property for container injection.

2 Constructor injection

spring also supports constructor injection, that is, constructor or constructor injection in some data.

Look at the configuration file first:

<!--普通构造器注入-->
<bean id="helloAction" class="org.yoo.action.ConstructorHelloAction">
<!--type 必须为Java.lang.String 因为是按类型匹配的,不是按顺序匹配-->
<constructor-arg type="java.lang.String" value="yoo"/>
<!-- 也可以使用index来匹配-->
<!--<constructor-arg index="1" value="42"/>-->
<constructor-arg><ref bean="helloService"/>
</constructor-arg>
</bean>
Copy after login

The code in the action implementation class:

private HelloServiceImpl helloservice;
private String name ;
public SpringConstructorHelloAction(HelloServiceImpl helloservice,String name){this.helloservice = helloservice;
this.name = name ;}
@Overridepublic void sayHello() {helloservice.sayHello();
System.out.println(this.name);}
Copy after login

Set 2 attributes as well, and then inject them in the constructor.

Three static factory injection

The configuration file is as follows:

<!--静态工厂构造注入-->
<!--注意这里的class 带factory-method参数-->
<!--factory-method的值是FactoryHelloAction中的工厂方法名createInstance-->
<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>
Copy after login

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 operationsreturn fha;}
@Overridepublic void sayHello() 
{helloservice.sayHello();
System.out.println(this.name);}
Copy after login

Four no configuration File injection (automatic injection)

The above three methods require writing configuration files. In spring 2.5, an ioc implementation without writing configuration files is also provided. It should be noted that no configuration file refers to dependencies between beans and is not based on configuration files, but does not mean that there is no 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.php.cn/">
<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>
Copy after login

It can be seen that the above only sets the two beans helloService and helloAction, and does not set the dependence of helloAction on helloService.

In addition, is required to support automatic injection.

Action implementation class:

/*
* 属性自动装载
*/
/*
@Autowired
private HelloService helloservice;
@Override
public void sayHello() {
helloservice.sayHello();
。
Copy after login

The above code is very simple. Add @Autowired annotation to the attribute, and spring will automatically inject HelloService.

Also supports the injection of constructors and setter methods, as follows:

Automatic injection of constructors:

/*
* 还可以使用构造函数设置
*/
@Autowired
public SpringAutowiredHelloAction(HelloServiceImpl helloservice){
this.helloservice = helloservice;
}
Copy after login

Automatic injection of setter methods:

/*
* 也可以使用set方法设置
*/
/*
@Autowired
public void setHelloservice(HelloService helloservice) {
this.helloservice = helloservice;
}
Copy after login

Finally, it is mentioned in the spring reference document that if you do not use automatic injection, try to use the setter method. Generally, the setter method is also used.

Whether to use automatic injection or the configuration file method, if jdk is lower than 1.5 or spring is not 2.5, you can only use the configuration file method. The rest depends on the actual project selection.

The above is the detailed introduction of several methods of Spring Ioc-dependency injection. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template