Spring的依赖注入?

依赖注入: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、实例工厂注入
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值