程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Spring 一二事(6),spring

Spring 一二事(6),spring

編輯:JAVA綜合教程

Spring 一二事(6),spring


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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved