Spring 一二事 - IOC MVC 简易搭建

Posted 风间影月の技术小站

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 一二事 - IOC MVC 简易搭建相关的知识,希望对你有一定的参考价值。

1     <bean id="personAction" class="com.lee.spring008.IOC.DI.MVC.PersonAction">
2         <property name="stuService" ref="stuService"></property>
3     </bean>
4     <bean id="stuService" class="com.lee.spring008.IOC.DI.MVC.StuServiceImpl">
5         <property name="stuDAO" ref="stuDAO"></property>
6     </bean>
7     <bean id="stuDAO" class="com.lee.spring008.IOC.DI.MVC.StuDAOImpl"></bean>

IStuDAO.java

1 package com.lee.spring008.IOC.DI.MVC;
2 
3 public interface IStuDAO {
4     public void saveStu();
5 }

IStuService.java

1 package com.lee.spring008.IOC.DI.MVC;
2 
3 public interface IStuService {
4     public void saveStu();
5 }

PersonAction.java

 1 package com.lee.spring008.IOC.DI.MVC;
 2 
 3 public class PersonAction {
 4 
 5     public IStuService stuService;
 6 
 7     public void displaySave() {
 8         stuService.saveStu();
 9     }
10 
11     public IStuService getStuService() {
12         return stuService;
13     }
14 
15     public void setStuService(IStuService stuService) {
16         this.stuService = stuService;
17     }
18 
19 }

StuDAOImpl.java

 1 package com.lee.spring008.IOC.DI.MVC;
 2 
 3 public class StuDAOImpl implements IStuDAO {
 4 
 5     @Override
 6     public void saveStu() {
 7         System.out.println("dao: saving stu...");
 8     }
 9 
10 }

StuServiceImpl.java

 1 package com.lee.spring008.IOC.DI.MVC;
 2 
 3 public class StuServiceImpl implements IStuService {
 4 
 5     public IStuDAO stuDAO;
 6     
 7     @Override
 8     public void saveStu() {
 9         System.out.println("service: saving stu...");
10         stuDAO.saveStu();
11     }
12 
13     public IStuDAO getStuDAO() {
14         return stuDAO;
15     }
16 
17     public void setStuDAO(IStuDAO stuDAO) {
18         this.stuDAO = stuDAO;
19     }
20 
21 }

测试:

1 @Test
2     public void testPersonAction() {
3         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
4         PersonAction person = (PersonAction)context.getBean("personAction");
5         person.displaySave();
6     }

 

github地址:https://github.com/leechenxiang/maven-spring001-helloworld

以上是关于Spring 一二事 - IOC MVC 简易搭建的主要内容,如果未能解决你的问题,请参考以下文章

Spring 一二事 - 单例

Spring 一二事 - 别名

Spring 一二事 - 依赖注入

Spring 一二事 - xml 形式的 AOP

从零开始实现一个简易的Java MVC框架--实现IOC

H5一二事