在我们使用Spring DI之前,你需要先了解Spring IoC并且会简单的使用
传送门:Spring ioC的简单理解与使用_weixin_45506581的博客-CSDN博客
会简单使用Spring IoC之后,就可以开始使用Spring DI啦!
Spring DI:其中,DI 全称 Dependency InjectionDependency Injection(依赖注入),它是spring框架核心ioc的具体实现
作用:我们虽然通过控制反转,将对象的创建权交给了spring,但是代码中是不可能出现没有依赖的情况的,ioc只是降低了它们的依赖关系,但不会消除
例:当我们通过IoC创建一个对象后,如果这个对象里面还有一个 user对象 属性,那么我们就拿不到这个user对象的实例化,即 user == null
Spring DI注入有两种方式: 构造方法注入与set方法注入
上代码:
目录结构:
user对象:
package com.wanshi.pojo;
public class user {
private String id;
private String username;
private String userpassword;
private String realname;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpassword() {
return userpassword;
}
public void setUserpassword(String userpassword) {
this.userpassword = userpassword;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
}
userService对象:
package com.wanshi.service;
import com.wanshi.pojo.user;
public class userService {
private user user;// 注:当对象为private的时候,需要写set方法来注入依赖
// public void setUser(com.wanshi.pojo.user user) {
// this.user=user;
// }
private String name;
// public void setName(String name) {
// this.name=name;
// }
userService(user user,String name){
this.user=user;
this.name=name;
}
@Override
public String toString() {
return "userService{" +
"user=" + user +
", name='" + name + '\'' +
'}';
}
}
application.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"
default-lazy-init="true"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- user的bean对象-->
<bean id="user" class="com.wanshi.pojo.user"/>
<!-- 构造方法为userService注入依赖-->
<bean id="userService" class="com.wanshi.service.userService">
<constructor-arg name="user" ref="user"/>
<constructor-arg name="name" value="张三"/>
</bean>
<!-- set方法为userService注入依赖-->
<!-- <bean id="userService" class="com.wanshi.service.userService">-->
<!-- <property name="name" value="张三"/>-->
<!-- <property name="user" ref="user"/>-->
<!-- </bean>-->
</beans>
单元测试 Test类 代码:
package com.wanshi.test;
import com.wanshi.service.userService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Test {
@org.junit.Test
public void Test() throws IOException {
// 初始化工厂对象,用于获取Spring容器中的bean对象
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
// 打印从Spring容器中拿到的userService对象
userService userservice= (userService) applicationContext.getBean("userService");
System.out.println(userservice);
}
}
执行效果:
构造方法注入与set方法注入只是在application.xml中,bean标签里的注入子标签不同而已:
<constructor-arg name="user" ref="user"/> //构造方法注入user对象,使用ref属性,引用的是 <bean id="user" class="com.wanshi.pojo.user"/>
<constructor-arg name="name" value="张三"/> ///构造方法注入常用数据类型,使用value属性
<property name="user" ref="user"/> //set方法注入user对象,前提是要有此属性的set方法,其余地方感觉与 constructor-arg标签相同
<property name="name" value="张三"/> //set方法注入常用数据类型