Spring Boot相比于Spring需要大量的配置而言,所有配置都是默认的,需要调整时再去定义,同时依赖也进行对应整合,同时内置容器,可用jar包直接启动服务。
Spring Boot主要是由maven配置的,以下为maven配置项
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 项目配置信息 -->
<groupId>com.sfz</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.0-SNAPSHOT</version>
<!--
<packaging>pom</packaging>
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- 指定使用 Spring Boot 2.3.1.RELEASE 版本 -->
<version>2.3.1.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<!-- 打包插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Spring Boot需要一个启动类Applicatoin.class
@SpringBootApplication
public class Applicatoin {
public static void main(String[] args) {
SpringApplication.run(Applicatoin.class,args);
}
这个类关键在于注解@SpringBootApplication,这个类里面含有多个注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
...
}
@SpringBootConfiguration,继承了@Configuration,将当前类标注为配置类。
@ComponentScan,通过扫描包发现 Bean 。默认扫描当前包及其子包,在其它包下需要指定。
@EnableAutoConfiguration,开启自动配置, 用来扫描配置类,根据配置启动 Spring 容器。
网友评论