1.set方法注入
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
<bean id="userDao" class="com.maweiqi.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.maweiqi.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
特殊:p命名空间注入
xmlns:p="http://www.springframework.org/schema/p"
<bean id="userService" class="com.maweiqi.service.impl.UserServiceImpl" p:userDao-ref="userDao"/>
2.有参构造注入
private UserDao userDao;
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
<bean id="userDao" class="com.maweiqi.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.maweiqi.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>