Spring-学习笔记1

Java-Spring-dljd-学习笔记




一、 Spring概述

1.1. 什么是spring

spring就是一个java框架,使用java语言开发的, 轻量级的, 开源的框架。
可以在j2se、j2ee项目中都可以使用。

spring核心技术: ioc, aop

spring又叫做:容器, spring作为容器, 装的是java对象。

spring作用: 实现解耦合, 解决java对象之间的耦合, 解决模块之间的耦合。

Spring官网https://spring.io/

1.2 Spring优点?

Spring 是一个框架,是一个半成品的软件。有20个模块组成。 它是一个容器管理对象,容器是装东西的,
Spring容器不装 文本,数字。装的是对象。 Spring 是存储对象的容器。

(1)轻量

Spring 框架使用的 jar 都比较小,一般在 1M 以下或者几百 kb。Spring核心功能的所需的 jar 总共在 3M 左右。
Spring 框架运行占用的资源少,运行效率高。不依赖其他 jar

(2)针对接口编程,解耦合

Spring 提供了 Ioc 控制反转,由容器管理对象,对象的依赖关系。原来在程序代码中的对象创建方式,
现在由容器完成。对象之间的依赖解耦合。

(3)AOP 编程的支持

通过 Spring 提供的 AOP 功能,方便进行面向切面的编程,许多不容易用传统 OOP 实现的功能可以通过 AOP 轻松应付在
Spring 中,开发人员可以从繁杂的事务管理代码中解脱出来,通过声明式方式灵活地进行事务的管理,提高开发效率和质量。

(4)方便集成各种优秀框架

Spring 不排斥各种优秀的开源框架,相反 Spring 可以降低各种框架的使用难度,Spring 提供了对各种优秀框架(如
Struts,Hibernate、MyBatis)等的直接支持。简化框架的使用。Spring
像插线板一样,其他框架是插头,可以容易的组合到一起。需要使用哪个框架,就把这个插头放入插线板。不需要可以轻易的移除。

二、IoC 控制反转

1.IoC 概念

IoC,Inversion of Control : 控制反转, 是一个理论,一个指导思想。 指导开发人员如何使用对象,管理对象的。
把对象的创建,属性赋值,对象的声明周期都交给代码之外的容器管理。

(1) IoC分为 控制和反转

控制: 对象创建,属性赋值, 对象声明周期管理

反转:把开发人员管理对象的权限转移给了代码之外的容器实现。 由容器完成对象的管理。

正转:开发人员在代码中, 使用 new 构造方法创建对象。 开发人员掌握了对象的创建,属性值,对象从开始到销毁的全部过程。 开发人员有对对象 全部控制。

(2) IoC的技术实现

DI ( 依赖注入) :Dependency Injection, 缩写是DI . 是IoC的一种技术实现。
程序只需要提供要使用的对象的名称就可以了, 对象如何创建, 如何从容器中查找,获取都由容器内部自己实现。

依赖名词: 比如说ClassA类使用了ClassB的属性或者方法,叫做ClassA依赖ClassB.

(3) Spring框架使用的DI实现IoC.

通过spring框架, 只需要提供要使用的对象名词就可以了。 从容器中获取名称对应的对象。

spring底层使用的 反射机制, 通过反射创建对象,给属性。

2. 开发工具准备

在这里插入图片描述

2.1 创建maven项目

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

2.2 引入maven依赖pom.xml

代码如下(示例):

<!--    junit 单元测试-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

<!--    spring依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>

3. Spring的第一个程序

使用spring: spring作为容器管理对象,开发人员从spring中获取要使用的对象。

实现步骤:
1.新建maven项目
2.加入依赖, 修改pom.xml spring-context : spring依赖junit: 单元测试
3.开发人员定义类: 接口和实现类 类也可以没有接口。 接口和实现类定义:和没有spring一样。
4.创建spring的配置文件。作用:声明对象。 把对象交给spring创建和管理。 使用表示对象声明,一个bean表示一个java对象。
5.使用容器中的对象。 创建一个表示spring容器的对象 Applicationcontext从容器中,根据名称获取对象,使用getBean(“对象名称”)

3.1 创建maven项目

在这里插入图片描述

3.2 引入 maven依赖 pom.xml

代码如下(示例):

<!--    junit 单元测试-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

<!--    spring依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>

3.3 定义接口与实体类

在这里插入图片描述
代码如下(示例):

public interface SomeService {
    void doSome();
}

在这里插入图片描述
在这里插入图片描述
代码如下(示例):

import com.it.service.SomeService;

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

在这里插入图片描述

3.4 创建Spring配置文件

在src/main/resources/目录现创建一个xml文件,文件名可以随意,但Spring 建议的名称为
applicationContext.xml.

spring 配置中需要加入约束文件才能正常使用,约束文件是xsd扩展名。
在这里插入图片描述

**spring标准的配置文件:**
(1) 根标签是beans
(2) beans 后面的是约束文件说明
(3) beans里面是bean声明。
(4) 什么是bean: bean就是java对象, spring容器管理的java对象,叫做bean

在这里插入图片描述
代码如下(示例):

<?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">

<!--    声明对象
        id:自定义对象名称,唯一值。(可以没有,spring可以提供默认名称)
        class:类的全限定名称,spring通过反射机制创建对象,不能是接口
        spring 根据id,class创建对象,把对象放入到spring的一个map对象
        map.put(id,对象)
        -->
    <bean id="someService" class="com.it.service.impl.SomeServiceImpl"/>

</beans>

:用于定义一个实例对象。一个实例对应一个bean元素。

id:该属性是Bean实例的唯一标识,程序通过id属性访问Bean,

Bean与Bean间的依赖关系也是通过id属性关联的。 class:指定该 Bean所属的类,注意这里只能是类,不能是接口。

3.5 运用spring创建对象

在这里插入图片描述
代码如下(示例):

import com.it.service.SomeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppMain {
    public static void main(String[] args) {
        // SomeService service = new SomeServiceImpl();
        // service.doSome();

        // 1.指定spring配置文件:从类路径(classpath)之下开始的路径
        String config = "beans.xml";
        // 2、创建容器对象,ApplicationContext 表示spring容器对象。通过ctx获取摸个java对象
        // ClassPathXmlApplicationContext(config) 表示从类路径中获取容器对象
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        // 3、从spring容器中获取指定名称的对象,使用getBean("id")
        SomeService service = (SomeService) ctx.getBean("someService");
        // 4、调用对象的方法,接口中的方法
        service.doSome();
    }
}

3.6 定义测试类

在这里插入图片描述
代码如下(示例):

import com.it.service.SomeService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    // spring创建对象,调用的是那个方法?
    /*
*  默认是调用类的无参数构造方法
* */
    @Test
    public void Test01(){
        // 1,指定配置文件的类路径
        String config = "beans.xml";
        // 2.创建spring容器
        ApplicationContext cxt = new ClassPathXmlApplicationContext(config);
        // 3.从spring容器中获取指定的对象
        SomeService service = (SomeService) cxt.getBean("someService");
        // 4.调用方法
        service.doSome();
    }
}

在这里插入图片描述
在这里插入图片描述
代码如下(示例):

public SomeServiceImpl() {
    System.out.println("执行了SomeServiceImpl中的无参构造方法~");
}

再次执行测试类Test01
在这里插入图片描述
在这里插入图片描述

3.6.2 创建测试类Test02

代码如下(示例):

/*
* spring是什么时候创建的对象?
* 创建spring容器对象的时候,会读取配置文件,创建文件中声明的java对象。
*
* 优点:
*   获取对象的速度快,因为对象已经创建好了
*
* 缺点:
*   占用内存
* */

@Test
public void Test02(){
    String config = "beans.xml";
    ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
    //SomeService service = (SomeService) ctx.getBean("someService");
    //service.doSome();
}

在这里插入图片描述

3.6.3 创建测试类Test03

在这里插入图片描述
代码如下(示例):

/*
* spring容器创建对象,一次创建几个
* 在创建容器(ApplicationContext)对象时,会把配置文件中的所有对象都创建出来(spring的默认规则)
* */
@Test
public void Test03(){
    String config = "beans.xml";
    ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
    //SomeService service = (SomeService) ctx.getBean("someService");
    //service.doSome();
}

在这里插入图片描述

3.6.4 定义测试类Test04(获取容器中对象的信息)

代码如下(示例):

/*
* 获取容器中对象的信息
* */
@Test
public void test04(){
    String config = "beans.xml";
    ApplicationContext ac = new ClassPathXmlApplicationContext(config);
    // 获取容器中定义对象的数量
    int count = ac.getBeanDefinitionCount();
    System.out.println("容器中定义对象的数量==" + count);
    // 获取容器中定义对象的名称
    String[] names = ac.getBeanDefinitionNames();
    for (String name : names) {
        System.out.println("容器中对象的名称==" + name);
    }

在这里插入图片描述

3.6.5 使用 spring 创建非自定义类对象

在这里插入图片描述
代码如下(示例):

/*
* 让spring创建非自定义类的对象
* 有class就能让spring创建对象
* */
@Test
public void test05(){
    String config = "beans.xml";
    ApplicationContext ac = new ClassPathXmlApplicationContext(config);
    Date date = (Date) ac.getBean("mydate");
    System.out.println("date==" + date);
}

在这里插入图片描述
####3.6.6 使用spring给没有接口的类创建对象
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码如下(示例):

OtherService otherService = (OtherService) ac.getBean("otherService");
otherService.doOther();

在这里插入图片描述

3.7 spring容器创建对象的特点

  1. 容器对象ApplicationContext:接口通过ApplicationContext对象,获取要使用的其他java对象,执行getBean(“的id”)
  1. spring默认是调用类的无参数构造方法,创建对象
  1. spring读取配置文件,一次创建好所有的java对象,都放到map中。

4. DI:给属性赋值

spring调用类的无参数构造方法,创建对象。
对象创建后给属性赋值。
给属性赋值可以使用

(1) xml配置文件中的标签和属性;
(2)使用注解。

DI分类:
1 set注入,也叫做设值注入;
2构造注入。

4.1 基于xml的DI (基本数据类型)

在xml配置文件中使用标签和属性,完成对象创建,属性赋值。
(1) set注入,也叫做设值注入。

4.1.1 创建环境(基本语法)

概念: spring调用类中的set方法,在set方法中可以完成属性赋值。推荐使用。

(1) 创建一个新的模块
在这里插入图片描述
(2) 修改pom.xml配置文件
代码如下(示例):

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
<!--     测试坐标-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

<!--    Spring坐标-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>

(3) 创建Student类
在这里插入图片描述
代码如下(示例):

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

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

(3) 创建配置文件 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对象-->
    <bean id="myStudent" class="org.it.ba01.Student">

    </bean>

</beans>

(4) 创建测试类
在这里插入图片描述

代码如下(示例):

import org.it.ba01.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

(5) 运行测试结果如下
在这里插入图片描述
(6) 使用set注入给属性赋值
在这里插入图片描述
代码如下(示例):

<!--  声明bean对象-->
    <!--
        DI:狗日属性赋值
        简单类型:java中的基本数据类型和String
        1. set注入:Spring调用类的set方法,通过set方法完成属性赋值
            简单类型的set注入:
            语法:<bean id = "xxx" class = "yyy">
                    <property name = "属性名" value = "简单类型属性值"/>
                    ...
                </bean>
    -->
    <bean id="myStudent" class="org.it.ba01.Student">
        <property name="age" value="20"/>
        <property name="name" value="李四"/>
    </bean>

(7) 运行测试结果如下
在这里插入图片描述
(8) 修改属性的值只需要该配置文件中的属性赋值
在这里插入图片描述
(9) 运行测试结果如下
在这里插入图片描述

4.1.2 必须有set方法 (set注入执行的是Set方法完成赋值)

在这里插入图片描述
在Student类中添加以下代码
在这里插入图片描述
运行结果如下
在这里插入图片描述

4.1.3 set注入执行的顺序

在这里插入图片描述
执行结果如下
在这里插入图片描述

4.1.4 给非自定义类属性赋值

(1)声明一个日期类
在这里插入图片描述
代码如下(示例):

<!--  声明日期类-->
    <bean id="mydate" class="java.util.Date">
        <property name="time" value="944449999999"/>
    </bean>

(2)在测试代码中添加以下代码
在这里插入图片描述
代码如下(示例):

Date date = (Date) ac.getBean("mydate");
        System.out.println("date===" + date);

(3)测试代码结果
在这里插入图片描述

4.2基于xml的DI(引用类型)

4.2.1 创建环境(基本语法)

(1)在ba02下创建Student类
在这里插入图片描述
代码如下(示例):

public class Student {
    private String name;
    private int age;
    // 引用类型
    private School school;

    public Student() {
        System.out.println("执行了Student的无参构造方法~~");
    }

    public void setName(String name) {
        System.out.println("setName===" + name);
        this.name = name;
    }

    public void setAge(int age) {
        System.out.println("setAge===" + age);
        this.age = age;
    }

    public void setSchool(School school) {
    System.out.println("setSchool===" + school);
        this.school = school;
    }

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

(2)在ba02下创建School类
在这里插入图片描述
代码如下(示例):

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 + '\'' +
                '}';
    }
}

