public class Student { private int id; private String name; private String age; public Student(){ } public Student(int id, String name, String age) { this.id = this.id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
public interface StudentDao { boolean saveStudents(Student s); } public class StudentDaoImpl implements StudentDao { @Override public boolean saveStudents(Student s) { // TODO Auto-generated method stub if (s != null){ System.out.println("学号=" + s.getId()); System.out.println("姓名=" + s.getName()); System.out.println("年龄=" + s.getAge()); return true; } return false; } }
public class StudentService { private StudentDao sDao; private Student s; public Student getS() { return s; } public void setS(Student s) { this.s = s; } public StudentDao getsDao() { return sDao; } public void setsDao(StudentDao sDao) { this.sDao = sDao; } public boolean saveStudent(){ if (sDao.saveStudents(s)){ return true; }else{ return false; } } }
public class testStudentService extends TestCase { public void testSaveStudent() { ApplicationContext ctx = new ClassPathXmlApplicationContext("lesson2.xml"); StudentService stService = (StudentService) ctx.getBean("studentService"); Assert.assertEquals(true, stService.saveStudent()); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id = "student" class="bupt.lesson2.Student"> <constructor-arg index="0" value="10" /> <constructor-arg index="1" type="java.lang.String" value="abc" /> <constructor-arg index="2" type="java.lang.String" value="26" /> </bean> <bean id = "studentDao" class="bupt.lesson2.StudentDaoImpl"/> <bean id = "studentService" class="bupt.lesson2.StudentService"> <property name="sDao" ref="studentDao"/> <property name="s" ref="student"/> </bean> </beans>