创建Spring Boot项目时,关于依赖库我查到了一些资料
Gradle 是什么?
基於Apache Ant和Apache Maven的概念的開源建構自動化系統。 參考: Qiita 上的Gradle入門。
我在Spring Boot项目中使用Gradle时遇到了一些困难,关于这些问题进行了描述。
当使用Gradle时,简单地创建并构建所需的文件,它会自动为项目组装所需的文件和源代码的工具。
关于依赖库
在中文中修改以上内容可能如下:
使用gradle命令来构建Gradle项目。该gradle命令将引用当前目录的build.gradle文件来进行构建。
在build.gradle文件中,有一个声明库以供Spring项目使用的区域。
依赖关系可以在dependencies块内定义。
有关Gradle依赖关系的编写方法。
dependencyManagement {
dependencies {
compile group:'org.springframework', name:'spring-core', version:'4.0.3.RELEASE'
}
}
另外,还可以使用快捷方式来达到以下效果。
dependencyManagement {
dependencies {
compile 'org.springframework:spring-core:4.0.3.RELEASE'
}
}
无论哪种语法,通过这种结构,所有的依赖关系(直接或间接)都可以具有spring-core的版本。(由于compile配置的依赖关系会传播到子孙依赖关系,所以目前不建议使用。)
这次我遇到了困难的地方。
由于对依赖库的内容不太了解,所以在调查它时遇到了困难。因此,以下是以条目形式进行记录。
implementation 'org.springframework.boot:spring-boot-starter-web'
如果您要创建Web应用程序,则可以使用spring-boot-starter-web模块。
这是一个用于使用Spring MVC构建包含RESTful应用程序的Web的启动器。
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
使用Spring Data JPA的起始器。Spring使用JPA来操作数据库,而这个起始器提供了从Spring Boot使用JPA的功能。如果需要使用数据库,则是必需的。
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation "org.thymeleaf:thymeleaf-spring4:3.0.11.RELEASE"
implementation "org.thymeleaf:thymeleaf:3.0.11.RELEASE"
设置模板引擎
编写使用Thymeleaf视图的MVC Web应用程序的起始器
下面两行代码在spring-boot-starter-thymeleaf的目录下,由于需要更改版本,所以进行了记录。
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
自动资源管理、getter、setter、equals、hashCode和toString的自动生成。
developmentOnly 'org.springframework.boot:spring-boot-devtools'
当类路径发生变化时,会自动重新启动(热重载功能)。默认情况下,保存更改后,这会在Eclipse中自动进行。
runtimeOnly 'com.h2database:h2'
H2是一個Java製的數據庫庫,可以用作嵌入式和服務器。
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter'
MyBatis的起始器
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
从带有注释的项目中,可以轻松生成独特的构建元数据文件(在Gradle 4.6及更高版本中,使用annotationProcessor声明)。
]providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
用于将Tomcat作为嵌入式Servlet容器使用的启动器。
被spring-boot-starter-web使用的默认启动器Servlet容器。
testImplementation('org.springframework.boot:spring-boot-starter-test') {
//JUnit4以前のグループを除外
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
使用JUnit、Hamcrest、Mockito等库来测试Spring Boot应用程序的起始器
请使用Gradle命令“gradle dependencies”来确认依赖树,同时声明库的版本设置。如果有任何修改需要,将随时进行更新。