首页 > 其他分享 >关于SpringBoot你需要了解这些

关于SpringBoot你需要了解这些

时间:2024-03-26 18:04:12浏览次数:19  
标签:userProperties SpringBoot Spring 配置 了解 关于 spring 注解 public

文章目录

写在前面

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服务器

标签:userProperties,SpringBoot,Spring,配置,了解,关于,spring,注解,public
From: https://blog.csdn.net/qq_51059003/article/details/137052836

相关文章

  • SpringBoot基础24_SpringBoot3原理分析
    一、起步依赖原理分析1、分析spring-boot-starter-parent按住Ctrl点击pom.xml中的spring-boot-starter-parent,跳转到了spring-boot-starter-parent的pom.xml,xml配置如下(只摘抄了部分重点配置):<parent><groupId>org.springframework.boot</groupId><artifactId>spr......
  • 关于WPF进度条的使用
    本文讲述如何在软件启动和窗体按钮操作时弹出进度条。运行环境:Win10、VS2022一、新建WPF项目。 二、新建WPF窗体。1、新建窗体,取名DefProcessBar.xaml。 2、设置窗体属性、样式。<Windowx:Class="WpfApp4.DefProcessBar"xmlns="http://schemas.microsoft.c......
  • 【一】了解计算机的原理以及Python
    【一】编程和编程语言是什么【1】什么是编程编程其实就是将人类的语言转换为计算机能识别的语言【2】什么是编程语言人和人之间都会进行交流人和人之间交流的媒介就是语言编程语言就是人与计算机之间进行交流的语言【3】为什么要出现编程语言最根本的原因就是计算机......
  • java毕业设计小说阅读网(springboot+mysql+jdk1.8+meven)
    本系统(程序+源码)带文档lw万字以上 文末可领取本课题的JAVA源码参考系统程序文件列表系统的选题背景和意义选题背景:随着互联网技术的飞速发展,人们的阅读习惯也在逐渐改变。在线阅读逐渐成为人们获取文学作品的主要方式之一。小说阅读网站作为提供在线阅读服务的平台,因其......
  • java毕业设计小区物业管理系统(springboot+mysql+jdk1.8+meven)
    本系统(程序+源码)带文档lw万字以上 文末可领取本课题的JAVA源码参考系统程序文件列表系统的选题背景和意义选题背景:随着城市化进程的加快,现代小区物业管理逐渐呈现出规模化、复杂化的特点。高效、规范的物业管理服务对于提升居民生活品质和社区环境至关重要。传统的物业......
  • 【SpringBoot3+Mybatis】小程序和后台管理系统 员工/分类/菜品/套餐管理 上传文件 CRU
    文章目录一、项目介绍&Github二、技术选型三、开发环境搭建四、员工管理4.1新增员工①sql②对象拷贝DTO与Entity③异常捕获与处理④动态获取当前登录者Id⑤ThreadLocal4.2员工分页查询①请求参数实体与响应数据实体②controller层③service层使用pageHelper......
  • 【CKA模拟题】Ingress新手必看,全面了解Ingress的基础操作
    题干Forthisquestion,pleasesetthiscontext(Inexam,diffclustername)kubectlconfiguse-contextkubernetes-admin@kubernetesThereexistsadeploymentnamednginx-deploymentexposedthroughaservicecallednginx-service.Createaningressres......
  • IT部门都想要的跨网文件交换解决方案,了解一下!
    近年来全球网络安全威胁态势的加速严峻,使得企业对于网络安全有了前所未有的关注高度,企业的网络安全体系建设正从“以合规为导向”转变到“以风险为导向”,从原来的“保护安全边界”转换到“保护核心数据资产”的思路上来。为了保护企业的核心数据资产,绝大多数企业都在内部实施了内......
  • SpringBoot手动取消接口执行方案
    实际开发中经常会遇到比较耗时的接口操作,但页面强制刷新或主动取消接口调用后后台还是会继续运行,特别是有大量数据库操作时会增加服务器压力,所以进行研究测试后总结了一套主动取消接口调用的解决方案自定义注解用于标记耗时接口@Retention(RetentionPolicy.RUNTIME)@Target({El......
  • SpringBoot基础24_SpringBoot简介1
    一、原有Spring优缺点分析1、Spring的优点分析Spring是Java企业版(JavaEnterpriseEdition,JEE,也称J2EE)的轻量级代替品。无需开发重量级的EnterpriseJavaBean(EJB),Spring为企业级Java开发提供了一种相对简单的方法,通过依赖注入和面向切面编程,用简单的Java对象(PlainOldJava......