(3)在ba02下创建xml配置文件
在这里插入图片描述
代码如下(示例):

<!--  声明bean对象-->
    <!--
        DI:给属性赋值
        2. 引用类型set注入:
            语法:<bean id = "xxx" class = "yyy">
                    <property name = "属性名" ref = "bean的id"/>
                    ...
                </bean>
    -->
<!--    简单类型set注入-->
    <bean id="myStudent" class="org.it.ba02.Student">
        <property name="age" value="30"/>
        <property name="name" value="李四"/>
<!--        引用类型的赋值-->
        <property name="school" ref="myschool"/>
    </bean>

<!--    声明School-->
    <bean id="myschool" class="org.it.ba02.School">
        <property name="name" value="北京大学"/>
        <property name="address" value="北京的海淀区"/>
    </bean>

(4)定义测试方法
在这里插入图片描述
代码如下(示例):

import org.it.ba02.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest02 {
    @Test
    public void test02(){
        String config = "ba02/applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        Student student = (Student) ac.getBean("myStudent");
        System.out.println("student===" + student);
    }
}

(5)测试结果如图
在这里插入图片描述

4.3 构造注入

构造注入:spring调用类中的有参数构造方法, 在创建对象的同时,给属性赋值

4.3.1 创建环境(基本语法)使用name属性

(1)在ba03下创建School类
在这里插入图片描述
代码如下(示例):

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 + '\'' +
                '}';
    }
}

(2)在ba03下创建Student类
在这里插入图片描述
代码如下(示例):

public class Student {
    private String name;
    private int age;
    // 引用类型
    private School school;

    public Student() {
        System.out.println("执行了Student的无参构造方法~~");
    }
    // 创建有参构造方法

    public Student(String name, int age, School school) {
        System.out.println("执行了Student的有参构造方法~~");
        this.name = name;
        this.age = age;
        this.school = school;
    }

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

(3)在ba03下创建Spring xml配置文件
在这里插入图片描述
代码如下(示例):

<!--  声明bean对象-->
    <!--
        DI:给属性赋值
        3.构造注入:Spring调用类的有参构造方法,创建对象同时给属性赋值
            语法:<bean id = "xxx" class = "yyy">
                    <constructor-arg>: 表示一个构造方法的形参
                    标签有属性:name:构造方法形参名
                              index: 构造方法的参数位置
                              value:简单类型的形参值
                              ref:  引用类型的形参值
                </bean>
    -->
    <!--   构造注入,使用name属性-->
    <bean id="myStudent" class="org.it.ba03.Student">
        <constructor-arg name="name" value="李四"/>
        <constructor-arg name="age" value="22"/>
        <constructor-arg name="school" ref="myschool"/>
    </bean>

