springboot中需要知道的一些配置

springboot可以快速的搭建一个web项目,我们来看看是如何做到的

首先我们查看pom.xml

1
2
3
4
5
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
parent标签用来表示当前我们的项目是继承于spring-boot-starter-partent这个项目,这是springboot提供的一个springboot的一个依赖,让我们才能够快速的开发
spring-boot-starter-partent同时又继承spring-boot-dependencies
在spring-boot-dependencies中通过标签dependencyManagement为我们管理了大量的依赖以及版本信息.
当我们要用到一些包时在pom.xml中就只需要标明组id和项目名,而不需要表明版本号
再看spring-boot-starter-web包
1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

点进去可以看到里面管理了spring-mvc,json,tomcat等相关的包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.5.0</version>
<scope>compile</scope>
</dependency>
<!--json相关包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.5.0</version>
<scope>compile</scope>
</dependency>
<!--tomcat相关包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.5.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.7</version>
<scope>compile</scope>
</dependency>
<!--sprinmvc相关包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.7</version>
<scope>compile</scope>
</dependency>
</dependencies>

再来看看启动类

1
2
3
4
5
6
7
8
9
@SpringBootApplication
//@PropertySource("classpath:aa.properties")
//@ImportResource("classpath:application-context.xml")
public class AppStart {

public static void main(String[] args) {
SpringApplication.run(AppStart.class,args);
}
}
方法说明:程序启动类
@SpringBootApplication点进去我们可以看到这是一个注释的组合包,里面包含了三个重要的注释
1
2
3
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
@SpringBootConfiguration:这个注解用于声明被注解的类是springboot的注解类
@EnableAutoConfiguration:这个注解功能为自动配置注解,这也是使用springboot能快速开发的原因
@ComponentScan:这个注解会扫描被注解类的包及其子包中的注解并使其生效

Comments: