Spring之Bean的依赖注入(基于XML)

简介: Spring之Bean的依赖注入(基于XML)

0x00 教程内容


  1. Spring配置文件的编写
  2. 测试

紧接着前一篇文章:JDBC实现MySQL数据库的增删改查,这篇文章补一下基础,以方便后面的学习。


0x01 Spring配置文件的编写


1. 引入依赖

JDBC依赖包后面还会用到,所以此处先引入:


<!-- Spring JDBC依赖包 -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>5.2.4.RELEASE</version>
</dependency>
<!-- Spring context依赖 -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.2.4.RELEASE</version>
</dependency>


2. 新建Spring的配置文件

IDEA里有内置的生成方式,如图:

微信图片_20220619184025.png


此处我取名为:spring.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">
</beans>


当然,自己新建一个文件,然后再加入相关的内容也可以,当然不排除有些同学的IDEA是没有那个自动生成的选项的,那怎么找呢?方法有以下如下:


1、把我上面的copy进去或者自己网上搜一份,但是悄悄告诉你,我上面的只适合本次教程哈。

2、直接在官网找,地址直达:https://docs.spring.io/spring-framework/docs/5.2.4.RELEASE/spring-framework-reference/core.html#beans-factory-metadata


微信图片_20220619184048.png


拉下一点点就可以看到了:


微信图片_20220619184056.png


然后在配置文件里面加入需要注入的Bean,我这里是将Student注入进入,并且加入了几个属性值:

<bean id="student" class="com.shaonaiyi.domain.Student">
    <property name="id" value="1"/>
    <property name="name" value="shaonaiyi"/>
    <property name="age" value="18"/>
</bean>


image.png


此过程就相当于 IoC 容器新建了一个Student对象,名字为student,并且注入了id、name、age这三个属性的值,那竟然是新建了一个对象,并且设置了属性值,那应该可以将其打印出来,下一步我们继续操作。


0x02 测试结果


1. 新建一个测试类

image.png


编写代码如下:

package com.shaonaiyi.domain;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * @Auther: shaonaiyi@163.com
 * @Date: 2021/1/16 11:45
 * @Description: 测试Bean依赖注入
 */
public class StudentTest {
    private ApplicationContext applicationContext = null;
    private Student student = null;
    @Before
    public void before() {
        applicationContext = new ClassPathXmlApplicationContext("spring.xml");
    }
    @After
    public void after() {
        applicationContext = null;
    }
    @Test
    public void testStudent() {
        student = (Student) applicationContext.getBean("student");
        System.out.println("---------------------");
        System.out.println("学生id:" + student.getId());
        System.out.println("学生名称:" + student.getName());
        System.out.println("学生年龄:" + student.getAge());
    }
}


2. 运行结果

image.png


至此,我们就实现了从IoC容器里面获取对象了。


0xFF 总结


  1. 本文章为Java的Spring基础知识,因为写博客需要,所以顺便写了出来。
  2. 下一篇教程,我们会通过JDBCTemplate方式实现对数据库的访问,链接跳转:使用JdbcTemplate对MySQL数据库进行增删改查
相关文章
|
8天前
|
XML Java 程序员
Spring6框架中依赖注入的多种方式(推荐构造器注入)
依赖注入(DI)是一种过程,对象通过构造函数参数、工厂方法的参数或在对象实例构建后设置的属性来定义它们的依赖关系(即与其一起工作的其他对象)。
19 3
|
1天前
|
Java Spring 缓存
Spring Bean循环依赖详解
【6月更文挑战第2天】
7 2
|
5天前
|
存储 Java 数据库
Spring的使用-Bean对象的储存和获取/Bea对象的作用域与生命周期
Spring的使用-Bean对象的储存和获取/Bea对象的作用域与生命周期
|
10天前
|
Java Spring 容器
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入
22 1
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入
|
10天前
|
Java 容器 Spring
Spring的加载配置文件、容器和获取bean的方式
Spring的加载配置文件、容器和获取bean的方式
21 3
Spring的加载配置文件、容器和获取bean的方式
|
10天前
|
Java Spring 容器
Spring依赖注入方式,自动装配及自动装配特征
Spring依赖注入方式,自动装配及自动装配特征
11 1
|
10天前
|
Java Spring 容器
Spring核心概念、IoC和DI的认识、Spring中bean的配置及实例化、bean的生命周期
Spring核心概念、IoC和DI的认识、Spring中bean的配置及实例化、bean的生命周期
28 0
|
10天前
|
XML Java 数据格式
Spring框架学习 -- Bean的生命周期和作用域
Spring框架学习 -- Bean的生命周期和作用域
17 2
|
10天前
|
存储 XML Java
Spring框架学习 -- 读取和存储Bean对象
Spring框架学习 -- 读取和存储Bean对象
14 0
Spring框架学习 -- 读取和存储Bean对象
|
XML Java API
Spring 依赖注入的方式,你了解哪些?
前言 依赖查找和依赖注入是 Spring 实现 IoC 容器提供的两大特性,相对于依赖查找,Spring 更推崇的是使用依赖注入,本篇先对 Spring 中依赖注入的几种方式进行介绍,后续再分享其实现。
205 0
Spring 依赖注入的方式,你了解哪些?