Three methods of Spring dependency injection: instance explanation and spring

Source: Internet
Author: User

Three methods of Spring dependency injection: instance explanation and spring

Three methods of Spring dependency injection (DI) are as follows:

1. Interface Injection

2. Setter method Injection

3. constructor Injection

The following describes how the three dependency injection methods are implemented in Spring.

First, we need the following classes:

Interface Logic. java

Interface implementation class LogicImpl. java

One processing class LoginAction. java

There is also a test class TestMain. java

Logic. java is as follows:

package com.spring.test.di;public interface Logic {public String getName();}

LogicImpl. java is 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) {// obtain the ApplicationContext object ApplicationContext ctx = new FileSystemXmlApplicationContext ("applicationContext. xml "); // obtain Bean LoginAction loginAction = (LoginAction) ctx. getBean ("loginAction"); loginAction.exe cute ();}}

LoginAction. java is slightly different based on different injection methods.

The following shows the LoginAction. java class based on the injection method.

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

A Logic variable logic is defined. In LoginAction, logic is not instantiated, but only the corresponding setter/getter method is used, because Spring dependency injection is used here.

The applicationContext. xml configuration file is as follows:

Now we can run testMain. java. We can see that My Name Is fengyun in the console

OK. This is spring's setter method injection, which is very simple.

Let's take a look at constructor injection.

As the name implies, constructor injection relies on the constructor of LoginAction to achieve DI, as shown below:

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 have added a LoginAction constructor.

The applicationContext. xml configuration file is as follows:

We use constructor-arg for configuration. The index attribute is used to represent the order of parameters in the constructor. If there are multiple parameters, they are sorted from 0, 1... To configure

Now we can run testMain. java, and the result is exactly the same as the Setter method.

Note that the constructor has multiple parameters, such as parameter 1 and parameter 2, and parameter 2 depends on Parameter 1, in this case, pay attention to the order of the constructor. Parameter 1 must be placed before parameter 2.

Next we will continue to talk about the interface injection that we don't often use, or take LogicAction as an example. We have modified it, as shown below:

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();  }}}

Configuration file:

For the method of interface injection, the above is my personal understanding and I do not know if there is any problem. Haha

We usually use the Setter and constructor injection methods. As for their respective comparisons, I want to have a clear answer as long as baidu is used.

For Spring dependency injection, the most important thing is to understand it. Once understood, it will be very simple. Instead, let the container instantiate those classes for us. All we need to do is provide this interface to the container. This interface is our set method or constructor.

If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!

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.