Spring 利用springIOC和DI实现MVC的模拟例子

来源:互联网 发布:httpclient post 数据 编辑:程序博客网 时间:2024/04/29 12:18

1、建类 PersonAction.java

public  class  PersonAction{private  PersonService  personService;封装……public  void  savePerson(){this.personService.savePerson();}}

2、建接口 PersonService

public  interface  PersonService{public  void  savePerson();}

3、建类 PersonServiceImpl  实现 PersonService

public  class PersonServiceImpl  implements  PersonService {private  PersonDao  personDao;封装personDao……@Overridepublic  void  savePerson(){this.personDao.savePerson();}}

4、建接口 PersonDao

public interface PersonDao{public  void  savePerson();}

5、类 PersonDaoImpl  实现 PersonDao

public  class  PersonDaoImpl  implements  PersonDao{@Overridepublic  void  savePerson(){s.o.p("save  person  dao");}}

6、配置文件 applicationContext-mvc.xml

<bean  id="personDao"  class="cn.google.spring.PersonDaoImpl"></bean><bean  id="personService"  class="cn.google.spring.PersonServiceImpl"><property  name="personDao"><ref  bean="personDao"></property></bean><bean  id="personAction"  class="cn.google.spring.PersonAction"><property  name="personService"><ref  bean="personService"></property></bean>

7、建测试类 MVCTest

public  class  MVCTest  extends  SpringInit{@Testpublic  void  test(){PersonAction  personAction  = (PersonAction)context.getBean("personAction");personAction.savePerson();}}

        结果:输出 save person dao


0 0
原创粉丝点击