DI 概述
DI:Dependency Injection
,依赖注入,在Spring框架负责创建Bean对象的时候,动态的将依赖对象注入到Bean组件中
入门程序
在IOC入门程序中,如果UserServiceImpl
的实现类中有一个属性,那么使用Spring框架的IOC功能时,可以通过依赖注把该属性的值传递进来。
创建接口
public interface UserService {
public void sayHello();
}
编写接口的实现类
public class UserServiceImpl implements UserService {
private String name;
public void setName(String name) {
this.name = name;
}
public void sayHello(){
System.out.println("Demo01: Hello Spring "+name);
}
}
配置文件
在applicationContest.xml
中使用bean
标签
<bean id="userService" class="wangfei910.Demo01.UserServiceImpl">
<!-- 容器在创建 userService 对象的时候,同时给成员属性赋值 -->
<property name="name" value="Jack"/>
</bean>
创建测试类
/**
* @Auther wangfei
*依赖注入
*/
@org.junit.Test
public void run4(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService=(UserService)applicationContext.getBean("userService");
userService.sayHello();
}
类成员变量注入
Setter方法注入
-
成员属性注入
//实现类 public class UserServiceImpl implements UserService { private String name; public void setName(String name) { this.name = name; } }
<!-- xml配置 --> <bean id="userService" class="wangfei910.Demo01.UserServiceImpl"> <property name="name" value="Jack"/> </bean>
-
引用变量注入
//引用变量实现类 public class CustomerDaoImpl { public void save(){ System.out.println("我是持久层 DAO ..."); } } //实现类 public class CustomerServiceImpl { //提供成员属性 private CustomerDaoImpl custDao; public void setCustDao(CustomerDaoImpl custDao) { this.custDao = custDao; } public void save(){ System.out.println("我是业务层 Service..."); custDao.save(); } }
<!-- xml配置 --> <bean id="customerDao" class="wangfei910.Demo02.CustomerDaoImpl"/> <!-- customerDao 注入到 customerService --> <bean id="customerService" class="wangfei910.Demo02.CustomerServiceImpl"> <!-- name: 与private 成员属性相同 values :字符串 ref :引用对象 --> <property name="custDao" ref="customerDao"/> </bean>
构造方法注入
-
成员属性
//实现类 public class Car1 { private String carName; private double carPrice; private String carUserName; public Car1(String carName,double carPrice){ super(); this.carName=carName; this.carPrice=carPrice; } //第三种注入方法(Car1拥有两个重载的构造函数) public Car1(String carName,String carUserName){ super(); this.carName=carName; this.carUserName=carUserName; } }
<!-- xml配置 --> <bean id="car1" class="wangfei910.Demo03.Car1"> <!-- 第一种注入方式 --> <constructor-arg name="carName" value="丰田汽车"/> <constructor-arg name="carPrice" value="250000.01"/> <!-- 第二种注入方式 [index中,0代表构造方法中第一个成员变量] --> <constructor-arg index="0" value="奥迪汽车"/> <constructor-arg index="1" value="500000"/> <!-- 第三种:联合使用类型和索引匹配入参 --> <constructor-arg index="0" type="java.lang.String"> <value>宝马汽车</value> </constructor-arg> <constructor-arg index="1" type="java.lang.String"> <value>张三</value> </constructor-arg> </bean>
-
引用变量注入
//实现类 public class Person { private String PersonName; private Car1 car1; public Person(String personName, Car1 car1) { PersonName = personName; this.car1 = car1; } }
<!-- xml配置 --> <bean id="person" class="wangfei910.Demo03.Person"> <constructor-arg name="personName" value="gong"/> <constructor-arg name="car1" ref="car1"/> </bean>
集合注入和配置文件注入
public class User {
private String[] arrs;
public void setArrs(String[] arrs) {
this.arrs = arrs;
}
private List<String> list;
public void setList(List<String> list) {
this.list = list;
}
private Map<String,String> map;
public void setMap(Map<String, String> map) {
this.map = map;
}
//配置文件
private Properties pro;
public void setPro(Properties pro) {
this.pro = pro;
}
}
<!-- 注入集合 -->
<bean id="user" class="wangfei910.Demo03.User">
<!-- 注入数组 -->
<property name="arrs">
<list>
<value>哈哈</value>
<value>呵呵</value>
<value>嘿嘿</value>
</list>
</property>
<!-- 注入List -->
<!--
List 存放引用对象
<property name="list" >
<list>
<ref bean=""></ref>
</list>
</property>
-->
<property name="list" >
<list>
<value>小王</value>
<value>张三</value>
<value>李四</value>
</list>
</property>
<!-- Map 注入 -->
<property name="map">
<map>
<entry key="AAA" value="刘邦"/>
<entry key="BBB" value="关羽"/>
</map>
</property>
<!-- Properties 属性文件注入 -->
<property name="pro">
<props>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
下一篇:Spring学习笔记[3]之IOC基于注解的方式 https://blog.csdn.net/Rabbit_Judy/article/details/81842514