使用构造器装配属性 PersonServiceBean.java package service.impl; import java.util.Set; import dao.PersonDao; import service.PersonService; public class PersonServiceBean implements PersonService { private PersonDao personDao; private String name; public PersonServiceBean(PersonDao personDao, String name) { this.personDao = personDao; this.name = name; } @Override public String getName() { // TODO Auto-generated method stub return this.name; } @Override public PersonDao getPersonDao() { // TODO Auto-generated method stub return this.personDao; } @Override public void save() { // TODO Auto-generated method stub System.out.println(this.getClass().getName()+" save方法"); } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public void setName(String name) { this.name = name; } } beans.xml <bean id="personService" class="service.impl.PersonServiceBean"> <constructor-arg index="0" ref="personDao"></constructor-arg> <constructor-arg index="1" value="pf"></constructor-arg> </bean> index指明了参数的位置,加了index就不用加type来说明了。