public interface IPersonService {
public abstract void Save();
}
public class PersonDaoBean implements IPersonDao {
public void add(){
System.out.println("这是personDaoBean的Add()方法");
}
}
public class PersonServiceBean implements IPersonService {
private IPersonDao iPersonDao;
public IPersonDao getIPersonDao() {
return iPersonDao;
}
public void setIPersonDao(IPersonDao personDao) {
iPersonDao = personDao;
}
public void Save(){
iPersonDao.add();
}
}
<bean id="personDaoBean" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
<bean id="personService"
class="cn.itcast.service.impl.PersonServiceBean">
<property name="IPersonDao" ref="personDaoBean"></property>
</bean>
public void instanceSpring() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"beans.xml");
IPersonService ipersonService = (IPersonService) ctx
.getBean("personService");
ipersonService.Save();
ctx.close();
// ctx.registerShutdownHook();
}
输出:这是personDaoBean的Add()方法