简体   繁体   English

使用 Spring Boot 启动器的更好方法

[英]Better way of using spring boot starters

I found two way of adding dependencies for spring boot rest service application.我找到了两种为 spring boot rest 服务应用程序添加依赖项的方法。

Method 1 :方法一:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>

Method 2:方法二:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

Both method given above does the same job?上面给出的两种方法都做同样的工作? Any difference in performance?性能上有什么区别吗?

Thanks in advance提前致谢

The difference between these methods is that spring-boot-starter-web contains more dependencies than just spring-boot-starter and spring-web .这些方法之间的区别在于spring-boot-starter-web包含更多的依赖项,而不仅仅是spring-boot-starterspring-web

For the version 1.5.8.RELEASE it will be:对于 1.5.8.RELEASE 版本,它将是:

  • spring-boot-starter弹簧启动器
  • spring-boot-starter-tomcat spring-boot-starter-tomcat
  • hibernate-validator休眠验证器
  • jackson-databind杰克逊数据绑定
  • spring-web弹簧网
  • spring-webmvc弹簧-webmvc

All other stuff depends on your requirements.所有其他的东西取决于你的要求。 For instance, if you're developing commercial product you must check all included transitive dependencies for their licenses.例如,如果您正在开发商业产品,您必须检查其许可证的所有包含的传递依赖项。

The general advice is to use only features you need.一般建议是仅使用您需要的功能。 Don't forget that you still can depend on top level artifact excluding not required parts using Maven feature.不要忘记,您仍然可以依赖顶级工件,不包括使用 Maven 功能的不需要的部分。

At for the performance boost, basically it should not be that much.在性能提升方面,基本上应该不会那么多。 The difference is only if the Spring (with top-level artifact dependency) will load and auto configure some features which are not practically used in your code (during the classpath scanning).区别仅在于 Spring(具有顶级工件依赖项)将加载并自动配置一些在您的代码中实际上未使用的功能(在类路径扫描期间)。 The startup time could be slightly increased by the same reason.出于同样的原因,启动时间可能会略有增加。

Hope it helps!希望能帮助到你!

Obviously second approach is better ie using springboot starter pom.显然第二种方法更好,即使用 springboot starter pom。

Reasons I say that for are as follows:-我这么说的原因如下:-

  1. It allows zero configuration or auto configuration ie most of the web related settings would be given to you by default.它允许zero configurationauto configuration即默认情况下将向您提供大多数与 Web 相关的设置。 eg by default tomcat server would be integrated, springboot dependencies would be added for you, automatic registration of converter and other web related dependencies etc. See this link.例如默认情况下将集成 tomcat 服务器,将为您添加 springboot 依赖项,自动注册转换器和其他与 Web 相关的依赖项等。请参阅链接。

  2. You could take advantage of easy override ie if tomorrow you want to use jetty in place of tomcat just add the jetty dependency and it's configurations and now you could use jetty.您可以利用简单的覆盖,即如果明天您想使用 jetty 代替 tomcat,只需添加 jetty 依赖项及其配置,现在您可以使用 jetty。

  3. Your pom.xml would be neater and more readable as less no.你的pom.xml会更整洁,更易读,因为更少。 of dependencies are put inside it and picked from starter poms.依赖项被放入其中并从 starter poms 中挑选。

  4. Easy compatibility management.轻松的兼容性管理。 By default spring-boot picks up the version of starter parent .默认情况下,spring-boot 选择starter parent的版本。 Hence, you could rest assured that all compatible dependencies would be downloaded as part of mvn dependencies and if you specifically want to upgrade any of those you could.因此,您可以放心,所有兼容的依赖项都将作为 mvn 依赖项的一部分下载,如果您特别想升级其中任何一个,则可以。 But this gives advantage of upgrading to compatible dependencies by just changing the version of springboot starter parent(Note: You could use dependency management as well in place of starter parent pom. See this link).但这可以通过更改 springboot starter parent 的版本来升级到兼容的依赖项(注意:您也可以使用依赖项管理来代替 starter parent pom。请参阅链接)。

Performance wise there would be a trade-off as springboot by default downloads more dependencies than minimum required initially.在性能方面会有一个折衷,因为 springboot 默认下载比最初所需的最小值更多的依赖项。 But over the time as application starts becoming mature most of these dependencies are used anyways.但是随着应用程序开始变得成熟,大多数这些依赖项无论如何都会被使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在常规Spring项目中使用Spring Boot Starters - Using Spring boot starters in regular Spring project 如何在Spring Boot Starters中指定maven版本? - How is the maven version specified in Spring Boot Starters? 使用 Junit 在 Spring 引导中编写单元测试的更好方法是什么? - What is better way to write Unit test in Spring Boot using Junit? 在 spring 引导中,在 redis 缓存(使用 redis 作为持久存储)中实现审计的更好方法是什么? - what is the better way to implement auditing in redis cache (using redis as persistant storage) in spring boot? 有没有更好的方法来使用 Java Spring Boot 和 JPA 来编写这个练习? - Is there a better way to code this exercise with Java Spring Boot and JPA? 在Spring Boot Project中将HikariCP与Hibernate结合使用的更好方法 - Better way to use HikariCP with Hibernate in Spring Boot Project 在应用程序运行时更新 Spring Boot 属性的更好方法 - Better way to update Spring Boot properties while app is running Spring Boot Starter。 如何在启动程序“ x”完成bean创建之前停止初始化其他启动程序? - Spring Boot Starter. How to stop from initialization other starters before starter 'x' finish beans creation? 使用 Spring Boot 实现 2 路 SSL - Implementing 2 way SSL using spring boot 有没有办法在使用Spring或Spring Boot的JUnit测试中使用Autowired构造函数? - Is there a way to use Autowired constructor in JUnit test using Spring or Spring Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM