SpringBoot---概述

Overview

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

Spring Boot 让创建 独立、生产级别、基于Spring 应用 变得简单;

Features

1、Create stand-alone Spring applications

  创建独立的Spring应用;

2、Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)

  内嵌Tomcat、Jetty(无需部署war包)

3、Provide opinionated 'starter' dependencies to simplify your build configuration

  提供 starter,以简化构建应用(依赖管理);

4、Automatically configure Spring and 3rd party libraries whenever possible

  尽可能 自动配置Spring与第三方库;

5、Provide production-ready features such as metrics, health checks, and externalized configuration

  提供 生产级别的特色:健康检测、远程配置...

6、Absolutely no code generation and no requirement for XML configuration

  没有额外的代码生成、无需xml配置;

微服务

依赖管理

父依赖

1
2
3
4
5
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
    </parent>

  

父依赖内容

 

父依赖的父依赖

1
2
3
4
5
6
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.2.2.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
  </parent>

 

 

 

将所有常用的依赖统一进行管理  

自定义依赖管理

1、查看spring-boot-dependencies的当前规定的依赖版本 用的key

2、在当前pom中重写<properties>

 

starter

https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.build-systems.starters

Starters are a set of convenient dependency descriptors that you can include in your application.

Starters是一组 便利的依赖,可以在应用中使用;

The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.

Starters包含大量 应用需要的依赖;

官方starter

All official starters follow a similar naming pattern; spring-boot-starter-*, where * is a particular type of application. 

所有官方的starters命名规则:spring-boot-starter-*

自定义starter

As explained in the “Creating Your Own Starter” section, third party starters should not start with spring-boot, as it is reserved for official Spring Boot artifacts. 

第三方自定义starter,不允许以spring-boot开头;

Rather, a third-party starter typically starts with the name of the project.For example, a third-party starter project called thirdpartyproject would typically be named thirdpartyproject-spring-boot-starter.

第三方starter可以用项目名称开头;比如 *-spring-boot-starter;

注意

所有的starter最终都会依赖

1
2
3
4
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>  

 

自动配置

各种配置拥有默认值

默认配置最终都映射到MultipartProperties;

配置文件的值,最终都会绑定到某个类,这个类会在容器中创建对象;

按需加载所有自动配置

引入哪个才会开启哪个的自动配置;

SpringBoot的所有的自动配置功能都在spring-boot-autoconfigure包中;

 

容器功能

组件添加

xml

<bean></bean>

@Component、@Controller、@Service、@Repository

@Configuration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * 1、配置类使用@Bean添加Bean,默认是单例;
 * 2、配置类本身也是Bean;
 * 3、@Configuration的proxyBeanMethods:
 *      3.1、Full:
 *          proxyBeanMethods = true;
 *          保证每个@Bean的方法被调用N次,返回的Bean都是同一个单例Bean;
 *      3.2、Lite:
 *          proxyBeanMethods = false;
 *          每次调用@Bean的方法,都会创建一个新的Bean;
 *
 *      【注意】
 *          Bean之间相互依赖,必须使用Full
 */
@Configuration(proxyBeanMethods = true) // Config.java 等价于 xx.xml
public class Config {
 
    @Bean // 给容器添加Bean (默认方法名为Bean的id、返回值类型即Bean类型)
    public User user(){
        return new User();
    }
 
}

@CompomentScan

指定Bean扫描路径

@Import

给容器中创建指定类型的Bean;

默认的Bean名称:全类名;

@Conditional

条件添加Bean;

在类上,满足条件,类中的内容才生效;

在方法上,满足条件,方法才生效;

 

导入配置文件

@ImportResource

加载指定资源文件

配置类使用;

@ImportResource(value = "bean.xml")

配置绑定 

如何使用Java读取properties文件中的内容,并将其转换为JavaBean;

@Component+@ConfigurationProperties

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * 只有容器中的Bean,才能拥有SpringBoot的强大功能
 */
@Component
@ConfigurationProperties(prefix = "man")
public class Man {
 
    private String name;
    private int age;
 
    get,set...
 
}

    

applcation.xml

1
2
3
4
5
6
...
 
man.name=rose
man.age=13
 
...

  

@EnableConfigurationProperties+@ConfigurationProperties

1
2
3
4
5
6
7
8
@ConfigurationProperties(prefix = "man")
public class Man {
 
    private String name;
    private int age;
    get,set...
 
}

 

Config.java

1
2
3
4
@EnableConfigurationProperties(value = Man.class) // 1、开启指定Bean的配置绑定 2、将指定Bean注入容器中
public class Config {
    ...
}

  

applcation.xml

1
2
man.name=rose
man.age=13

  

  

 

posted on   anpeiyong  阅读(139)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示