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