本学习笔记基于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