简体   繁体   English

使用Spring Boot构建基本Web应用程序的不同方法?

[英]Different approaches to build basic web app with spring boot?

I was trying to understand how to build "basic" web app with spring boot. 我试图了解如何使用Spring Boot构建“基本” Web应用程序。 I found different articles online to do it in various different ways: 我在线上找到了不同的文章,以各种不同的方式来做它:

  1. This article 这篇文章
    • Makes its main class 成为主班
      • Use @SpringBootApplication 使用@SpringBootApplication
      • extends SpringBootServletInitializer
      • Call SpringApplication.run() in its main() main()调用SpringApplication.run() main()
    • Finally creates @Controller and JSP pages 最后创建@Controller和JSP页面
  2. This article does almost same as above, but does not makes its main calls extends SpringBootServletInitializer 文章确实与上述基本相同,但不使其主呼叫extends SpringBootServletInitializer
  3. This article is also same as first one, with only difference that it makes its main class implements CommandLineRunner 文章也是一样的第一个,用它使得它的主类唯一的区别implements CommandLineRunner

My questions: 我的问题:

  1. Are approaches correct? 方法正确吗?
  2. What is the difference between them? 它们之间有什么区别?
  3. Is extends SpringBootServletInitializer in article 1 unnecessary? 第1条中是否不需要extends SpringBootServletInitializer
  4. I read CommandLineRunner is required to build non web apps. 我读到CommandLineRunner是构建非Web应用程序所必需的。 Then how example in article 3 works? 那么第3条中的示例如何工作?

Are approaches correct? 方法正确吗?

Yes. 是。

What is difference between them? 它们之间有什么区别?

#1 extends SpringBootServletInitializer so the web application can also be deployed as a .war file into a standalone servlet container, instead of running it from the command-line using the embedded servlet container. #1扩展了SpringBootServletInitializer因此Web应用程序可以作为.war文件部署到独立的Servlet容器中,而不是使用嵌入式Servlet容器从命令行运行它。

#2 didn't need that optional feature for the demo. #2不需要该演示的可选功能。

#3 uses CommandLineRunner to seed test data needed by its demo. #3使用CommandLineRunner其演示所需的测试数据。

extends SpringBootServletInitializer in article 1 unnecessary? extends SpringBootServletInitializer了文章1中的extends SpringBootServletInitializer不必要?

Yes. 是。 Only needed if you want to be able to deploy as .war file. 仅当您希望能够作为.war文件进行部署时才需要。

I read CommandLineRunner is required to build non web apps. 我读到CommandLineRunner是构建非Web应用程序所必需的。 Then how example in article 3 works? 那么第3条中的示例如何工作?

It might be required for non web app, but that doesn't mean it's invalid for a web app. 非Web应用程序可能需要使用它,但这并不意味着它对于Web应用程序无效。

Non web apps (may) need it as the entry point for running the main non web app logic. 非Web应用程序(可能)需要它作为运行主要非Web应用程序逻辑的入口点。

Web apps don't need it, unless they need extra initialization logic, because the embedded servlet container is automatically started by SpringApplication.run() . Web应用程序不需要它,除非它们需要额外的初始化逻辑,因为嵌入式servlet容器由SpringApplication.run()自动启动。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM