一、控制反转:将创建对象、属性值的方式进行了反转,从new、setxxx()变为从SpringIOC中getbean();
二、依赖注入:将属性值注入给属性,将属性注入给bean,将bean注入给了SpringIOC;
创建接口类,ICourse.java:
package org.ruangong.newinstance; public interface ICourse { void learn();//学习 }
创建实现类,HtmlCourse:
package org.ruangong.newinstance; public class HtmlCourse implements ICourse{ @Override public void learn() { // TODO Auto-generated method stub System.out.println("学习html中......"); } }
创建实现类,JavaCourse:
package org.ruangong.newinstance; public class JavaCourse implements ICourse{ @Override public void learn() { // TODO Auto-generated method stub System.out.println("学习JAVA......"); } }
实体类:
package org.ruangong.entity; import org.ruangong.factory.CourseFactory; import org.ruangong.newinstance.HtmlCourse; import org.ruangong.newinstance.ICourse; import org.ruangong.newinstance.JavaCourse; public class Student { private int stuNo; private String stuName; private int stuAge; public int getStuNo() { return stuNo; } public void setStuNo(int stuNo) { this.stuNo = stuNo; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public int getStuAge() { return stuAge; } public void setStuAge(int stuAge) { this.stuAge = stuAge; } @Override public String toString() { return "Student [stuNo=" + stuNo + ", stuName=" + stuName + ", stuAge=" + stuAge + "]"; } public void learnJavaWithIoc(){ ICourse course = new JavaCourse(); course.learn(); } public void learnHtmlWithIoc(){ ICourse course = new HtmlCourse(); course.learn(); } public void learnAnyone(String name){ ICourse course = CourseFactory.getCourse(name); course.learn(); } }
工厂类:
package org.ruangong.factory; import org.ruangong.newinstance.HtmlCourse; import org.ruangong.newinstance.ICourse; import org.ruangong.newinstance.JavaCourse; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class CourseFactory { public static ICourse getCourse(String name){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); if(name.equals("java")){ return (ICourse) context.getBean("JavaCourse"); }else{ return (ICourse) context.getBean("HtmlCourse"); } } }
测试类:
public static void learnCourseWithFactory(){
Student student = new Student();
student.learnAnyone("java");
}
ApplicationContext.xml文件;
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student" class="org.ruangong.entity.Student"> <property name="stuNo" value="20173681"></property> <property name="stuName" value="靳晨晨"></property> <property name="stuAge" value="21"></property> </bean> <bean id="JavaCourse" class="org.ruangong.newinstance.JavaCourse"></bean> <bean id="HtmlCourse" class="org.ruangong.newinstance.HtmlCourse"></bean> </beans>