IOC示例,属性注入,构造函数注入
2013-09-07 13:57
288 查看
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>
相关文章推荐
- (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)
- IOC 构造函数注入vs属性注入
- 控制反转(IOC)、依赖注入(DI)之通过构造函数注入简单属性
- ASP.NET Spring.Net 入门2 简单入门示例--属性与构造函数注入
- My.Ioc 代码示例——属性和方法注入
- Spring实现控制反转(IOC)的三种方式(一)——属性注入
- Ninject依赖注入——构造函数、属性、方法和字段的注入(三)
- 《step3:tiny-Spring-IoC学习三》——为bean注入属性
- Spring笔记之一:依赖注入的方式属性注入和构造函数注入
- spring Ioc 依赖注入的三种方式:构造函数注入、setter方法注入和接口注入
- Spring--IoC--域属性的注入-使用@Resource注解(JSR)
- 在 MVC 控制器中使用 构造函数时行依赖注入 (IoC)
- 10、IoC之依赖注入(3)-解析注入的属性
- Ninject依赖注入——构造函数、属性、方法和字段的注入
- Spring依赖属性注入和构造函数注入
- Ninject依赖注入——构造函数、属性、方法和字段的注入
- 【wyy】springIOC之属性注入
- Autofac 的属性注入,IOC的坑
- WebAPI2使用Autofac实现IOC属性注入完美解决方案
- spring反转控制IOC或依赖注入之简单示例