Spring和SpringBoot之間的區(qū)別
在本教程中,我們將研究標(biāo)準(zhǔn)Spring框架和Spring Boot之間的區(qū)別。
我們將重點(diǎn)討論Spring的模塊,如MVC和Security,在核心Spring中使用時(shí)與在Boot中使用時(shí)的區(qū)別。
Spring是什么?簡(jiǎn)單地說(shuō),Spring框架為開發(fā)Java應(yīng)用程序提供了全面的基礎(chǔ)設(shè)施支持。
它包含了一些很好的功能,比如依賴注入,以及一些現(xiàn)成的模塊,比如:
Spring JDBC Spring MVC Spring Security Spring AOP Spring ORM Spring Test這些模塊可以大大縮短應(yīng)用程序的開發(fā)時(shí)間。
例如,在java web開發(fā)的早期,我們需要編寫大量樣板代碼來(lái)將記錄插入到數(shù)據(jù)源中。通過(guò)使用springjdbc模塊的JDBCTemplate,我們可以用很少的配置將它簡(jiǎn)化為幾行代碼。
Spring Boot是什么?Spring Boot基本上是Spring框架的擴(kuò)展,它消除了設(shè)置Spring應(yīng)用程序所需的樣板配置。
它對(duì)Spring平臺(tái)持固執(zhí)己見的觀點(diǎn),它為更快、更高效的開發(fā)生態(tài)系統(tǒng)鋪平了道路。
以下是Spring Boot的一些功能:
持約定優(yōu)于配置的“starter”依賴關(guān)系,以簡(jiǎn)化構(gòu)建和應(yīng)用程序配置 嵌入式服務(wù)器避免了應(yīng)用程序部署的復(fù)雜性 度量、運(yùn)行狀況檢查和外部化配置 自動(dòng)配置-只要可能讓我們逐步熟悉這兩個(gè)框架。
Maven依賴項(xiàng)首先,讓我們看看使用Spring創(chuàng)建web應(yīng)用程序所需的最小依賴性:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.5</version></dependency><dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.5</version></dependency>
與Spring不同,Spring Boot只需要一個(gè)依賴項(xiàng)即可啟動(dòng)并運(yùn)行web應(yīng)用程序:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.4.4</version></dependency>
在構(gòu)建期間,所有其他依賴項(xiàng)都會(huì)自動(dòng)添加到最終存檔中。
另一個(gè)很好的例子是測(cè)試庫(kù)。我們通常使用一組Spring-Test、JUnit、Hamcrest和Mockito庫(kù)。在Spring項(xiàng)目中,我們應(yīng)該添加所有這些庫(kù)作為依賴項(xiàng)。
或者,在springboot中,我們只需要starter依賴項(xiàng)就可以自動(dòng)包含這些庫(kù)。
springboot為不同的Spring模塊提供了許多啟動(dòng)程序依賴項(xiàng)。最常用的方法有: spring-boot-starter-data-jpa spring-boot-starter-security spring-boot-starter-test spring-boot-starter-web spring-boot-starter-thymeleaf要獲得starters的完整列表,還可以查看Spring文檔:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-starter
MVC配置讓我們研究一下使用Spring和SpringBoot創(chuàng)建jsp web應(yīng)用程序所需的配置。
Spring需要定義dispatcherservlet、映射和其他支持配置。我們可以用web.xml文件或初始值設(shè)定項(xiàng)類:
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('/'); }}
我們還需要將@EnableWebMvc注釋添加到@Configuration類中,并定義一個(gè)視圖解析器來(lái)解析從控制器返回的視圖:
@EnableWebMvc@Configurationpublic 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; }}
相比之下,在添加web starter后,Spring Boot只需要幾個(gè)屬性就可以工作:
spring.mvc.view.prefix=/WEB-INF/jsp/spring.mvc.view.suffix=.jsp
通過(guò)一個(gè)名為auto-configuration的process添加bootwebstarter,上面所有的Spring配置都會(huì)自動(dòng)包含進(jìn)來(lái)。
這意味著springboot將查看應(yīng)用程序中存在的依賴項(xiàng)、屬性和bean,并基于它們啟用配置。
當(dāng)然,如果我們想添加我們自己的自定義配置,那么Spring-Boot自動(dòng)配置就會(huì)退出。
配置模板引擎現(xiàn)在讓我們學(xué)習(xí)如何在Spring和springboot中配置Thymeleaf模板引擎。
在Spring中,我們需要為視圖解析器添加thymeleaf-spring5依賴項(xiàng)和一些配置:
@Configuration@EnableWebMvcpublic 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); }}
SpringBoot只需要springbootstarter thymeleaf的依賴性就可以在web應(yīng)用程序中啟用thymeleaf支持。由于Thymeleaf3.0中的新特性,我們還必須在springboot2web應(yīng)用程序中添加thymeleaf-layout-dialect作為依賴項(xiàng)?;蛘?,我們可以選擇添加一個(gè)springbootstarter和eleaf依賴,它將為我們處理所有這些。
一旦依賴項(xiàng)就位,我們就可以將模板添加到src/main/resources/templates文件夾中,Spring引導(dǎo)將自動(dòng)顯示它們。
Spring Security 配置為了簡(jiǎn)單起見,我們將看到如何使用這些框架啟用默認(rèn)的HTTP基本身份驗(yàn)證。
讓我們先看看使用Spring啟用安全性所需的依賴項(xiàng)和配置。
Spring需要標(biāo)準(zhǔn)的springsecurityweb和springsecurityconfig依賴項(xiàng)來(lái)設(shè)置應(yīng)用程序中的安全性。
接下來(lái),我們需要添加一個(gè)類來(lái)擴(kuò)展WebSecurityConfigureAdapter并使用@EnableWebSecurity注釋:
@Configuration@EnableWebSecuritypublic 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(); }}
這里我們使用inMemoryAuthentication來(lái)設(shè)置身份驗(yàn)證。
springboot還需要這些依賴項(xiàng)才能工作,但是我們只需要定義spring-boot-starter-security的依賴項(xiàng),因?yàn)檫@樣會(huì)自動(dòng)將所有相關(guān)的依賴項(xiàng)添加到classpath類路徑中。
springboot中的security安全配置與上面的相同。
要了解如何在Spring和Spring引導(dǎo)中實(shí)現(xiàn)JPA配置,我們可以查看我們的文章A Guide To JPA with Spring:https://www.baeldung.com/the-persistence-layer-with-spring-and-jpa
Application Bootstrap
在Spring和Spring引導(dǎo)中引導(dǎo)應(yīng)用程序的基本區(qū)別在于servlet。Spring使用web.xml或SpringServletContainerInitializer作為其引導(dǎo)入口點(diǎn)。
另一方面,SpringBoot只使用Servlet3特性來(lái)引導(dǎo)應(yīng)用程序。我們來(lái)詳細(xì)談?wù)劇?/p>Spring如何引導(dǎo)?
Spring既支持傳統(tǒng)的web.xml引導(dǎo)方式以及最新的Servlet3+方法。
讓我們看看web.xml分步進(jìn)近:
1. Servlet容器(服務(wù)器)讀取web.xml.
2.DispatcherServlet定義在web.xml中由容器實(shí)例化。
3. DispatcherServlet通過(guò)讀取WEB-INF/{servletName}創(chuàng)建WebApplicationContext-servlet.xml.
4. 最后,DispatcherServlet注冊(cè)在應(yīng)用程序上下文中定義的bean。
下面是如何使用Servlet3+方法進(jìn)行Spring引導(dǎo):
1. 容器搜索實(shí)現(xiàn)ServletContainerInitializer的類并執(zhí)行。
2. SpringServletContainerInitializer查找實(shí)現(xiàn)WebApplicationInitializer的所有類。
3. WebApplicationInitializer使用XML或@Configuration類創(chuàng)建上下文。
4. WebApplicationInitializer使用先前創(chuàng)建的上下文創(chuàng)建DispatcherServlet。
如何啟動(dòng)Spring Boot?Spring Boot應(yīng)用程序的入口點(diǎn)是用@SpringBootApplication注釋的類:
@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
默認(rèn)情況下,springboot使用嵌入式容器來(lái)運(yùn)行應(yīng)用程序。在本例中,springboot使用public static void主入口點(diǎn)來(lái)啟動(dòng)嵌入式web服務(wù)器。
它還負(fù)責(zé)將Servlet、Filter和servletContextInitializerbean從應(yīng)用程序上下文綁定到嵌入式Servlet容器。
springboot的另一個(gè)特性是,它自動(dòng)掃描主類的同一個(gè)包或子包中的所有類中的組件。
此外,springboot還提供了將其部署為外部容器中的web存檔的選項(xiàng)。在這種情況下,我們必須擴(kuò)展SpringBootServletInitializer:
@SpringBootApplicationpublic class Application extends SpringBootServletInitializer { // ...}
在這里,外部servlet容器查找在web存檔的META-INF文件中定義的主類,SpringBootServletInitializer將負(fù)責(zé)綁定servlet、過(guò)濾器和ServletContextInitializer。
打包和部署最后,讓我們看看如何打包和部署應(yīng)用程序。這兩個(gè)框架都支持Maven和Gradle等常見的包管理技術(shù);但是,在部署方面,這些框架有很大的不同。
例如,springboot maven插件在Maven中提供springboot支持。它還允許打包可執(zhí)行jar或war,并“就地”運(yùn)行應(yīng)用程序
在部署環(huán)境中,Spring Boot優(yōu)于Spring的一些優(yōu)點(diǎn)包括:
提供嵌入式容器支持 設(shè)置為使用命令java-jar獨(dú)立運(yùn)行jar 用于排除依賴項(xiàng)的選項(xiàng),以避免在外部容器中部署時(shí)發(fā)生潛在的jar沖突 用于在部署時(shí)指定活動(dòng)配置文件的選項(xiàng) 集成測(cè)試的隨機(jī)端口生成 結(jié)論在本文中,我們了解了Spring和Spring Boot之間的區(qū)別。
簡(jiǎn)而言之,我們可以說(shuō)springboot只是Spring本身的一個(gè)擴(kuò)展,它使開發(fā)、測(cè)試和部署更加方便。
以上就是Spring和SpringBoot之間的區(qū)別的詳細(xì)內(nèi)容,更多關(guān)于Spring和SpringBoot區(qū)別的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
