Spring 利用springIOC和DI实现MVC的模拟例子
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…… @Override public void savePerson(){ this.personDao.savePerson(); } }
4、建接口 PersonDao
public interface PersonDao{ public void savePerson(); }
5、类 PersonDaoImpl 实现 PersonDao
public class PersonDaoImpl implements PersonDao{ @Override public 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{ @Test public void test(){ PersonAction personAction = (PersonAction)context.getBean("personAction"); personAction.savePerson(); } }
结果:输出 save person dao
上一篇:
IDEA上Java项目控制台中文乱码