【框架:Spring】万字详解IOC控制反转(上:注入)

1.入门

1.1简介

spring可以减轻项目模块之间的管理,类和类之间的管理。帮助开发人员创建对象,管理对象之间的关系。spring的核心技术是IOC和AOP。

1.2优点

  • 轻量
  • 针对接口编程,解耦合
  • AOP编程的支持
  • 方便集成各种框架

1.3 Spring的体系结构

在这里插入图片描述

2.IOC控制反转

2.1简介

在这里插入图片描述

2.2IOC的技术实现

在这里插入图片描述

2.3 创建第一个Spring

在这里插入图片描述

2.3.1创建maven项目

在这里插入图片描述

2.3.2 加入maven依赖包

<!--spring依赖-->
	<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>
 <!--单元测试-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

2.3.3创建类

在这里插入图片描述

接口

package com.sdnu.service;

public interface SomeService {
    void doSome();
}

实现类

package com.sdnu.service.impl;

import com.sdnu.service.SomeService;

public class SomeServiceImpl implements SomeService {
    @Override
    public void doSome() {
        System.out.println("执行了SomeServiceImpl的doSome方法");
    }
}

测试代码

    @Test
    public void test1(){
        SomeService service = new SomeServiceImpl();
        service.doSome();
    }

2.3.4 创建spring需要使用的配置信息

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

<?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="someService" class="com.sdnu.service.impl.SomeServiceImpl"></bean>
</beans>

2.3.5创建spring

    @Test
    public void test2(){
        //1.指定spring配置文件的名称
        String config = "beans.xml";
        //2.创建表示spring容器的对象,ApplicationContext
        //ApplicationContext就是表示spring容器,通过容器获取对象
        //ClassPathXmlApplicationContext表示从类路径加载spring的配置文件
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        //从容器中获取某个对象,你要调用对象的方法
        //getBean("配置文件的bean的id值")
        SomeService service = (SomeService) ac.getBean("someService");
        //使用spring创建好的对象
        service.doSome();
    }

2.3.6spring创建对象的时机

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

2.3.6 获取容器对象信息

我们在beans.xml中设置两个bean对象

<bean id="someService" class="com.sdnu.service.impl.SomeServiceImpl"></bean>
<bean id="someService2" class="com.sdnu.service.impl.SomeServiceImpl"></bean>

测试程序

    @Test
    public  void test3(){
        String config = "beans.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        int count = ac.getBeanDefinitionCount();
        System.out.println(count);
        String[] name = ac.getBeanDefinitionNames();
        for(String item : name) {
            System.out.println(item);
        }
    }

在这里插入图片描述

2.3.7创建非自定义对象

    @Test
    public void test4(){
        String config = "beans.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        Date myDate = (Date)ac.getBean("someService3");
        System.out.println("my Date is :" + myDate);
    }

在这里插入图片描述

2.4基于XML的DI

2.4.1注入分类

在这里插入图片描述
在这里插入图片描述
实现步骤:
在这里插入图片描述

2.4.2 set注入

在这里插入图片描述

(1)简单类型:
在这里插入图片描述
ApplicationContext.xml

    <bean id="myStudent" class="com.sdnu.ba01.Student">
        <property name="name" value="wangwu"/>
        <property name="age" value="21"/>
    </bean>

Student.java

package com.sdnu.ba01;

public class Student {
    private String name;
    private Integer age;

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

测试程序

    @Test
    public void test1(){
        String config = "ba01/ApplicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        Student student = (Student) ac.getBean("myStudent");
        System.out.println(student);
    }

在这里插入图片描述
(2)引用类型
在这里插入图片描述
School类在下面还会使用,就不重复写了。
School.java

package com.sdnu.ba02;

public class School {
    private String name;
    private String address;

