Spring Boot学习 基于2.2.4版本

这篇博客介绍了Spring Boot 2.2.4的学习要点,包括@EnableAutoConfiguration的自动配置原理,spring-boot-starter-parent父工程的特性,如修改依赖版本的方式。此外,详细讲解了@SpringBootApplication的作用,自动配置的启用与禁用,以及如何通过exclude属性控制特定自动配置。还提到了Spring Boot的打包插件、启动器的命名规范以及热部署的配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本学习笔记基于Spring官网。

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Example {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(Example.class, args);
    }

}
  • @EnableAutoConfiguration 告诉Spring Boot通过你导入的jar包依赖去推测你想怎么配置Spring。因为spring-boot-starter-web添加了Tomcat和Spring MVC,自动配置假定你在开发一个网页应用并据此设置Spring。
  • Each release of Spring Boot is associated with a base version of the Spring Framework. We highly recommend that you not specify its version.(Spring Boot官方不建议修改依赖版本。)
  • spring-boot-starter-parent 父工程的特点:
Java 1.8 as the default compiler level.

UTF-8 source encoding.

A Dependency Management section, inherited from the spring-boot-dependencies pom, that manages the versions of common dependencies. This dependency management lets you omit <version> tags for those dependencies when used in your own pom.

An execution of the repackage goal with a repackage execution id.

Sensible resource filtering.

Sensible plugin configuration (exec plugin, Git commit ID, and shade).

Sensible resource filtering for application.properties and application.yml including profile-specific files (for example, application-dev.properties and application-dev.yml)
  • Note that, since the application.properties and application.yml files accept Spring style placeholders (${…​}), the Maven filtering is changed to use @…@ placeholders. (You can override that by setting a Maven property called resource.delimiter.)修改了占位符。
  • 如果需要修改依赖版本直接重写版本属性即可。
<properties>
    <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>
  • 不使用Spring Boot 父工程配置仅导入相关依赖则可以通过设置作用域实现。
<dependencyManagement>
    <dependencies>
            <!-- Override Spring Data release train provided by Spring Boot
            在前面加上这个元素就可以修改依赖的版本号 -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Fowler-SR2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <!-- Import dependency management from Spring Boot 
            导入Spring Boot的相关依赖,版本号已默认。-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.2.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
  • Spring Boot includes a Maven plugin that can package the project as an executable jar. Add the plugin to your section if you want to use it, as shown in the following example: 本身有打包插件。
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
  • Starters are a set of convenient dependency descriptors that you can include in your application.启动器其实就是一组依赖的描述符,简化了依赖的配置。
  • 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. 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.创建自己的启动器建议命名方式。
  • If you don’t want to use @SpringBootApplication, the @EnableAutoConfiguration and @ComponentScan annotations that it imports defines that behaviour so you can also use those instead. @SpringBootApplication导入了@EnableAutoConfiguration和@ComponentScan。
  • Spring Boot favors Java-based configuration. Although it is possible to use SpringApplication with XML sources, we generally recommend that your primary source be a single @Configuration class. Usually the class that defines the main method is a good candidate as the primary @Configuration.建议使用JAVA注解配置。可以通过搜索Enable*注解来找到XML配置的等效替换。
  • You need not put all your @Configuration into a single class. The @Import annotation can be used to import additional configuration classes. Alternatively, you can use @ComponentScan to automatically pick up all Spring components, including @Configuration classes.既可以导入配置,也可以扫描配置,@Configuration注解的底层就有@Component。
  • If you absolutely must use XML based configuration, we recommend that you still start with a @Configuration class. You can then use an @ImportResource annotation to load XML configuration files.一个主类。
  • Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added. Spring Boot通过导入的依赖自动配置。You need to opt-in to auto-configuration by adding the @EnableAutoConfiguration or @SpringBootApplication annotations to one of your @Configuration classes.在配置类上选择添加一个带有自动配置的注解且建议只添加一个带有自动配置的注解。自动配置是非入侵的,可以随时替换掉默认配置。
  • If you need to find out what auto-configuration is currently being applied, and why, start your application with the --debug switch. Doing so enables debug logs for a selection of core loggers and logs a conditions report to the console.通过调试找到应用的自动配置。
  • 使用exclude属性禁用特定的自动配置。
import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;

@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}
  • If the class is not on the classpath, you can use the excludeName attribute of the annotation and specify the fully qualified name instead. Finally, you can also control the list of auto-configuration classes to exclude by using the spring.autoconfigure.exclude property. 属性值一个是类,一个是全限定类名,既可以在注解上定义,也可以在配置文件中定义。建议使用禁用特定自动配置的公共API,其他公共API不建议直接使用。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

    Class<?>[] exclude() default {};

    String[] excludeName() default {};
}
  • If you structure your code as suggested above (locating your application class in a root package), you can add @ComponentScan without any arguments. All of your application components (@Component, @Service, @Repository, @Controller etc.) are automatically registered as Spring Beans. 如果应用类放在根包下,则同目录下的所有组件都会自动注册。
  • A single @SpringBootApplication annotation can be used to enable those three features, that is: 这个注解相当于以下三个注解放在一起
@EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism

@ComponentScan: enable @Component scan on the package where the application is located (see the best practices)

@Configuration: allow to register extra beans in the context or import additional configuration classes
  • In this example, Application is just like any other Spring Boot application except that @Component-annotated classes and @ConfigurationProperties-annotated classes are not detected automatically and the user-defined beans are imported explicitly (see @Import). 不使用注解扫描和配置属性扫描的一个例子。
package com.example.myapplication;

import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration
@Import({ MyConfig.class, MyAnotherConfig.class })
public class Application {

    public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
    }

}
  • While caching is very beneficial in production, it can be counter-productive during development, preventing you from seeing the changes you just made in your application. For this reason, spring-boot-devtools disables the caching options by default. Spring Boot热部署工具默认关闭了缓存选项。If you don’t want property defaults to be applied you can set spring.devtools.add-properties to false in your application.properties. 如果不想使用默认属性配置关闭即可。

  • Executable jars can be used for production deployment. As they are self-contained, they are also ideally suited for cloud-based deployment. 每一个jar包都能独立运行,方便进行云部署。

  • 配置Spring Boot 热部署
    在这里插入图片描述
    按 Ctrl + Alt + Shift + / 选择Registry
    在这里插入图片描述
    在这里插入图片描述

在遇到Spring Boot 2.2.4版本导入org.springframework.web包的问题时,可能是因为版本兼容性或仓库问题导致的依赖无法正确解析。首先,你需要确认IDE(如IntelliJ IDEA)是否配置正确,Maven或Gradle是否能够正常访问远程仓库。如果这些配置无误,但问题依旧存在,那么可以尝试以下几个步骤来解决问题: 参考资源链接:[SpringBoot 2.2.4版本导入不了org.springframework.web包](https://wenku.csdn.net/doc/6412b793be7fbd1778d4ac8d?spm=1055.2569.3001.10343) 1. 清理并更新Maven依赖:在IDE中,可以执行清理和更新操作,或者在项目的根目录下,使用Maven命令行工具执行`mvn clean install`,确保所有的依赖都是最新且正确下载的。 2. 手动下载和替换jar包:如果自动解析依赖失败,你可以手动从Maven中央仓库或其他可信赖的仓库下载相应的jar包,并替换到本地Maven仓库中对应的位置。 3. 更改依赖版本:如果上述方法都不能解决问题,可能是因为Spring Boot 2.2.4与某些库的特定版本不兼容。此时可以尝试更改Spring Boot版本或者更改相关依赖的版本号,查看是否有改善。比如,将Spring Boot降级到2.1.6或1.5.9版本4. 检查IDE设置:有时候IDE缓存问题会导致类似的问题,尝试重建IDE的索引,或者重启IDE。 5. 查看构建日志:仔细查看构建过程中的错误日志,通常可以找到问题的线索。 在解决这类问题时,一个实用的资源是《SpringBoot 2.2.4版本导入不了org.springframework.web包》,这份资料详细描述了问题发生的背景和解决步骤,可以帮助你更深入地理解问题所在,并提供了解决问题的直接方法。通过阅读这份资料,你不仅能够解决当前的依赖导入问题,还能学习到如何应对未来可能遇到的类似问题。 参考资源链接:[SpringBoot 2.2.4版本导入不了org.springframework.web包](https://wenku.csdn.net/doc/6412b793be7fbd1778d4ac8d?spm=1055.2569.3001.10343)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值