必须 per set super() 注入 开始 属性 cati
--- 恢复内容开始 ---
一、通过 bean 的 property 属性,必须要有 set 方法
二、通过构造器设置参数
一)PersonServiceImpl 中写构造器
- public class PersonServiceImpl implements PersonService {
- PersonDao personDao;
- private String name;
- @Override
- public void save() {
- // TODO Auto-generated method stub
- System.out.println("PersonServiceImpl··"+name);
- personDao.add();
- }
- public PersonServiceImpl(PersonDao personDao, String name) {
- super();
- this.personDao = personDao;
- this.name = name;
- }
- public PersonServiceImpl() {
- super();
- }
- }
二)applicationContext.xml 中配置构造器
- <bean id="personService" class="com.lovo.u34.service.impl.PersonServiceImpl">
- <constructor-arg index="0" type="com.lovo.u34.dao.PersonDao" ref="personDao"></constructor-arg>
- <constructor-arg index="1" type="String" value="张三"></constructor-arg>
- </bean>
- <bean id="personDao" class="com.lovo.u34.dao.impl.PersonDaoImpl">
- </bean>
--- 恢复内容结束 ---
(三)依赖注入的三种方式
来源: http://www.bubuko.com/infodetail-2156993.html