pom.xml设置
springboot 父级依赖,spring-boot-starter-parent是一个特殊的starter,它用来提供相关的Maven默认依赖,使用它之后,常用的包依赖可以省去version标签。
如果你不想使用某个依赖默认的版本,您还可以通过覆盖自己的项目中的属性来覆盖各个依赖项,例如,要升级到另一个Spring Data版本系列,您可以将以下内容添加到pom.xml中。
<properties> <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>< /properties>
如果你不想使用spring-boot-starter-parent,您仍然可以通过使用scope = import依赖关系来保持依赖关系管理:
| |
该设置不允许您使用如上所述的属性(properties)覆盖各个依赖项,要实现相同的结果,您需要在spring-boot-dependencies项之前的项目的dependencyManagement中添加一个配置,例如,要升级到另一个Spring Data版本系列,您可以将以下内容添加到pom.xml中。
<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>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
注解
controller层的注解为@RestController:该注解是@Controller和@ResponseBody注解的合体版,使用这个注解的类里面的方法都以json格式输出。
spring boot内置了Tomcat,不用手动的去配置Tomcat服务器。
@SpringBootApplication是Sprnig Boot项目的核心注解,主要目的是开启自动配置。它是一个组合注解,该注解组合了:@Configuration、@EnableAutoConfiguration、@ComponentScan; 若不是用 @SpringBootApplication 注解也可以使用这三个注解代替。
- 其中,@EnableAutoConfiguration 让 Spring Boot 根据类路径中的 jar 包依赖为当前项目进行自动配置,例如,添加了 spring-boot-starter-web 依赖,会自动添加 Tomcat 和 Spring MVC 的依赖,那么 Spring Boot 会对 Tomcat 和 Spring MVC 进行自动配置。
- Spring Boot 还会自动扫描 @SpringBootApplication 所在类的同级包以及下级包里的 Bean ,所以入口类建议就配置在 grounpID + arctifactID 组合的包名下(这里为 cn.wmyskxz.springboot 包)