Spring的依赖注入 常用的有两种方式:set方法注入和构造方法注入,除了这两种常用方式外,还有p名称空间注入(2.5版本后)、SpEL属性注入(Spring3.0以后),下面分别介绍这四种注入方式。

本文以基于XML配置来演示

导入Spring的开发的四个基本jar包,如果要记录日志,导入logging包,如果需要用到log4j,导入该包

项目结构图如下:

java怎么导入依赖 java依赖注入的三种方式_User

一、set方法注入

  该种注入方式,前提要求属性必须有set方法,类有无参构造方法。我们还是以User、Address为例,为了演示的方便,我们暂时不使用接口,直接使用类,实际开发中,Dao和Service一般会使用接口。

1.代码

  • Address.java(地址)
package com.zdxh.injection;

public class Address {
	private String country;
	private String city;
	//使用set方法注入,需要的条件:1,属性必须有set方法,2.必须有无参构造方法
	public void setCountry(String country) {
		this.country = country;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public Address() {
		super();
		// TODO Auto-generated constructor stub
	}
	// 重写toString方法
	@Override
	public String toString() {
		return "Address [country=" + country + ", city=" + city + "]";
	}
	
}
  • User.java(一个用户暂定只有一个地址)
package com.zdxh.injection;
public class User {
	private String name;
	private Integer age;
	private Address address;
	public void setName(String name) {
		this.name = name;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + ", address=" + address + "]";
	}

	
}

2.配置文件,假如该配置文件放在com.zdxh.injection包下,名字叫injection.xml

<?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.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="address" class="com.zdxh.injection.Address">
		<!-- 普通属性的set方法注入,要求属性有set方法,类有无参构造方法,使用property元素的 -->
		<!-- name:为类中属性的名字,value:给基本数据类型属性注入的值 -->
		<property name="country" value="中国"/>
		<property name="city" value="北京"/>
	</bean>
	<bean id="user" class="com.zdxh.injection.User">
		<property name="name" value="貂蝉"/>
		<property name="age" value="20"/>
		<!-- 对象类型的属性,使用ref,指向一个bean的引用 -->
		<property name="address" ref="address"/>
	</bean>

</beans>

3.测试代码

package com.zdxh.injection;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InjectionTest {
	@Test
	public void test1() {
		ApplicationContext ac= new ClassPathXmlApplicationContext("com/zdxh/injection/injection.xml");
		User user = (User) ac.getBean("user");
		System.out.println(user);
	}
}

4.测试结果

java怎么导入依赖 java依赖注入的三种方式_User_02

二、构造方法注入:要求类中有带参构造方法

1.在User.java中加入如下构造方法代码:

public User(String name, Integer age, Address address) {
		this.name = name;
		this.age = age;
		this.address = address;
	}

2.在Address.java中加入构造方法如下

public Address(String country, String city) {
		this.country = country;
		this.city = city;
	}

3.配置文件中增加配置如下:

<!-- 使用构造方法注入 ,使用constructor-arg元素-->
	<bean id="address1" class="com.zdxh.injection.Address">
		<!--index对应有参构造方法中参数的顺序号,从0开始  -->
		<!-- 如果属性是基本数据类型(包括字符串),使用value -->
		<constructor-arg index="0" value="美国"/>
		<constructor-arg index="1" value="洛杉矶"/>
	</bean>
	<bean id="user1" class="com.zdxh.injection.User">
		<constructor-arg index="0" value="李冠希"/>
		<constructor-arg index="1" value="35"/>
		<!-- 如果属性是对象类型,使用ref -->
		<constructor-arg index="2" ref="address1"/>
	</bean>

4.测试代码:

@Test
	public void test2() {
		ApplicationContext ac= new ClassPathXmlApplicationContext("com/zdxh/injection/injection.xml");
		User user1 = (User) ac.getBean("user1");
		System.out.println(user1);
	}

5.测试结果

java怎么导入依赖 java依赖注入的三种方式_构造方法_03

三、使用p名称空间注入值

1.首先,在配置文件中引入p名称空间:

java怎么导入依赖 java依赖注入的三种方式_java怎么导入依赖_04

代码如下:

xmlns:p="http://www.springframework.org/schema/p"

2.在配置文件中使用p名称空间注入属性值

java怎么导入依赖 java依赖注入的三种方式_ci_05

在这里,因为Address的属性都是普通属性,所以用p:属性名配置,而User中的address属性是对象类型,使用p:属性名-ref配置

<bean id="address2" class="com.zdxh.injection.Address" p:country="英国" p:city="伦敦"/>
<bean id="user2" class="com.zdxh.injection.User" p:name="刘备" p:age="51" p:address-ref="address2"/>

4.编写测试代码:

@Test
	public void test3() {
		ApplicationContext ac= new ClassPathXmlApplicationContext("com/zdxh/injection/injection.xml");
		User user = (User) ac.getBean("user2");
		System.out.println(user);
	}

5.测试结果

java怎么导入依赖 java依赖注入的三种方式_ci_06

四、使用SpEL属性注入

该方法在Spring3.0以后可以使用。SpEL表达式的语法格式为:#{SpEL表达式}。想了解更多,请阅读Spring帮助文档,我使用的是Spring4.3.6,在帮助文档中的位置如图:

java怎么导入依赖 java依赖注入的三种方式_User_07

1.在配置文件中增加SpEL的配置

<!-- 使用SpEL表达式注入值 ,property中全部使用表达式语法:#{表达式},表达式可以是普通数据类型,也可以是方法的调用,也可以是bean的引用-->
	<bean id="address3" class="com.zdxh.injection.Address" >
		<property name="country" value="#{'日本'}"></property>
		<property name="city" value="#{'东京'}"></property>
	</bean>
	<bean id="user3" class="com.zdxh.injection.User">
		<property name="name" value="#{'貂蝉'}"/>
		<property name="age" value="#{20}"/>
		<!-- 对象类型的属性,表达式是一个bean的id -->
		<property name="address" value="#{address3}"/>
	</bean>

2.编写测试方法:

@Test
	public void test4() {
		ApplicationContext ac= new ClassPathXmlApplicationContext("com/zdxh/injection/injection.xml");
		User user = (User) ac.getBean("user3");
		System.out.println(user);
	}

3.测试结果

java怎么导入依赖 java依赖注入的三种方式_构造方法_08

总结:本文介绍了Spring基于XML配置注入属性的方法,其中前两种是常用的方法,但是随着Spring版本的改善,后面增加的这两种方法也是常用的。