Dependency Injection,依赖注入。
对象之间依赖关系的管理交给 Spring 维护;
是实现控制反转的方式之一;
可以降低程序之间的耦合(依赖关系)。
Spring 实现的 IoC 容器中,基本类型和对象都可以注入,按配置细节区分:
Java 基本类型
String
bean
Spring 自建 bean(未自己申明的 bean,可以通过 getBean 方法获取到)
非 bean(无法通过 getBean 方法获取到)
数组
集合(List、Set、Map)
使用 setter 方法演示以上数据类型的注入
bean 的类
package constxiong.datatype;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class User {
private double id;
private String name;
private User wife;
private User[] friends;
private List<User> schoolmates;
private Set<String> favorites;
private Map<String, String> emails;
public void setId(double id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setWife(User wife) {
this.wife = wife;
}
public void setFriends(User[] friends) {
this.friends = friends;
}
public void setSchoolmates(List<User> schoolmates) {
this.schoolmates = schoolmates;
}
public void setFavorites(Set<String> favorites) {
this.favorites = favorites;
}
public void setEmails(Map<String, String> emails) {
this.emails = emails;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", wife=" + wife + ", friends=" + Arrays.toString(friends)
+ ", schoolmates=" + schoolmates + ", favorites=" + favorites + ", emails=" + emails + "]";
}
}
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="wife" class="constxiong.datatype.User">
<property name="id" value="2"/>
<property name="name" value="wife"/>
</bean>
<bean id="ConstXiong" class="constxiong.datatype.User">
<property name="id" value="1"/>
<property name="name" value="ConstXiong"/>
<property name="wife" ref="wife"/>
<property name="friends">
<list>
<ref bean="wife"/>
</list>
</property>
<property name="schoolmates">
<list>
<ref bean="wife"/>
</list>
</property>
<property name="favorites">
<set>
<value>写代码</value>
<value>睡觉</value>
</set>
</property>
<property name="emails">
<map>
<entry key="公司" value="123@123.com"></entry>
<entry key="个人" value="123@abc.com"></entry>
</map>
</property>
</bean>
</beans>
测试代码
package constxiong.datatype;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 依赖注入-可注入的数据类型
*/
public class Test {
@SuppressWarnings("resource")
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("META-INF/spring-dependency-injection-datatype.xml");
User constxiong = (User)factory.getBean("ConstXiong");
System.out.println(constxiong);
}
}
结果打印
User [id=1.0, name=ConstXiong, wife=User [id=2.0, name=wife, wife=null, friends=null, schoolmates=null, favorites=null, emails=null], friends=[User [id=2.0, name=wife, wife=null, friends=null, schoolmates=null, favorites=null, emails=null]], schoolmates=[User [id=2.0, name=wife, wife=null, friends=null, schoolmates=null, favorites=null, emails=null]], favorites=[写代码, 睡觉], emails={公司=123@123.com, 个人=123@abc.com}]
上面这个例子演示了基本类型 double、String、bean、数组、List、Set、Map 的注入,不难看懂,不多解释。
那什么是 Spring 自建的 bean 和 非 bean 呢?这两个概念,看下面这个例子。
新增类 SpecialUser,包含了 BeanFactory 和 Environment 两个类型的属性
package constxiong.datatype;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.core.env.Environment;
public class SpecialUser {
private BeanFactory beanFactory;
private Environment environment;
public BeanFactory getBeanFactory() {
return beanFactory;
}
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public Environment getEnvironment() {
return environment;
}
public void setEnvironment(Environment environment) {
this.environment = environment;
}
}
xml 新增一个 bean 配置
<bean id="specialUser" class="constxiong.datatype.SpecialUser" autowire="byType">
</bean>
测试类的 main 方法中新增测试代码
SpecialUser specialUser = (SpecialUser)factory.getBean("specialUser");
//Spring 自建 bean,并未在 spring-dependency-injection-datatype.xml 配置文件中申明,却可以注入与依赖查找
System.out.println(specialUser.getEnvironment());//通过 autowire="byType" 可以注入成功
System.out.println(factory.getBean(Environment.class));
//非 bean,可以注入,但无法通过 getBean 方法依赖查找
System.out.println(specialUser.getBeanFactory());//通过 autowire="byType" 可以注入成功
System.out.println(factory.getBean(BeanFactory.class));//getBean 方法无法获取到 BeanFactory
打印结果
ConstXiong 备案号:苏ICP备16009629号-3