    <!--    声明School-->
    <bean id="myschool" class="org.it.ba03.School">
        <property name="name" value="北京大学"/>
        <property name="address" value="北京的海淀区"/>
    </bean>

(4)创建MyTest03测试类

在这里插入图片描述
代码如下(示例):

import org.it.ba03.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest03 {
    @Test
    public void test01(){
        String config = "ba03/applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        Student student = (Student) ac.getBean("myStudent");
        System.out.println("student===" + student);
    }
}

(5)执行结果如下图

在这里插入图片描述

4.3.2 使用index属性

(1)在xml配文件中添加以下代码
在这里插入图片描述
代码如下(示例):

<!--    构造注入,使用index属性,参数的位置,构造方法参数从左往右位置是0,1,2....-->
    <bean id="myStudent2" class="org.it.ba03.Student">
        <constructor-arg index="0" value="张三"/>
        <constructor-arg index="1" value="20"/>
        <constructor-arg index="2" ref="myschool"/>
    </bean>

(2)在测试类中添加以下代码
在这里插入图片描述
代码如下(示例):

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

(3)运行结果如下图所示
在这里插入图片描述

4.3.3 省略index属性

(1)在xml配文件中添加以下代码
在这里插入图片描述

代码如下(示例):

    <!--    构造注入,省略index属性-->
    <bean id="myStudent3" class="org.it.ba03.Student">
        <constructor-arg  value="王五"/>
        <constructor-arg  value="26"/>
        <constructor-arg  ref="myschool"/>
    </bean>

(2)在测试类中添加以下代码
在这里插入图片描述
代码如下(示例):

    @Test
    public void test03(){
        String config = "ba03/applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        Student student = (Student) ac.getBean("myStudent3");
        System.out.println("student===" + student);
    }

(3)运行结果如下图所示
在这里插入图片描述

4.3.4 创建File对象

(1)找到File类并打开
在这里插入图片描述
(2)File类中的方法
在这里插入图片描述
在这里插入图片描述

(3)在xml配置文件中添加以下代码
在这里插入图片描述
在这里插入图片描述

代码如下(示例):

<!--    声明File对象-->
    <bean id="myFile" class="java.io.File">
        <constructor-arg name="parent" value="F:\\PS资料\\Ps历史记录"/>
        <constructor-arg name="child" value="Photoshop 编辑日志.txt"/>
    </bean>

(4)定义测试类
在这里插入图片描述
代码如下(示例):

    @Test
    public void test04(){
        String config = "ba03/applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        File file  = (File) ac.getBean("myFile");
        System.out.println("file===" + file.getName());
    }

(5)运行结果如图所示
在这里插入图片描述

4.4 引用类型的自动注入

概念:Spring可以根据某些规则给引用类型完成赋值。只对引用类型有效。规则byName,byType

  1. byName(按名称注入):java类中引用类型属性名称和Spring容器中bean的id名称一样的,且数据类型也是一样的,这些bean能够赋值给引用类型。
  2. byType (按类型注入):java类中引用类型的数据类型和Spring容器中bean的class值是同源关系的,这样的bean赋值给引用类型。
4.4.1 环境准备
  1. 在ba04下创建Student类和School类
    在这里插入图片描述
    Student类 代码如下(示例):
public class Student {
    private String name;
    private int age;
    // 引用类型
    private School school;

    public Student() {
        System.out.println("执行了Student的无参构造方法~~");
    }

    public void setName(String name) {
        System.out.println("setName===" + name);
        this.name = name;
    }

    public void setAge(int age) {
        System.out.println("setAge===" + age);
        this.age = age;
    }

    public void setSchool(School school) {
        System.out.println("setSchool===" + school);
        this.school = school;
    }

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

School类 代码如下(示例):

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 + '\'' +
                '}';
    }
}
  1. 在ba04下创建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对象-->

    <!--    简单类型set注入-->
    <bean id="myStudent" class="org.it.ba04.Student">
        <property name="age" value="30"/>
        <property name="name" value="李四"/>
        <!--        引用类型的赋值-->
        <property name="school" ref="myschool"/>
    </bean>
    
    <!--    声明School-->
    <bean id="myschool" class="org.it.ba04.School">
        <property name="name" value="北京大学"/>
        <property name="address" value="北京的海淀区"/>
    </bean>
</beans>
  1. 创建MyTest测试类
    在这里插入图片描述
    代码如下(示例):
