控制反转:控制了对象的创建,反转:反转的是获取对象的方式,从自己创建对象变为由Spring工厂推送
还要导入:

先导入模板
- "1.0" encoding="utf8" ?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- 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
- http://www.springframework.org/schema/p
- http://www.springframework.org/schema/p/spring-p.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd "
- >
-
-
- beans>
新建Student类
- public class Student {
- private Integer stuno;
-
- private String stuname;
-
- private Integer stuage;
-
- public Integer getStuno() {
- return stuno;
- }
-
- public void setStuno(Integer stuno) {
- this.stuno = stuno;
- }
-
- public String getStuname() {
- return stuname;
- }
-
- public void setStuname(String stuname) {
- this.stuname = stuname == null ? null : stuname.trim();
- }
-
- public Integer getStuage() {
- return stuage;
- }
-
- public void setStuage(Integer stuage) {
- this.stuage = stuage;
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "stuno=" + stuno +
- ", stuname='" + stuname + '\'' +
- ", stuage=" + stuage +
- '}';
- }
-
- }
写个测试类test.java
来看以前的做法:先新建对象,在通过set()属性赋值

再来用IOC方法
1. 在application-context.xml文件中写bean,通过property标签设置属性

2. 回到test测试类
现在怎么创建student对象?
第一步:获取Spring上下文对象context
第二步:通过 context.getBean()获取刚刚在Spring配置文件application-context.xml文件中的bean(id="Student")

对象的创建和对象的赋值全部交给IOC容器
下面的代码,执行从springIOC容器中获取一个id为student的bean对象,该对象的类型为对应bean标签class的值
Student student = (Student)context.getBean("student");
理解工厂的概念:
创建一个ICourse接口:课程
- public interface ICourse {
- public void learn();
- }
不同课程例如JavaCourse、MybatisCourse实现这个接口
- public class JavaCourse implements ICourse {
- @Override
- public void learn() {
- System.out.println("learning Java...");
- }
- }
- public class MybatisCourse implements ICourse {
- @Override
- public void learn() {
- System.out.println("learning Mybatis...");
- }
- }
学生可以学习课程:为Student类添加方法 learnJava()、learnMybatis()
- public void learnJava(){
- ICourse iCourse = new JavaCourse();
- iCourse.learn();
- }
- public void learnMybatis(){
- ICourse iCourse = new MybatisCourse();
- iCourse.learn();
- }
测试一下:

上面是我们原始的开发方式
每次需要课程都要在Student类里面写一个learnXxx(),
如果要学习20个课程,就要20个方法,且每个方法里面都要创建一个课程的实现类对象

这种方式创建对象new非常零散,造成后期维护较为麻烦
创建工厂,由工厂统一的创建对象

根据名称创建课程对象
- public class CourseFactory {
- public static ICourse getCourse(String name){
- if(name.equalsIgnoreCase("java")){
- return new JavaCourse();
- }else {
- return new MybatisCourse();
- }
- }
- }
把Student中的learnJava()、learnMybatis()方法统一写在learn方法中,并给形参String name,并把name交给工厂CourseFactory来创建对象

测试:

现在要学习不同课程只需要改参数值就可以了,通过简单的工厂,可以将创建课程new 集中起来操作,方便后期维护
所以SpringIOC容器就是一个工厂,不需要我们去写CourseFactory,由SpringIOC容器为我们创建
我们只需要通过调用
为了更方便理解控制反转,我们也可以把这种方式叫做依赖注入
在spring配置文件中把Course的两个实现类设置bean

如下代码:在需要创建JavaCourse对象的地方通过spring上下文context调用getBean()方法拿对象,不需要自己创建
- 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.equalsIgnoreCase("java")) {
- return (ICourse) context.getBean("javaCourse");
- } else {
- return (ICourse) context.getBean("mybatisCourse");
- }
- }
- }
对比之前自己new对象
- public class CourseFactory {
- public static ICourse getCourse(String name){
- if(name.equalsIgnoreCase("java")){
- return new JavaCourse();
- }else {
- return new MybatisCourse();
- }
- }
- }