依赖注入:Dependency Injection(简称DI注入)。它是spring框架核心 ioc容器,bean属性值赋值的具体方案(通俗的说:成员变量赋值 建立bean与bean之间的关系)
DI注入的方式有4种:
1、 set方法注入
private String name;
public void setName(String name) {
this.name = name;
}
<bean id="acountService" class="com.spring.service.Impl.AcountServiceImpl">
<property name="name"value="李白"></property>
</bean>
2、构造方法注入
//自定义对象
private AccountDao accountDao;
public AcountServiceImpl(AccountDao accountDao) {
this.accountDao = accountDao;
}
<bean id="accountDao" class="comspring.dao.Impl.AccountDaoImpl"></bean>
<bean id="acountService" class="com.spring.service.Impl.AcountServiceImpl">
<constructor-arg name="accountDao" ref="accountDao"></constructor-arg>
</bean>
//String类型
private String name;
public AcountServiceImpl(String name) {
this.name = name;
}
<bean id="acountService" class="com.spring.service.Impl.AcountServiceImpl">
<constructor-arg name="name" value="张无忌"></constructor-arg>
</bean>
```/
3、静态工厂注入
4、实例工厂注入