1.依赖注入的作用
将spring的核心容器中的对象赋值给类的成员变量。
2.依赖注入实现的方法
2.1构造方法方式注入
要定义变量 ,提供构造方法
java代码如下:
public class AccountServiceImpl implements AccountService {
private String name;
public AccountServiceImpl(String name) {
this.name = name;
}
@Override
public void save() {
System.out.println("AccountServiceImpl已保存"+name);
}
}
配置文件:使用构造函数的方式进行依赖注入,那么我们使用constructor-arg标签
<!--注册AccountService-->
<bean id="accountService" class="com.service.impl.AccountServiceImpl">
<constructor-arg name="name" value="胡歌"></constructor-arg>
</bean>
2.2 set方法注入
要定义变量 ,提供Set方法
配置文件:property标签中的name表示要赋值的属性。
property标签中的value或者ref表示要赋给那个属性的内容
(1)如果属性是简单类型,则使用value
(2)如果属性是bean类型,则使用ref(非常重要)
2.2.1 简单类型String类型注入
<bean id="accountService" class="com.service.impl.AccountServiceImpl">
<property name="name" value="李四"></property>
</bean>
2.2.2 注入数组类型
java代码:
public class AccountServiceImpl implements AccountService {
private String[] hobbys;
public void setHobbys(String[] hobbys) {
this.hobbys = hobbys;
}
@Override
public void save() {
System.out.println(Arrays.toString(hobbys));
}
}
配置文件:
<bean id="accountService" class="com.service.impl.AccountServiceImpl">
<property name="hobbys">
<array>
<value>篮球</value>
<value>足球</value>
<value>乒乓球</value>
<value>排球</value>
</array>
</property>
</bean>
2.2.3 注入Map类型
public class AccountServiceImpl implements AccountService {
private Map<String,String> map;
public void setMap(Map<String, String> map) {
this.map = map;
}
@Override
public void save() {
Set<Map.Entry<String, String>> set = map.entrySet();
for (Map.Entry<String, String> entry : set) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
System.out.println("AccountServiceImpl... save()");
}
}
配置文件:
<bean id="accountService" class="com.service.impl.AccountServiceImpl">
<property name="map">
<props>
<prop key="akey">abc</prop>
<prop key="bkey">efg</prop>
<prop key="ckey">hij</prop>
</props>
</property>
</bean>
2.3.4 注入java对象类型
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void save() {
System.out.println("AccountServiceImpl已实现");
accountDao.save();
}
}
配置文件:
<!--注册AccountService-->
<bean id="accountService" class="com.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--注册accountDao-->
<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
</bean>
2.3 P名称空间注入
首先引入约束:xmlns:p="http://www.springframework.org/schema/p"
约束文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
配置文件:使用p:要赋值的对象
<bean id="accountService" class="com.service.impl.AccountServiceImpl" p:name="胡歌">
</bean>
2.4SpEL的属性注入
这个spEL 方式赋值,最大的好处在于,它能像EL表达式一般,在里面进行运算、 逻辑判断,还可以调用其它Bean的属性和方法给当前属性赋值。
- 语法格式 : #{spEL}
- 类中存在一个属性 name , 并且给定 set方法。
配置文件:
<property name="name" value="#{'张三'}" ></property>
主要熟练掌握前两种方法即可。