1. ภาพรวม
ในบทความนี้เราจะมาดูความแตกต่างระหว่าง Spring frameworks มาตรฐานและ Spring Boot
เราจะเน้นและพูดคุยว่าโมดูลของ Spring เช่น MVC และ Security แตกต่างกันอย่างไรเมื่อใช้ใน Core Spring เทียบกับเมื่อใช้กับ Boot
2. ฤดูใบไม้ผลิคืออะไร?
ใส่เพียงแค่กรอบฤดูใบไม้ผลิให้การสนับสนุนโครงสร้างพื้นฐานที่ครอบคลุมสำหรับการพัฒนาโปรแกรม Java
มันเต็มไปด้วยคุณสมบัติที่ดีบางอย่างเช่น Dependency Injection และโมดูลนอกกล่องเช่น:
- สปริง JDBC
- MVC ฤดูใบไม้ผลิ
- ความปลอดภัยในฤดูใบไม้ผลิ
- สปริง AOP
- ฤดูใบไม้ผลิ ORM
- การทดสอบสปริง
โมดูลเหล่านี้สามารถลดเวลาในการพัฒนาแอปพลิเคชันได้อย่างมาก
ตัวอย่างเช่นในช่วงแรกของการพัฒนาเว็บ Java เราจำเป็นต้องเขียนโค้ดสำเร็จรูปจำนวนมากเพื่อแทรกบันทึกลงในแหล่งข้อมูล แต่ด้วยการใช้JDBCTemplateของโมดูล Spring JDBC เราสามารถลดโค้ดให้เหลือเพียงไม่กี่บรรทัดด้วยการกำหนดค่าเพียงเล็กน้อย
3. Spring Boot คืออะไร?
Spring Boot เป็นส่วนขยายของ Spring framework ซึ่งตัดการกำหนดค่าสำเร็จรูปที่จำเป็นสำหรับการตั้งค่าแอปพลิเคชัน Spring
มันต้องใช้มุมมองความเห็นของแพลตฟอร์มฤดูใบไม้ผลิซึ่งปูทางสำหรับการพัฒนาระบบนิเวศได้เร็วขึ้นและมีประสิทธิภาพมากขึ้น
นี่เป็นเพียงคุณสมบัติบางอย่างใน Spring Boot:
- การอ้างอิง 'เริ่มต้น' ที่ได้รับการพิจารณาเพื่อลดความซับซ้อนของการสร้างและการกำหนดค่าแอปพลิเคชัน
- เซิร์ฟเวอร์ในตัวเพื่อหลีกเลี่ยงความซับซ้อนในการปรับใช้แอปพลิเคชัน
- เมตริกการตรวจสอบความสมบูรณ์และการกำหนดค่าภายนอก
- กำหนดค่าอัตโนมัติสำหรับฟังก์ชัน Spring - เมื่อใดก็ตามที่ทำได้
มาทำความคุ้นเคยกับกรอบงานทั้งสองนี้ทีละขั้นตอน
4. การพึ่งพา Maven
ก่อนอื่นเรามาดูการอ้างอิงขั้นต่ำที่จำเป็นในการสร้างเว็บแอปพลิเคชันโดยใช้ Spring:
org.springframework spring-web 5.2.9.RELEASE org.springframework spring-webmvc 5.2.9.RELEASE
ซึ่งแตกต่างจาก Spring Spring Boot ต้องการการพึ่งพาเพียงครั้งเดียวเพื่อให้เว็บแอปพลิเคชันทำงานได้:
org.springframework.boot spring-boot-starter-web 2.3.4.RELEASE
การอ้างอิงอื่น ๆ ทั้งหมดจะถูกเพิ่มลงในไฟล์เก็บถาวรสุดท้ายโดยอัตโนมัติในช่วงเวลาสร้าง
อีกตัวอย่างที่ดีคือการทดสอบไลบรารี โดยปกติเราจะใช้ชุดของห้องสมุด Spring Test, JUnit, Hamcrest และ Mockito ในโปรเจ็กต์ Spring เราควรเพิ่มไลบรารีเหล่านี้ทั้งหมดเป็นการอ้างอิง
แต่ใน Spring Boot เราต้องการเพียงแค่การพึ่งพาเริ่มต้นสำหรับการทดสอบเพื่อรวมไลบรารีเหล่านี้โดยอัตโนมัติ
Spring Boot มีการพึ่งพาสตาร์ทเตอร์จำนวนมากสำหรับโมดูล Spring ต่างๆ สิ่งที่ใช้บ่อยที่สุด ได้แก่ :
- สปริงบูตเริ่มต้นข้อมูล jpa
- สปริงบูตเริ่มต้นความปลอดภัย
- สปริงบูตเริ่มต้นทดสอบ
- ฤดูใบไม้ผลิบูตเริ่มต้นเว็บ
- ฤดูใบไม้ผลิบูตเริ่มต้นโหระพา
สำหรับรายการเริ่มต้นทั้งหมดโปรดดูเอกสาร Spring
5. การกำหนดค่า MVC
มาสำรวจการกำหนดค่าที่จำเป็นในการสร้างเว็บแอปพลิเคชัน JSP โดยใช้ทั้ง Spring และ Spring Boot
Spring ต้องการการกำหนด servlet ตัวเลือกจ่ายการแมปและการกำหนดค่าอื่น ๆ ที่รองรับ เราสามารถทำได้โดยใช้ไฟล์web.xmlหรือคลาสInitializer :
public class MyWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation("com.baeldung"); container.addListener(new ContextLoaderListener(context)); ServletRegistration.Dynamic dispatcher = container .addServlet("dispatcher", new DispatcherServlet(context)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } }
We also need to add the @EnableWebMvc annotation to a @Configuration class and define a view-resolver to resolve the views returned from the controllers:
@EnableWebMvc @Configuration public class ClientWebConfig implements WebMvcConfigurer { @Bean public ViewResolver viewResolver() { InternalResourceViewResolver bean = new InternalResourceViewResolver(); bean.setViewClass(JstlView.class); bean.setPrefix("/WEB-INF/view/"); bean.setSuffix(".jsp"); return bean; } }
By comparison to all this, Spring Boot only needs a couple of properties to make things work, once we've added the web starter:
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
All the Spring configuration above is automatically included by adding the Boot web starter, through a process called auto-configuration.
What this means is that Spring Boot will look at the dependencies, properties, and beans that exist in the application and enable configuration based on these.
Of course, if we want to add our own custom configuration, then the Spring Boot auto-configuration will back away.
5.1. Configuring Template Engine
Let's now learn how to configure a Thymeleaf template engine in both Spring and Spring Boot.
In Spring we need to add the thymeleaf-spring5 dependency and some configurations for the view resolver:
@Configuration @EnableWebMvc public class MvcWebConfig implements WebMvcConfigurer { @Autowired private ApplicationContext applicationContext; @Bean public SpringResourceTemplateResolver templateResolver() { SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); templateResolver.setApplicationContext(applicationContext); templateResolver.setPrefix("/WEB-INF/views/"); templateResolver.setSuffix(".html"); return templateResolver; } @Bean public SpringTemplateEngine templateEngine() { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver()); templateEngine.setEnableSpringELCompiler(true); return templateEngine; } @Override public void configureViewResolvers(ViewResolverRegistry registry) { ThymeleafViewResolver resolver = new ThymeleafViewResolver(); resolver.setTemplateEngine(templateEngine()); registry.viewResolver(resolver); } }
Spring Boot 1 required only the dependency of spring-boot-starter-thymeleaf to enable Thymeleaf support in a web application. But because of the new features in Thymeleaf3.0, we have to add thymeleaf-layout-dialect also as a dependency in a Spring Boot 2 web application. Alternatively, we can choose to add a spring-boot-starter-thymeleaf dependency that'll take care of all this for us.
Once the dependencies are in place, we can add the templates to the src/main/resources/templates folder and the Spring Boot will display them automatically.
6. Spring Security Configuration
For the sake of simplicity, we'll see how the default HTTP Basic authentication is enabled using these frameworks.
Let's start by looking at the dependencies and configuration we need to enable Security using Spring.
Spring requires both the standard spring-security-web and spring-security-config dependencies to set up Security in an application.
Next, we need to add a class that extends the WebSecurityConfigurerAdapter and makes use of the @EnableWebSecurity annotation:
@Configuration @EnableWebSecurity public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user1") .password(passwordEncoder() .encode("user1Pass")) .authorities("ROLE_USER"); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() .and() .httpBasic(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } }
Here we're using inMemoryAuthentication to set up the authentication.
Similarly, Spring Boot also requires these dependencies to make it work. But we need to define only the dependency ofspring-boot-starter-security as this will automatically add all the relevant dependencies to the classpath.
The security configuration in Spring Boot is the same as the one above.
If you need to know how the JPA configuration can be achieved in both Spring and Spring Boot, then check out our article A Guide to JPA with Spring.
7. Application Bootstrap
The basic difference in bootstrapping of an application in Spring and Spring Boot lies with the servlet. Spring uses either the web.xml or SpringServletContainerInitializer as its bootstrap entry point.
On the other hand, Spring Boot uses only Servlet 3 features to bootstrap an application. Let's talk about this in detail.
7.1. How Spring Bootstraps?
Spring supports both the legacy web.xml way of bootstrapping as well as the latest Servlet 3+ method.
Let's see the web.xml approach in steps:
- Servlet container (the server) reads web.xml
- The DispatcherServlet defined in the web.xml is instantiated by the container
- DispatcherServlet creates WebApplicationContext by reading WEB-INF/{servletName}-servlet.xml
- Finally, the DispatcherServlet registers the beans defined in the application context
Here's how Spring bootstraps using Servlet 3+ approach:
- The container searches for classes implementing ServletContainerInitializer and executes
- The SpringServletContainerInitializer finds all classes implementing WebApplicationInitializer
- The WebApplicationInitializer creates the context with XML or @Configuration classes
- The WebApplicationInitializer creates the DispatcherServlet with the previously created context.
7.2. How Spring Boot Bootstraps?
The entry point of a Spring Boot application is the class which is annotated with @SpringBootApplication:
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
By default, Spring Boot uses an embedded container to run the application. In this case, Spring Boot uses the public static void main entry-point to launch an embedded web server.
Also, it takes care of the binding of the Servlet, Filter, and ServletContextInitializer beans from the application context to the embedded servlet container.
Another feature of Spring Boot is that it automatically scans all the classes in the same package or sub packages of the Main-class for components.
Spring Boot provides the option of deploying it as a web archive in an external container as well. In this case, we have to extend the SpringBootServletInitializer:
@SpringBootApplication public class Application extends SpringBootServletInitializer { // ... }
Here the external servlet container looks for the Main-class defined in the META-INF file of the web archive and the SpringBootServletInitializer will take care of binding the Servlet, Filter, and ServletContextInitializer.
8. Packaging and Deployment
Finally, let's see how an application can be packaged and deployed. Both of these frameworks support the common package managing technologies like Maven and Gradle. But when it comes to deployment, these frameworks differ a lot.
For instance, the Spring Boot Maven Plugin provides Spring Boot support in Maven. It also allows packaging executable jar or war archives and running an application “in-place”.
Some of the advantages of Spring Boot over Spring in the context of deployment include:
- Provides embedded container support
- Provision to run the jars independently using the command java -jar
- Option to exclude dependencies to avoid potential jar conflicts when deploying in an external container
- Option to specify active profiles when deploying
- Random port generation for integration tests
9. Conclusion
ในบทช่วยสอนนี้เราได้เรียนรู้เกี่ยวกับความแตกต่างระหว่าง Spring และ Spring Boot
พูดไม่กี่คำเราสามารถพูดได้ว่า Spring Boot เป็นเพียงส่วนเสริมของ Spring เพื่อให้การพัฒนาทดสอบและการปรับใช้งานสะดวกยิ่งขึ้น