import org.it.ba04.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest04 {
    @Test
    public void test01(){
        String config = "ba04/applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        Student student = (Student) ac.getBean("myStudent");
        System.out.println("student===" + student);
    }

}
  1. 运行结果如下(结果正常)
    在这里插入图片描述
4.1.2 使用byName(按名称自动注入)
  1. byName(按名称注入):java类中引用类型属性名称和Spring容器中bean的id名称一样的,且数据类型也是一样的,这些bean能够赋值给引用类型。
  1. 修改xml配置文件
    在这里插入图片描述
    代码如下(示例):
<!--  声明bean对象-->
    <!--
        引用类型自动注入:Spring根据byName,byType规则给引用类型赋值
        1. byName(按名称注入):java类中引用类型的属性名称和Spring容器中bean的id名称一样,
                                且数据类型一样的,这样的bean能够赋值给引用类型
         语法:
            <bean id = "xxx" class = "yyy" autowire = "byName">
                简单类型属性赋值
            </bean>
    -->

    <!--    byName自动注入-->
    <bean id="myStudent" class="org.it.ba04.Student" autowire="byName">
        <property name="age" value="30"/>
        <property name="name" value="李四"/>
        <!--        引用类型的赋值-->
<!--        <property name="school" ref="myschool"/>-->
    </bean>

    <!--    声明School-->
    <bean id="school" class="org.it.ba04.School">
        <property name="name" value="清华大学"/>
        <property name="address" value="北京的海淀区"/>
    </bean>
</beans>
4.1.3 使用byType(按类型自动注入)
4.1.3.1 同源关系1(class值一样)
  1. 将ba04中的类复制到ba05中
    在这里插入图片描述
  2. 将ba04中的配置文件复制到ba05中 并修改代码
    在这里插入图片描述
    代码如下(示例):
<!--  声明bean对象-->
    <!--
        引用类型自动注入:Spring根据byName,byType规则给引用类型赋值
         2. byType(按类型注入):java类中引用类型的数据类型和bean的class是同源的,
                                这些的bean能够赋值给引用类型。
             同源关系:
                1.java中引用类型的数据类型和bean的class值是一样的
                2.java中引用类型的数据类型和bean的class值是父子类关系的
                3.java中引用类型的数据类型和bean的class值是接口和实现类关系的
             语法:
                <bean id = "xxx" class = "yyy" autowire = "byType">
                简单类型属性赋值
            </bean>
            注意:在xml配置文件中,符合条件的对象,只能有一个
                 多余一个是报错的
    -->
    <!--    byType自动注入-->
    <bean id="myStudent" class="org.it.ba05.Student" autowire="byType">
        <property name="age" value="26"/>
        <property name="name" value="赵六"/>
        <!--        引用类型的赋值-->
<!--        <property name="school" ref="mySchool"/>-->
    </bean>

    <!--    声明School-->
    <bean id="mySchool" class="org.it.ba05.School">
        <property name="name" value="航空大学"/>
        <property name="address" value="北京的海淀区"/>
    </bean>
</beans>
  1. 创建测试类MyTest05
    在这里插入图片描述

代码如下(示例):

import org.it.ba05.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest05 {
    @Test
    public void test01(){
        String config = "ba05/applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        Student student = (Student) ac.getBean("myStudent");
        System.out.println("student===" + student);
    }
}
  1. 运行结果如下
    在这里插入图片描述
4.1.3.2 同源关系2(父子类关系)

(1)在ba05下创建PrimarySchool类 并继承School类
在这里插入图片描述
代码如下(示例):

/**
 *  子类
 */
public class PrimarySchool extends School{
}

(2)在xml配置文件中添加以下代码
在这里插入图片描述
代码如下(示例):

<!--     声明School子类-->
        <bean id="primarySchool" class="org.it.ba05.PrimarySchool">
            <property name="name" value="北京大兴小学"/>
            <property name="address" value="北京的大兴区"/>
        </bean>

(3)运行测试结果如下
在这里插入图片描述

4.1.3.3 同源关系3(接口和实现类关系)

该处使用的url网络请求的数据。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七@归七

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值