• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
xiaoyaovo
博客园    首页    新随笔    联系   管理    订阅  订阅
JavaEE 学习笔记 —— Spring Framework

spring framework runtime

在这里插入图片描述

核心容器

容器:servlet 容器、集合

线程池、常量池、数据库连接池的作用类似于容器
但是他们属于设计模式中的享元模式,即从池子中取东西,如果取不到,再创建

spring 中的核心容器,类似于集合

作用

  1. 存放对象
  2. 取出来使用

在 spring 中,有些对象是由容器来创建的,并且复制管理对象间的依赖

对于对象间的依赖:A 对象的属性为 B 对象,A 和 B 就产生了依赖关系

IOC

概念:IOC,(Inversion of Control,控制反转) —— 以前自己 new 对象以及组织对象间的关系,现在全部交给容器来统一管理,控制权发送了反转,因此称为控制反转。

实现方式:使用 IOC 容器来实现

在 spring 在,称为 spring bean 容器、spring 容器或 IoC 容器

DI

概念:DI, (Dependency Injection,依赖注入) —— 由 IoC 容器在运行期间,动态的将某种依赖关系注入到对象之中

注入
把 B 对象注入到 A ,称为 A 的属性
把 A 注入 IoC 容器中

IoC/DI 的作用

不使用:
在这里插入图片描述

使用:
在这里插入图片描述

作用
解耦

spring 容器使用流程

使用流程

  1. 配置 pom.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>spring01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring-framework.version>5.2.10.RELEASE</spring-framework.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 明确指定一些插件的版本,以免受到 maven 版本的影响 -->
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.3</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>

        </plugins>
    </build>
</project>

点击刷新,使得配置文件完成
在这里插入图片描述

  1. 在 resources 目录下创建 beans.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
        
<!--    org.example 代表扫描 org.example 包下的文件-->
    <context:component-scan base-package="org.example"/>

</beans>
  1. 启动 spring 容器
package org.example;

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

public class App {

    public static void main(String[] args) {
        //根据Spring配置文件路径创建容器:应用上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //关闭容器
        ((ClassPathXmlApplicationContext) context).close();
    }
}

理解注入流程

  1. 启动 spring 容器:new ApplicationContext 的实现类
  2. 注册 / 注入对象到容器中:加载指定的配置文件,执行包扫描,指定的包,子包下,带 spring 框架提供的四个注解,会注入到容器中
    @Controller (控制器)
    @Service(服务)
    @Repository(存储库)
    @Component (组件)
  3. 装配 / 注入依赖关系:如果注册到容器中的对象,属性使用一下两个注释
    @Autowired(自动装配)
    @Resource(资源)

注入代码讲解

1.使用 Controller 注解、Bean 注解 + 方法注入
bean 注解使用在方法上,该方法所在的类也需要注解

从容器中获取 Bean 对象的方法:

  • 通过类型获取(需要容器中只有一个对象)
  • 通过名称、id 获取
package org.example.controller;

import org.example.model.Duck;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;

// 使用 Controller 注解将该类注入到 Bean 容器中
@Controller
public class LoginController {
    
    // 方法前使用 Bean 注解, 也能够注入对象
    @Bean
    public Duck d1() {
        Duck duck = new Duck();
        duck.setName("唐老鸭");
        return duck;
    }


    @Bean
    public Duck d2() {
        Duck duck = new Duck();
        duck.setName("香酥鸭");
        return duck;
    }
}

  1. 使用 Repository 注解注入
package org.example.dao;

import org.springframework.stereotype.Repository;

@Repository
public class LoginRepository {
}

  1. 创建 Bean 注入的类
package org.example.model;


import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class Duck {
    private String name;
}

  1. 通过 Service 注解注入
package org.example.service;

import org.springframework.stereotype.Service;

@Service
public class LoginService {
	// 如果注入的对象不止一个 那么需要使用 @Qualifier 注解来确定使用的是哪个对象
    @Autowired
    @Qualifier("luYa")
    public Duck duck;
}

  1. @Scope 注解每次都能创建新对象
package org.example.service;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

// Scope 注解每次都能获取新的 Bean 对象
@Service
@Scope("prototype")
public class LoginServicePrototype {
}

  1. 检查注入结果
package org.example;

import org.example.controller.LoginController;
import org.example.dao.LoginRepository;
import org.example.model.Duck;
import org.example.service.LoginService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {


    public static void main(String[] args) {
        //根据Spring配置文件路径创建容器:应用上下文对象
        // 类加载路径下的 beans.xml 文件
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");


        // 1. 通过类型获取
        LoginRepository login1 = context.getBean(LoginRepository.class);

        // 2. 通过类名获取, 注意, 类名首字母小写
        LoginRepository login2 = (LoginRepository) context.getBean("loginRepository");

        System.out.println(login1);
        System.out.println(login2);


        // 通过类型获取
        LoginController login3 = context.getBean(LoginController.class);

        // 通过类名获取
        LoginController login4 = (LoginController) context.getBean("loginController");

        System.out.println(login3);
        System.out.println(login4);


        LoginService login5 = context.getBean(LoginService.class);

        // 通过类名获取
        LoginService login6 = (LoginService) context.getBean("loginService");

        System.out.println(login5);
        System.out.println(login6);


        Duck d1 = (Duck) context.getBean("d1");
        Duck d2 = (Duck) context.getBean("d2");

        System.out.println(d1);
        System.out.println(d2);

        // 同一类型注册了两个以上对象, 不能通过类型来获取
//        Duck d3 = context.getBean(Duck.class);
//        System.out.println(d3);
        //关闭容器
        ((ClassPathXmlApplicationContext) context).close();
    }
}




// 		  打印结果
//        org.example.dao.LoginRepository@76a4d6c
//        org.example.dao.LoginRepository@76a4d6c
//        org.example.controller.LoginController@517cd4b
//        org.example.controller.LoginController@517cd4b
//        org.example.service.LoginService@6cc7b4de
//        org.example.service.LoginService@6cc7b4de
//        Duck(name=唐老鸭)
//        Duck(name=香酥鸭)

BeanFactory 和 FactoryBean 的区别

  1. BeanFactory 是 Spring 的顶级接口
  2. FactroyBean 是工厂 Bean,本身是一个 Bean,且主要是生成 Bean 的工厂
    把 getObject() 方法的返回值注册到容器中
posted on 2021-10-22 19:46  豆本豆红枣豆奶  阅读(11)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3