Spring three ways dependency injection (DI) of

Spring dependency injection ( the DI three ways) in Spring three ways dependency injection (DI), namely:

1. Interface injection

2. Setter Method Injection

3. Constructor injection

Here are some of the three dependency injection in Spring is how to achieve.

First, we need the following categories:

Interface Logic.java

Interface implementation class LogicImpl.java

A processing class LoginAction.java

There is also a test class TestMain.java

Logic.java as follows:

package com.spring.test.di;

public interface Logic {

public String getName();

}

LogicImpl.java as follows:

package com.spring.test.di;

public class LogicImpl implements Logic{

public String getName(){

return "fengyun";

}

}

TestMain.java

package com.spring.test.di;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TestMain {

/**

* @param args

*/

public static void main (String [] args) {// get the ApplicationContext

ApplicationContext ctx = new FileSystemXmlApplicationContext(

"applicationContext.xml");

// get Bean

LoginAction loginAction = (LoginAction) ctx.getBean("loginAction");

loginAction.execute();

}

}

LoginAction.java based on the use of different injection methods slightly different

The method of injection according to the following classes of view LoginAction.java

Setter Method Injection:

package com.spring.test.di;

public class LoginAction {

private Logic logic;

public void execute() {

String name = logic.getName();

System.out.print("My Name Is " + name);

}

/**

* @return the logic

*/

public Logic getLogic() {

return logic;

}

/**

* @param logic

* the logic to set

*/

public void setLogic(Logic logic) {

this.logic = logic;

}

Logic} defines a variable of type logic, no logic and instantiated in LoginAction, and only his corresponding

setter / getter method, because we use here is the way Spring's dependency injection

applicationContext.xml configuration file as follows:

<bean id="logic" class="com.spring.test.di.LogicImpl"/>

<bean id="loginAction" class="com.spring.test.di.LoginAction">

<property name="logic" ref="logic"></property>

</bean>

You can now run testMain.java, we can see the console hit My Name Is fengyun

OK, this is the setter method injection spring, very simple

Let us look Constructor injection

As the name suggests, Constructor injection, that we rely on the construction method to achieve the purpose LoginAction DI, as follows:

LoginAction.java

package com.spring.test.di;

public class LoginAction {

private Logic logic;

public LoginAction(Logic logic) {

this.logic = logic;

}

public void execute() {

String name = logic.getName();

System.out.print("My Name Is " + name);

}

}

Here we add a constructor of a LoginAction

applicationContext.xml configuration file as follows:

<bean id="logic" class="com.spring.test.di.LogicImpl"/>

<bean id="loginAction" class="com.spring.test.di.LoginAction">

<constructor­arg index="0" ref="logic"></constructor­arg>

</bean>

We use constructorarg be configured, index property is used in a sequence constructor parameter, if multiple

Parameter, is in order, from 0,1 ... configure

We can now run testMain.java the results with the use of injection method Setter exactly the same thing which should be noted are: constructor has multiple parameters of words, such as: parameter 1, parameter 2, depends on the parameters and parameter 2 to 1, which

Note that in the case where the order will have constructor parameters must be placed before a second parameter.

Let's continue to talk about us not used to the interface injection, or to LogicAction example, we have for him has been modified as follows

It shows:

LogicAction.java

package com.spring.test.di;

public class LoginAction {

private Logic logic;

public void execute() {

try {

Object obj = Class.forName("com.spring.test.di.LogicImpl")

.newInstance();

logic = (Logic) obj;

String name = logic.getName();

System.out.print("My Name Is " + name);

} catch (Exception e) {

e.printStackTrace ();

}

}

}

Profiles:

<bean id="logic" class="com.spring.test.di.LogicImpl"/>

<bean id="loginAction" class="com.spring.test.di.LoginAction">

</bean>

Setter is the most commonly used constructors and two injection methods

Spring for dependency injection, the most important thing is to understand him, once understood, you will find it very simple. Nothing more than to let

Containers Give us an example of those classes, we need to do it is to provide the container interface that will set our methods or

By the constructor. 

Guess you like

Origin www.cnblogs.com/ycq-qiang/p/11161941.html