前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot构建系统

Spring Boot构建系统

作者头像
黑洞代码
发布2022-07-01 16:08:19
5700
发布2022-07-01 16:08:19
举报

在Spring Boot中,选择构建系统是一项重要任务。建议使用Maven或Gradle,因为它们可以为依赖关系管理提供良好的支持。Spring不支持其他构建系统。

依赖管理

Spring Boot团队提供了一个依赖项列表,以支持每个版本的Spring Boot版本。无需在构建配置文件中提供依赖项版本。Spring Boot会根据发行版自动配置依赖项版本。请记住,升级Spring Boot版本时,依赖项也会自动升级。

注 - 如果要指定依赖项的版本,可以在配置文件中指定它。但是,Spring Boot团队强烈建议不要指定依赖项的版本。

Maven依赖

对于Maven配置,应该继承Spring Boot Starter父项目来管理Spring Boot Starters依赖项。因此只需在pom.xml 文件中继承启动父级,如下所示。

代码语言:javascript
复制
<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.5.8.RELEASE</version>
</parent>


应该指定Spring Boot父 Starter依赖项的版本号。然后,对于其他启动器依赖项,不需要指定Spring Boot版本号。观察下面给出的代码 -

代码语言:javascript
复制
<dependencies>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
</dependencies>


Gradle依赖

可以将Spring Boot Starters依赖项直接导入build.gradle 文件。不需要Spring Boot启动父依赖,例如:Gradle。观察下面给出的代码 -

代码语言:javascript
复制
buildscript {
   ext {
      springBootVersion = '1.5.8.RELEASE'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}


同样,在Gradle中,不需要为依赖项指定Spring Boot版本号。Spring Boot会根据版本自动配置依赖项。

代码语言:javascript
复制
dependencies {
   compile('org.springframework.boot:spring-boot-starter-web')
}

依赖注入

在Spring Boot中,可以使用Spring Framework来定义bean及其依赖注入。@ComponentScan注释用于查找bean以及使用@Autowired注释注入的相应内容。如果遵循Spring Boot典型布局,则无需为@ComponentScan注释指定任何参数。所有组件类文件都自动注册到Spring Beans。以下示例提供了有关自动连接Rest Template对象并为其创建Bean代码片段 -

代码语言:javascript
复制
@Bean
public RestTemplate getRestTemplate() {
   return new RestTemplate();
}


以下代码显示主Spring Boot Application类文件中自动连接的Rest Template对象和Bean创建对象的代码 -

代码语言:javascript
复制
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class DemoApplication {
@Autowired
   RestTemplate restTemplate;

   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
   @Bean
   public RestTemplate getRestTemplate() {
      return new RestTemplate();   
   }
}


应用程序运行器

应用程序运行器(Runner)是一个用于在Spring Boot应用程序启动后执行代码的接口。下面给出的示例显示了如何在主类文件上实现Application Runner接口。

代码语言:javascript
复制
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication implements ApplicationRunner {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
   @Override
   public void run(ApplicationArguments arg0) throws Exception {
      System.out.println("Hello World from Application Runner");
   }
}


现在,如果从Application Runner观察Hello World下面的控制台窗口,则在Tomcat启动后执行println语句。以下屏幕截图所示:

命令行运行器

控制台窗口Runner是一个接口。它用于在Spring Boot应用程序启动后执行代码。下面给出的示例显示了如何在主类文件上实现控制台窗口Runner接口。

代码语言:javascript
复制
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
   @Override
   public void run(String... arg0) throws Exception {
      System.out.println("Hello world from Command Line Runner");
   }
}


查看下面的控制台窗口可以看到打印的字符串:"Hello world from Command Line Runner",它在Tomcat启动后执行println语句。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-07-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 落叶飞翔的蜗牛 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 依赖管理
  • Maven依赖
  • Gradle依赖
  • 依赖注入
  • 应用程序运行器
  • 命令行运行器
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档