【Spring】 - 基于xml的两种依赖注入方式(构造函数注入,setter注入)

概述

Spring中依赖注入 dependency injection  (DI)一般来说有两种形式: 1)基于xml的依赖注入, 2)基于注解的依赖注入。
基于xml的依赖注入方式通常又可以分为:1)构造函数方式注入。2)setter方式注入。

环境

Spring4.3.9, JDK1.8  pom添加spring依赖如下:

        org.springframework
        spring-context
        4.3.9.RELEASE
一个包就够了。

测试项目结构:
【Spring】 - 基于xml的两种依赖注入方式(构造函数注入,setter注入)_第1张图片
目的:将HelloService的实例HelloServiceImpl注入到HelloPrinter中并在Application中通过容器取出来使用。

代码如下:
HelloService接口
public interface HelloService {
	String sayHello();
}
HelloService实现
public class HelloServiceImpl implements HelloService {

	public String sayHello() {
		return "hello";
	}
	
}

HelloPrinter类(未完成版本,在这里使用不同的方法依赖注入)
public class HelloPrinter {

	private HelloService helloService;
	
	public void print() {
		System.out.println(helloService.sayHello());
	}
	
}
Application类
public class Application {
	
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
		HelloPrinter printer = (HelloPrinter) context.getBean("helloPrinter");
		printer.print();
	}
}

依赖注入

构造函数方式

单个参数

HelloPrinter类:
public class HelloPrinter {

	private HelloService helloService;
	
	public HelloPrinter(HelloService helloService) {
		this.helloService = helloService;
	}
	
	public void print() {
		System.out.println(helloService.sayHello());
	}
	
}
添加了构造函数,同时通过构造函数初始化helloService

spring.xml配置(以下helloService的配置省略):



    
    
    
        
    

首先使用的方式将helloService的实例加入容器。
再使用上面的注入
运行输出:
hello


多个参数

1. 按照顺序注入
HelloPrinter类:
public class HelloPrinter {

	private HelloService helloService;
	
	public HelloPrinter(HelloService helloService, String s1, String s2, int id) {
		this.helloService = helloService;
		System.out.println(s1 + s2 + id);
	}
	
	public void print() {
		System.out.println(helloService.sayHello());
	}
	
}

xml配置:

    

    
    
    
    
运行输出:
aaabbb123
hello
2. 按照类型注入
HelloPrinter
public class HelloPrinter {

	private HelloService helloService;
	
	public HelloPrinter(HelloService helloService, String s, int id) {
		this.helloService = helloService;
		System.out.println(s + id);
	}
	
	public void print() {
		System.out.println(helloService.sayHello());
	}
	
}

xml配置



    
    
    
即使顺序不同也可以正确注入。
如果有多个相同的类型,则按照实际注入的顺序和构造器中参数类型的顺序决定。(相同的类型先到先得)

运行输出:
aaa123
hello

3. 按照name注入

HelloPrinter
public class HelloPrinter {

	private HelloService helloService;
	
	public HelloPrinter(HelloService helloService, String s, int id) {
		this.helloService = helloService;
		System.out.println(s + id);
	}
	
	public void print() {
		System.out.println(helloService.sayHello());
	}
	
}

xml

    
    
    
指定构造器中参数的name
输出:
aaa123
hello

4. 按照index注入
HelloPrinter
public class HelloPrinter {

	private HelloService helloService;
	
	public HelloPrinter(HelloService helloService, String s, int id) {
		this.helloService = helloService;
		System.out.println(s + id);
	}
	
	public void print() {
		System.out.println(helloService.sayHello());
	}
	
}
xml配置:

    
    
    
index从0开始,分别对应构造器中的参数顺序。


setter注入

HelloPrinter
public class HelloPrinter {

	private HelloService helloService;
	
	private String s;
	
	private int id;
	
	public void setHelloService(HelloService helloService) {
		this.helloService = helloService;
	}

	public void setS(String s) {
		this.s = s;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void print() {
		System.out.println(helloService.sayHello() + s + id);
	}
	
}

xml配置:


    
    
    
运行输出:
hellohhhh123123


注意:使用构造器注入方式不能出现循环的依赖注入

例:



    



    
spring会报错

Error creating bean with name 'helloService': Requested bean is currently in creation: Is there an unresolvable circular reference?



你可能感兴趣的:(Spring)