美文网首页
《SpringBoot+Vue》Chapter01_Spring

《SpringBoot+Vue》Chapter01_Spring

作者: So_ProbuING | 来源:发表于2023-11-27 17:14 被阅读0次

SpringBoot的介绍

简单来说,SpringBoot就是Spring提供的用于Web开发的脚手架框架。配置简单、上手快速

SpringBoot的特性

  • 自带tomcat、Jetty服务器可以部署war包
  • 自动配置Spring框架和第三方框架
  • 能够提供应用的健康监控和配置的监控
  • 没有代码生成,并且尽可能的减少了xml等的配置

SpringBoot的创建

在线创建

通过 start.spring.io 来创建


springboot在线创建

通过idea创建

创建maven工程项目

添加maven依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

添加启动类

@EnableAutoConfiguration
@RestController
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}

SpringBoot的相关知识点分析

@SpringBootApplication

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM,
                classes = AutoConfigurationExcludeFilter.class) })

@Configuration

SpringBoot加载时会加载很多的后置处理器,各种后置处理器完成各种处理功能。其中有一个后置处理器ConfigurationClassPostProcessor。这个后置处理器就是专门负责处理带@Configuration的类。所以在SpringBoot加载的时候加载到所有带有@Configuration标记的类,在类中使用了一个代理类来增强了被@Configuration标记的类。原来的类会被增强的类替换。被代理的类执行的时候会执行一个BeanMethodInterceptor拦截器,在拦截器中会拦截加了@Bean的方法,然后会判断@Bean方法的Bean是否被创建。如果创建了就会直接返回。没有Bean就会创建。所以@Configuration标记的类创建的对象是同一个对象。
如果没有使用@Configuration,使用的是@Component,那么这个类就没有被增强没有被代理,那这个类就是普通的类,就会和普通类一样创建对象,创建的就不一定是同一个对象

SpringBoot的Parent

SpringBoot的Parent完成了以下的功能,使得我们的项目继承SpringBootParent后可以直接省略这些步骤

  1. 定义Java的编译版本
  2. 定义项目编码格式
  3. 定义依赖的版本号
  4. 项目打包的配置
  5. 资源文件的过滤

SpringBootParent使用的两种方式

  • 第一种 直接继承SpringBootParent
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>
  • 第二种在项目中使用SpringBootDependencies
    当我们的项目不方便继承SpringBootParent或者我们需要继承自己的Parent的时候,我们可以使用SpringBootDependencies
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

相关文章

网友评论

      本文标题:《SpringBoot+Vue》Chapter01_Spring

      本文链接:https://www.haomeiwen.com/subject/rszzwdtx.html