简体   繁体   中英

Different approaches to build basic web app with spring boot?

I was trying to understand how to build "basic" web app with spring boot. I found different articles online to do it in various different ways:

  1. This article
    • Makes its main class
      • Use @SpringBootApplication
      • extends SpringBootServletInitializer
      • Call SpringApplication.run() in its main()
    • Finally creates @Controller and JSP pages
  2. This article does almost same as above, but does not makes its main calls extends SpringBootServletInitializer
  3. This article is also same as first one, with only difference that it makes its main class implements CommandLineRunner

My questions:

  1. Are approaches correct?
  2. What is the difference between them?
  3. Is extends SpringBootServletInitializer in article 1 unnecessary?
  4. I read CommandLineRunner is required to build non web apps. Then how example in article 3 works?

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.

#2 didn't need that optional feature for the demo.

#3 uses CommandLineRunner to seed test data needed by its demo.

extends SpringBootServletInitializer in article 1 unnecessary?

Yes. Only needed if you want to be able to deploy as .war file.

I read CommandLineRunner is required to build non web apps. Then how example in article 3 works?

It might be required for non web app, but that doesn't mean it's invalid for a web app.

Non web apps (may) need it as the entry point for running the main non web app logic.

Web apps don't need it, unless they need extra initialization logic, because the embedded servlet container is automatically started by SpringApplication.run() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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