    public void setName(String name) {
        this.name = name;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

ApplicationContext.xml

    <bean id="mySchool2" class="com.sdnu.ba02.School">
        <property name="name" value="深圳大学"/>
        <property name="address" value="深圳市"/>
    </bean>
    <bean id="myStudent2" class="com.sdnu.ba02.Student">
        <property name="name" value="张三"/>
        <property name="age" value="21"/>
        <property name="school" ref="mySchool2"/>
    </bean>

Student.java

package com.sdnu.ba02;

public class Student {
    private String name;
    private Integer age;
    private School school;

    public void setSchool(School school) {
        this.school = school;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", school=" + school +
                '}';
    }
}

测试程序

    @Test
    public void test2(){
        String config = "ba01/ApplicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        Student student = (Student) ac.getBean("myStudent2");
        System.out.println(student);
    }

2.4.2构造注入

在这里插入图片描述

  • name

ApplicationContext.xml

    <bean id="mySchool3" class="com.sdnu.ba03.School">
        <property name="name" value="深圳大学"/>
        <property name="address" value="深圳市"/>
    </bean>
    <bean id="myStudent3" class="com.sdnu.ba03.Student">
        <constructor-arg name="name" value="张三"/>
        <constructor-arg name="age" value="21"/>
        <constructor-arg name="school" ref="mySchool3"/>
    </bean>

Student.java

package com.sdnu.ba03;

public class Student {
    private String name;
    private Integer age;
    private School school;

    public Student(String name, Integer age, School school) {
        this.name = name;
        this.age = age;
        this.school = school;
    }

}

测试程序

    @Test
    public void test3(){
        String config = "ba01/ApplicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        Student student = (Student) ac.getBean("myStudent3");
        System.out.println(student);
    }
  • index
    <!--index-->
    <bean id="myStudent3Index" class="com.sdnu.ba03.Student">
        <constructor-arg index = "0" value="张三"/>
        <constructor-arg index = "1" value="21"/>
        <constructor-arg index="2" ref="mySchool3"/>
    </bean>

  • 省略
    <!--省略-->
    <bean id="myStudent3Omit" class="com.sdnu.ba03.Student">
        <constructor-arg  value="张三"/>
        <constructor-arg  value="21"/>
        <constructor-arg ref="mySchool3"/>
    </bean>

2.4.3创建文件对象

ApplicationContext.xml

    <!--创建文件对象-->
    <bean id="myfile" class="java.io.File">
        <constructor-arg name="parent" value="D:\test\spring"/>
        <constructor-arg name="child" value="happy.txt"/>
    </bean>

测试程序

    @Test
    public void test3File(){
        String config = "ba01/ApplicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        File file = (File) ac.getBean("myfile");
        System.out.println(file);
    }

2.4.4引用类型自动注入

在这里插入图片描述
在这里插入图片描述
(1)byName

在这里插入图片描述

ApplicationContext.xml


    <!--注意id与Student.java中的Student类的school属性相同-->
    <bean id="school" class="com.sdnu.ba04.School">
        <property name="name" value="深圳大学"/>
        <property name="address" value="深圳市"/>
    </bean>
    <bean id="myStudent" class="com.sdnu.ba04.Student" autowire="byName">
        <property name="name" value="张三"/>
        <property name="age" value="21"/>
        <!--
        <property name="school" ref="mySchool"/>
        -->
    </bean>

在这里插入图片描述
(2)byType

ApplicationContext.xml

   <bean id="school" class="com.sdnu.ba05.School">
        <property name="name" value="深圳大学"/>
        <property name="address" value="深圳市"/>
    </bean>
    <bean id="myStudent" class="com.sdnu.ba05.Student" autowire="byType">
        <property name="name" value="张三"/>
        <property name="age" value="21"/>
        <!--
        <property name="school" ref="mySchool"/>
        -->
    </bean>

在这里插入图片描述

作者:Beyong    
出处:Beyong博客
github地址:https://github.com/beyong2019

本博客中未标明转载的文章归作者Beyong有,欢迎转载,但未经作者同意必须保留此段声明,且在文章明显位置给出原文连接,否则保留追究法律责任的权利。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值