推荐先阅读
:Spring全家桶
去除代码中的new,为属性提供setter方法
public class BookServiceImpl implements BookService { private BookDao bookDao; public void setBookDao(BookDao bookDao) { this.bookDao = bookDao; } }
|
修改配置完成注入
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bookDao" class="com.zx.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.zx.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao"/> </bean> </beans>
|
对应关系:name的值对应类成员变量,ref指向需要注入的bean