對于Spring和SpringBoot到底有什么區別,我聽到了很多答案,剛開始邁入學習SpringBoot的我當時也是一頭霧水隨著經驗的積累、我慢慢理解了這兩個框架到底有什么區別。
相信對于用了SpringBoot很久的同學來說,還不是很理解SpringBoot到底和Spring有什么區別,看完文章中的比較,或許你有了不同的答案和看法!
什么是Spring?
作為Java開發人員,大家都Spring都不陌生,簡而言之,Spring框架為開發Java應用程序提供了全面的基礎架構支持。它包含一些很好的功能,如依賴注入和開箱即用的模塊,如:
Spring JDBC 、Spring MVC 、Spring Security、 Spring AOP 、Spring ORM 、Spring Test
這些模塊縮短應用程序的開發時間,提高了應用開發的效率例如,在Java Web開發的早期階段,我們需要編寫大量的代碼來將記錄插入到數據庫中。但是通過使用Spring JDBC模塊的JDBCTemplate,我們可以將操作簡化為幾行代碼。
什么是Spring Boot?
Spring Boot基本上是Spring框架的擴展,它消除了設置Spring應用程序所需的XML配置,為更快,更高效的開發生態系統鋪平了道路。
Spring Boot中的一些特征:
1)創建獨立的Spring應用。
2)嵌入式Tomcat、Jetty、 Undertow容器(無需部署war文件)。
3)提供的starters 簡化構建配置。
4)盡可能自動配置spring應用。
5)提供生產指標,例如指標、健壯檢查和外部化配置
6)完全沒有代碼生成和XML配置要求。
需要更多大廠面試資料的話也可以點擊直接進入,免費獲取!暗號:CSDN
讓我們從配置分析這兩個框架
1、Maven依賴
首先,讓我們看一下使用Spring創建Web應用程序所需的最小依賴項
1
2
3
4
5
6
7
8
9
10
|
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version> 5.1 . 0 .RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version> 5.1 . 0 .RELEASE</version> </dependency> |
與Spring不同,Spring Boot只需要一個依賴項來啟動和運行Web應用程序:
1
2
3
4
5
|
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version> 2.0 . 6 .RELEASE</version> </dependency> |
在進行構建期間,所有其他依賴項將自動添加到項目中。
另一個很好的例子就是測試庫。我們通常使用Spring Test,JUnit,Hamcrest和Mockito庫。在Spring項目中,我們應該將所有這些庫添加為依賴項。但是在Spring Boot中,我們只需要添加spring-boot-starter-test依賴項來自動包含這些庫。
Spring Boot為不同的Spring模塊提供了許多依賴項。
一些最常用的是:
spring-boot-starter-data-jpa
spring-boot-starter-security
spring-boot-starter-test
spring-boot-starter-web
spring-boot-starter-thymeleaf
2、MVC配置
讓我們來看一下Spring和Spring Boot創建JSP Web應用程序所需的配置。
Spring需要定義調度程序servlet,映射和其他支持配置。我們可以使用 web.xml 文件或Initializer類來完成此操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class MyWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation( "com.pingfangushi" ); container.addListener( new ContextLoaderListener(context)); ServletRegistration.Dynamic dispatcher = container .addServlet( "dispatcher" , new DispatcherServlet(context)); dispatcher.setLoadOnStartup( 1 ); dispatcher.addMapping( "/" ); } } |
還需要將@EnableWebMvc注釋添加到@Configuration類,并定義一個視圖解析器來解析從控制器返回的視圖:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@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; } } |
再來看SpringBoot一旦我們添加了Web啟動程序,Spring Boot只需要在application配置文件中配置幾個屬性來完成如上操作:
1
2
|
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp |
上面的所有Spring配置都是通過一個名為auto-configuration的過程添加Boot web starter來自動包含的。
這意味著Spring Boot將查看應用程序中存在的依賴項,屬性和bean,并根據這些依賴項,對屬性和bean進行配置。當然,如果我們想要添加自己的自定義配置,那么Spring Boot自動配置將會退回。
3、配置模板引擎
現在我們來看下如何在Spring和Spring Boot中配置Thymeleaf模板引擎。
在Spring中,我們需要為視圖解析器添加thymeleaf-spring5依賴項和一些配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
@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 Boot1X只需要spring-boot-starter-thymeleaf的依賴項來啟用Web應用程序中的Thymeleaf支持。
但是由于Thymeleaf3.0中的新功能,我們必須將thymeleaf-layout-dialect 添加為SpringBoot2XWeb應用程序中的依賴項。配置好依賴,我們就可以將模板添加到src/main/resources/templates文件夾中,Spring Boot將自動顯示它們。
需要更多大廠面試資料的話也可以點擊直接進入,免費獲??!暗號:CSDN
4、Spring Security 配置
為簡單起見,我們使用框架默認的HTTP Basic身份驗證。讓我們首先看一下使用Spring啟用Security所需的依賴關系和配置。
Spring首先需要依賴spring-security-web和spring-security-config模塊。接下來, 我們需要添加一個擴展WebSecurityConfigurerAdapter的類,并使用@EnableWebSecurity注解:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
@Configuration @EnableWebSecurity public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser( "admin" ) .password(passwordEncoder() .encode( "password" )) .authorities( "ROLE_ADMIN" ); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() .and() .httpBasic(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } |
這里我們使用inMemoryAuthentication來設置身份驗證。同樣,Spring Boot也需要這些依賴項才能使其工作。但是我們只需要定義spring-boot-starter-security的依賴關系,因為這會自動將所有相關的依賴項添加到類路徑中。
應用程序啟動引導配置
Spring和Spring Boot中應用程序引導的基本區別在于servlet。
Spring使用web.xml或SpringServletContainerInitializer作為其引導入口點。
總結
本片文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注服務器之家的更多內容!
原文鏈接:https://blog.csdn.net/m0_60707538/article/details/119960731