Spring依赖注入

时间:2021-03-18 14:19:15   收藏:0   阅读:0

通过构造函数注入

<bean id="accountDao" class="com.ttpfx.dao.impl.AccountDaoImpl"/>
<bean id="accountService" class="com.ttpfx.service.impl.AccountServiceImpl">
    <constructor-arg name="accountDao" ref="accountDao"/>
</bean>

通过set方法注入

<bean id="accountDao" class="com.ttpfx.dao.impl.AccountDaoImpl"/>
<bean id="accountService" class="com.ttpfx.service.impl.AccountServiceImpl">
   <property name="accountDao" ref="accountDao"/>
</bean>

注入基本类型、String、Bean

public class Account {
    private Integer id;
    private String username;
    private Date birthday;
}
<bean id="now" class="java.util.Date"/>
<bean id="account" class="com.ttpfx.domain.Account">
    <property name="id" value="1"/>
    <property name="username" value="张三"/>
    <property name="birthday" ref="now"/>
</bean>  
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!