使用spring实现一个MVC

来源:互联网 发布:fpga和单片机哪个好 编辑:程序博客网 时间:2024/05/17 21:56

使用spring实现一个MVC

  1. MVC设计分为Dao层、Service层、视图层(Action)。

Dao层接口

public interface PersonDao {    void savePerson();}

Dao层实现类

public class PersonDaoImpl implements PersonDao{@Overridepublic void savePerson() {    System.out.println("保存用户");}}

Service层接口

public interface PersonService {void savePerson();}

Service层实现类(注入Dao接口,实现Service接口)

public class PersonServiceImpl implements PersonService{private PersonDao personDao;public PersonDao getPersonDao() {    return personDao;}public void setPersonDao(PersonDao personDao) {    this.personDao = personDao;}@Overridepublic void savePerson() {    this.personDao.savePerson();}}

Action测试类

public class PersonActionTest extends SpringHelper{static{    path="org/springframework/jmx/mvcTest.xml";}@Testpublic void test(){    PersonAction pa = (PersonAction) this.fileResource.getBean("personAction");    System.out.println(pa.getPersonService());    pa.savePersonAction();}}

Xml文件

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"><beans><!-- 注入PersonAction的Bean  --><bean id="personAction" class="com.lzl.test.mvc.PersonAction">    <!-- 注入service接口 -->     <property name="personService">        <ref bean="personService"/>    </property></bean><bean id="personService" class="com.lzl.test.mvc.PersonServiceImpl">    <!--    注入到接口      -->      <property name="personDao">        <ref bean="personDao"/>      </property></bean><bean id="personDao" class="com.lzl.test.mvc.PersonDaoImpl"></bean></beans>
0 0
原创粉丝点击