SpringBoot官方API知识点整合2
一. Spring Boot 的使用
1. 1 构建系统
尽量选择一个支持依赖管理,能消费发布到“Maven中央仓库
”的artifacts的构建系统,比如Maven
或者Gradle
。
1.2 依赖管理
Spting Boot 每次发布都会提供一个他所支持的精选依赖列表。实际上,在构建配置里你不需要提供任何依赖版本。因为SpringBoot已经帮你管理好了。当你进行更新时,那些依赖也会一起更新。
如果需要,你可以指定依赖的版本来覆盖SpringBoot的默认版本
精选列表包括所有能够跟SpringBoot一起使用的Spring模块及第三方库,该列表可以在材料清单Spring-boot-dependencies
中获取到,也可以找到一些支持Maven和Gradle的资料。
Spring Boot 每次发布都关联一个Spring框架的基础版本,一般建议不要自己制定Spring版本
1.3 Maven相关
Maven用户可以在spring-boot-starter-parent
项目中继承合理的默认值,比如properties文件和yml文件、资源过滤等。
如果要配置项目中继承spring-boot-starter-parent
,可进行如下设置:
<! - 从Spring Boot继承默认值 - >
<parent>
<groupId> org.springframework.boot </ groupId>
<artifactId> spring-boot-starter-parent </ artifactId>
<version> 2.0.1.RELEASE < / version>
</ parent>
此时可以只需在此依赖上指定SpringBoot版本,如果导入其他的starters,可以省略版本号。
按照以上设置完了后,你可以在自己的项目中通过覆盖属性来覆盖个别的依赖。例如,要升级到另一个SpringData发行版本,需要将一下内容添加到pom.xml:
<pre class="programlisting"><span class="hl-tag"><properties> </span>
<span class="hl-tag"><spring-data- </span><span class="hl-tag">releasetrain.version></span> Fowler-SR2 <span class="hl-tag"></spring-data-releasetrain.version>
</span><span class="hl-tag"></ properties></span></pre>
可以检查Spring-boot-dependencies
pom 来获取支持的属性列表
1.4 在没有Parent Pom 的情况下使用Spring Boot
不是每个人都喜欢从spring-boot-starter-parent
POM 继承。您可能拥有自己的公司标准parent
,或者您可能更愿意明确声明所有Maven配置。
如果你不想使用它spring-boot-starter-parent
,你仍然可以通过依赖来保持依赖管理的好处(但不是插件管理) scope=import
,如下所示:
<dependencyManagement>
<dependencies>
<dependency>
<! - 从Spring Boot导入依赖项管理 - >
<groupId> org.springframework.boot </ groupId>
<artifactId> spring-boot-dependencies </ artifactId>
<version> 2.0 .1.RELEASE </ version>
<type> pom </ type>
<scope> import </ scope>
</ dependency>
</ dependencies>
</ dependencyManagement>
如上所述,上述示例设置不会让您使用属性重写个别依赖关系。要达到相同的结果,您需要在输入之前在dependencyManagement
项目中添加一个 条目。例如,要升级到另一个Spring Data发行版,您可以将以下元素添加到您的:spring-boot-dependenciespom.xml
<dependencyManagement>
<dependencies>
<! - 覆盖Spring Boot提供的数据发布训练 - >
<dependency>
<groupId> org.springframework.data </ groupId>
<artifactId> spring-data-releasetrain </ artifactId>
<版本> Fowler-SR2 </ version>
<type> pom </ type>
<scope> import </ scope>
</ dependency>
<dependency>
<groupId> org.springframework.boot </ groupId>
<artifactId> spring-boot -dependencies </ artifactId>
<version>2.0.1.RELEASE </ version>
<type> pom</ type>
<scope> import </ scope>
</ dependency>
</ dependencies>
</ dependencyManagement>
1.5 使用SpringBootMaven插件
SpringBoot包含一个Maven插件,可以将项目打包为可执行的jar。使用时徐江该插件添加到<plugins>
中:
<plugins>
<plugin>
<groupId> org.springframework.boot </ groupId>
<artifactId> spring-boot-maven-plugin </ artifactId>
</ plugin>
</ plugins>
</ build>
二、构建代码
SpringBoot对于代码结构没什么特殊要求,不过遵循以下的内容还是有一些帮助的。
1.使用“default”包
没有声明package
时,就被默认在default package
下,通常不推荐使用此包,因为对于使用部分注解的SpringBoot 的应用来说,它会扫描每个jar中的类,可能会造成某些速度问题。
应该使用Java推荐的包命名规范多数ide会自动生成
2.主要应用程序类
应将应用的main类放到所有类的最顶层,并在此类中加入@EnableAutoConfiguration
注解,这样就隐式的定义了一个包搜索路径,来对一些注解实体实现更加准确的搜索(比如@Service,@Component等)
例如,在这样应用后,Spring 将搜索@EnableAutoConfiguration
注解类下的@Entity
实体。
采用root package 方式,你就可以使用@ComponentScan
注解而不需要制定basePackage
属性,也可以使用@SpringBootApplication
注解,只要将main类放在root package中。
Application.java
将声明main方法,还有基本的@Configuration
。以下是较为标准的main类书写方式:
package com.example.myproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}