【JavaEE进阶2】:spring的创建和使用

文章介绍了如何创建Spring项目,包括创建Maven项目,添加Spring支持,编写启动类。然后详细阐述了如何将bean对象存入Spring容器,如定义配置文件并将bean声明其中。接着讨论了从Spring容器中读取Bean的三种方式,包括按名称、类型或名称加类型获取。最后提到了ApplicationContext和BeanFactory的区别,以及如何使用获取到的bean对象。
摘要由CSDN通过智能技术生成

spring创建和使用的目标:

  1. spring 项目创建
  2. 将bean存储到spring(容器)
  3. 从spring中获取bean【DI】

1. 创建Spring项目(core)

1. 创建一个普通的maven项目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
出现这两部分,说明maven项目初始化好了。

2. 添加spring框架支持

(spring-context、spring-beans)。

首先配置maven国内源

在maven仓库中查找spring

    <dependencies>
    
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.26</version>
        </dependency>
        
    </dependencies>

3. 添加启动类

为后面从spring中获取bean对象做准备
在这里插入图片描述

public class App {
    public static void main(String[] args) {
    }
}

2. 将bean对象存储到spring容器中

这里的存储是声明式的,真正的存储在第三大步的第1或第2步

  1. 创建一个bean。
    什么是bean?
    在Java中,一个对象如果被使用多次,就可以称为bean
public class Student {
    public Student() {
        System.out.println("init student");
    }
    public void sayHi(){
        System.out.println("hi student");
    }
}
  1. 将创建的bean存储到spring容器中
    在resources下创建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"
       xmlns:content="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--    <content:component-scan base-package="com.bit.service"></content:component-scan>-->
</beans>
<!--    将 bean(com.spring.demo.Student)存储到spring容器,它的名称叫做student-->
<bean id="student" class="com.spring.demo.Student"></bean>

这里的id是自定义的
在这里插入图片描述

3. 从spring容器中读取到Bean (DI)

整体逻辑:

package org.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        // 1.得到spring
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");

        // 2.从spring容器中获取bean对象
        Student student = (Student) context.getBean("student");  // student 是 xml 中的 id

        // 3.使用bean
        student.sayHi();
    }
}

1. 创建spring上下文(得到spring对象)

  1. 得到spring(上下文)对象 (在启动类上)
    Spring 上下文对象可使用 ApplicationContext:
    这里的名称必须和resources中的文件名相同
// 1.得到 Spring 的上下⽂对象,创建的时候需要配置 Spring 配置信息
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
  • ApplicationContext:将xml中所有的对象存储到spring容器中,即一次性加载并初始化所有的bean对象。
    在new ApplicationContext上面这一步时就已经初始化了
    特征:比较费内存,但是一次性加载后,之后的读取会非常的快
  • BeanFactory :除了 ApplicationContext 之外,我们还可以使用 BeanFactory 来作为 Spring 的上下文。
    BeanFactory :lazy load,需要哪个才去加载。【不建议使用了】
    特征:节省内存,但调用时才加载初始化 bean 到 spring 中,效率不高。
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));

ApplicationContext 和 BeanFactory的区别(常见面试题)

  • 继承关系和功能方面来说:Spring 容器有两个顶级的接口:BeanFactoryApplicationContext
    • BeanFactory 提供了基础的访问容器的能力。【功能少】
    • ApplicationContext属于 BeanFactory 的子类,它除了继承了 BeanFactory 的所有功能之外,它还拥有独特的特性,还添加了对国际化支持、资源访问支持、以及事件传播等方面的支持。【功能多】
  • 从性能方面来说:
    • ApplicationContext 是一次性加载并初始化所有的 Bean 对象。
    • BeanFactory 是需要那个才去加载那个,因此更加轻量。

2. getBean方法获取bean对象

  1. 从spring容器中获取到bean对象
    这里的名称必须和spring-config.xml的id名称一致
Student student1 = (Student) context.getBean("student");

获取bean的3种方式

  1. 通过名称获取(bean的id)
Student student1 = (Student) context.getBean("student");
  1. 通过类型方式获取
Student student1 = context.getBean(Student.class);

虽然简洁,但如果spring中一个类型存储了多个实例,那么使用类型获取bean就会报错:
在这里插入图片描述

Exception in thread “main” org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type ‘org.example.Student’ available: expected single matching bean but found 2: student,stu

  1. 根据类型和名称一起获取【推荐】
Student student1 = context.getBean("student", Student.class);  //根据名称+类型获取对象

spring中一个类型存储的多个实例是不相同的,不是一个对象,是两个实例对象

<bean id="student" class="com.spring.demo.Student"></bean>
<bean id="stu" class="com.spring.demo.Student"></bean>
Student stu = context.getBean("stu", Student.class);  //根据名称+类型获取对象
Student student1 = context.getBean("student", Student.class);  //根据名称+类型获取对象

System.out.println("stu == student1 : " + (stu == student1));  //false

3. 使用bean对象(非必须)

student1.sayHi();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值