关于SpringBoot你需要了解这些

写在前面

springBoot官网

官网上是这么去描述springboot以及总结springboot特点的

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

We take an opinionated view of the Spring platform and third-party
libraries so you can get started with minimum fuss. Most Spring Boot
applications need minimal Spring configuration.

springBoot可以轻松的创建一个独立的,生产级别的,基于spring应用的,你可以立即运行的程序。

对于这句话的理解对于ssm的老开发,感受会非常深,因为需要配置一系列bean,比如,使用springmvc,我们需要配置dispatcherServlet,handlermapping,hadlerAdapter,viewResolver。使用到数据库,需要配置dataSource,JdbcTemplate,使用mybatis,需要配置SqlSessionFactoryBean会话工厂。。
我们对Spring平台和第三方库持有一种有立场的观点,这样您就可以尽可能轻松地开始。大多数Spring Boot应用程序只需要最少的Spring配置。

springBoot特点
  1. 创建一个独立的spring应用
  2. 直接内嵌了tomcat,jetty or Undertow (不需要不是war)
  3. 提供约定好的starter依赖项,去简化构建配置
  4. 尽可能自动配置spring及第三方依赖包
  5. 提供生产就绪的功能,例如指标、运行状况检查和外部化配置
  6. 完全不需要生成代码,也不需要 XML 配置

spring核心流程简图

流程简图

  1. SpringBoot通过**@EnableAutoConfiguration注解开启自动配置**,加载spring.factories中注册的各种AutoConfiguration类
  2. 当某个AutoConfiguration类满足其注解@Conditional指定的生效条件时,实例化该AutoConfiguration类中定义的Bean(组件等),并注入Spring容器/Springboot容器里,就可以完成依赖框架的自动配置。

Springboot常用注解

@SpringBootApplication 启动类上的注解,他是一个复合注解,下面主要介绍三个注解

  • @SpringBootConfiguration
    • 这个注解实际就是一个@Configuration,表示启动类也是一个配置类
  • @ComponentScan
    • 表示扫描路径,因为默认是没有配置实际扫描路径的,所以SpringBoot扫描的路径是启动类所在的包以及子包
  • @EnableAutoConfiguration
    • @EnableAutoConfiguration的关键功能是通过**@Import注解导入的AutoConfigurationImportSelector** 来完成从spring.factories 中加载配置类。

springBoot自动装配原理

  • 通过**@EnableAutoConfiguration** 的 @Import注解导入的AutoConfigurationImportSelector 扫描所有包的META-INF目录下的来完成从spring.factories 中加载配置类到一个String数组中。
  • 交给Spring, Spring会将它们封装成BeanDefinition,放到BeanDefinitionMap中去,最后Spring就能管理到这些Bean

手写如何自定义starter

创建配置类,@Configuration配置类,还可以添加一些@ConditionOnXXX的注解控制配置的生效条件,结合XXXProperties 获取配置信息 等。

@Configuration
@EnableConfigurationProperties(UserProperties.class)
@ConditionalOnClass(ChinService.class)
public class LisAutoConfiguration {

    @Autowired
    public UserProperties userProperties;
    @Bean
    @ConditionalOnMissingBean(name = "chinService")
    public ChinService chinService(){
        return new ChinService(userProperties);
    }
}

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.hbwxz.learnspringboot.autoConfigure.LisAutoConfiguration

创建UserProperties读取配置文件

package com.hbwxz.learnspringboot.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "ls")
public class UserProperties {
    private  String username;
    private Integer age;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

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

创建服务类,也就是我们真正应用需要注入的bean

package com.hbwxz.learnspringboot.service;

import com.hbwxz.learnspringboot.config.UserProperties;

public class ChinService {

    private UserProperties userProperties;

    public ChinService(UserProperties userProperties) {
        this.userProperties = userProperties;
    }

    public void getUserProperties() {
        System.out.println(userProperties.getAge());
    }


    public UserProperties getUser() {
        return this.userProperties;
    }

}

在另一个项目中引用这个starter依赖并测试

配置文件

ls:
  username: aka
  age: 10

测试类


@SpringBootTest
@RunWith(SpringRunner.class)
public class testStarter {

    @Autowired
    ChinService chinService;

    @Test
    public void test1(){
        chinService.getUserProperties();
    }
}

控制台

2024-03-26 17:17:27.703  INFO 24256 --- [           main] com.test.starter.testStarter             : Starting testStarter using Java 1.8.0_221 on DESKTOP-C8H6MAH with PID 24256 (started by admin in D:\code\springboot\testStarter)
2024-03-26 17:17:27.704  INFO 24256 --- [           main] com.test.starter.testStarter             : No active profile set, falling back to 1 default profile: "default"
2024-03-26 17:17:28.936  INFO 24256 --- [           main] com.test.starter.testStarter             : Started testStarter in 1.566 seconds (JVM running for 2.313)
10

SpringBoot是如何启动tomcat的

  • ⾸先SpringBoot在启动时会先创建⼀个Spring容器
  • 再创建容器的onRefresh方法中会创建webServer
  • 通过new Tomcat对象,绑定端口及协议,然后去启动tomcat服务器
  • 25
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值