久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術文章
文章詳情頁

Spring和SpringBoot之間的區(qū)別

瀏覽:28日期:2023-03-19 14:23:50

在本教程中,我們將研究標準Spring框架和Spring Boot之間的區(qū)別。

我們將重點討論Spring的模塊,如MVC和Security,在核心Spring中使用時與在Boot中使用時的區(qū)別。

Spring是什么?

簡單地說,Spring框架為開發(fā)Java應用程序提供了全面的基礎設施支持。

它包含了一些很好的功能,比如依賴注入,以及一些現(xiàn)成的模塊,比如:

Spring JDBC Spring MVC Spring Security Spring AOP Spring ORM Spring Test

這些模塊可以大大縮短應用程序的開發(fā)時間。

例如,在java web開發(fā)的早期,我們需要編寫大量樣板代碼來將記錄插入到數(shù)據源中。通過使用springjdbc模塊的JDBCTemplate,我們可以用很少的配置將它簡化為幾行代碼。

Spring Boot是什么?

Spring Boot基本上是Spring框架的擴展,它消除了設置Spring應用程序所需的樣板配置。

它對Spring平臺持固執(zhí)己見的觀點,它為更快、更高效的開發(fā)生態(tài)系統(tǒng)鋪平了道路。

以下是Spring Boot的一些功能:

持約定優(yōu)于配置的“starter”依賴關系,以簡化構建和應用程序配置 嵌入式服務器避免了應用程序部署的復雜性 度量、運行狀況檢查和外部化配置 自動配置-只要可能

讓我們逐步熟悉這兩個框架。

Maven依賴項

首先,讓我們看看使用Spring創(chuàng)建web應用程序所需的最小依賴性:

<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只需要一個依賴項即可啟動并運行web應用程序:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.4.4</version></dependency>

在構建期間,所有其他依賴項都會自動添加到最終存檔中。

另一個很好的例子是測試庫。我們通常使用一組Spring-Test、JUnit、Hamcrest和Mockito庫。在Spring項目中,我們應該添加所有這些庫作為依賴項。

或者,在springboot中,我們只需要starter依賴項就可以自動包含這些庫。

springboot為不同的Spring模塊提供了許多啟動程序依賴項。最常用的方法有: 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應用程序所需的配置。

Spring需要定義dispatcherservlet、映射和其他支持配置。我們可以用web.xml文件或初始值設定項類:

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類中,并定義一個視圖解析器來解析從控制器返回的視圖:

@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只需要幾個屬性就可以工作:

spring.mvc.view.prefix=/WEB-INF/jsp/spring.mvc.view.suffix=.jsp

通過一個名為auto-configuration的process添加bootwebstarter,上面所有的Spring配置都會自動包含進來。

這意味著springboot將查看應用程序中存在的依賴項、屬性和bean,并基于它們啟用配置。

當然,如果我們想添加我們自己的自定義配置,那么Spring-Boot自動配置就會退出。

配置模板引擎

現(xiàn)在讓我們學習如何在Spring和springboot中配置Thymeleaf模板引擎。

在Spring中,我們需要為視圖解析器添加thymeleaf-spring5依賴項和一些配置:

@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應用程序中啟用thymeleaf支持。由于Thymeleaf3.0中的新特性,我們還必須在springboot2web應用程序中添加thymeleaf-layout-dialect作為依賴項。或者,我們可以選擇添加一個springbootstarter和eleaf依賴,它將為我們處理所有這些。

一旦依賴項就位,我們就可以將模板添加到src/main/resources/templates文件夾中,Spring引導將自動顯示它們。

Spring Security 配置

為了簡單起見,我們將看到如何使用這些框架啟用默認的HTTP基本身份驗證。

讓我們先看看使用Spring啟用安全性所需的依賴項和配置。

Spring需要標準的springsecurityweb和springsecurityconfig依賴項來設置應用程序中的安全性。

接下來,我們需要添加一個類來擴展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來設置身份驗證。

springboot還需要這些依賴項才能工作,但是我們只需要定義spring-boot-starter-security的依賴項,因為這樣會自動將所有相關的依賴項添加到classpath類路徑中。

springboot中的security安全配置與上面的相同。

要了解如何在Spring和Spring引導中實現(xiàn)JPA配置,我們可以查看我們的文章A Guide To JPA with Spring:https://www.baeldung.com/the-persistence-layer-with-spring-and-jpa

Application Bootstrap

在Spring和Spring引導中引導應用程序的基本區(qū)別在于servlet。Spring使用web.xml或SpringServletContainerInitializer作為其引導入口點。

另一方面,SpringBoot只使用Servlet3特性來引導應用程序。我們來詳細談談。

Spring如何引導?

Spring既支持傳統(tǒng)的web.xml引導方式以及最新的Servlet3+方法。

讓我們看看web.xml分步進近:

1. Servlet容器(服務器)讀取web.xml.

2.DispatcherServlet定義在web.xml中由容器實例化。

3. DispatcherServlet通過讀取WEB-INF/{servletName}創(chuàng)建WebApplicationContext-servlet.xml.

4. 最后,DispatcherServlet注冊在應用程序上下文中定義的bean。

下面是如何使用Servlet3+方法進行Spring引導:

1. 容器搜索實現(xiàn)ServletContainerInitializer的類并執(zhí)行。

2. SpringServletContainerInitializer查找實現(xiàn)WebApplicationInitializer的所有類。

3. WebApplicationInitializer使用XML或@Configuration類創(chuàng)建上下文。

4. WebApplicationInitializer使用先前創(chuàng)建的上下文創(chuàng)建DispatcherServlet。

如何啟動Spring Boot?

Spring Boot應用程序的入口點是用@SpringBootApplication注釋的類:

@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}

默認情況下,springboot使用嵌入式容器來運行應用程序。在本例中,springboot使用public static void主入口點來啟動嵌入式web服務器。

它還負責將Servlet、Filter和servletContextInitializerbean從應用程序上下文綁定到嵌入式Servlet容器。

springboot的另一個特性是,它自動掃描主類的同一個包或子包中的所有類中的組件。

此外,springboot還提供了將其部署為外部容器中的web存檔的選項。在這種情況下,我們必須擴展SpringBootServletInitializer:

@SpringBootApplicationpublic class Application extends SpringBootServletInitializer { // ...}

在這里,外部servlet容器查找在web存檔的META-INF文件中定義的主類,SpringBootServletInitializer將負責綁定servlet、過濾器和ServletContextInitializer。

打包和部署

最后,讓我們看看如何打包和部署應用程序。這兩個框架都支持Maven和Gradle等常見的包管理技術;但是,在部署方面,這些框架有很大的不同。

例如,springboot maven插件在Maven中提供springboot支持。它還允許打包可執(zhí)行jar或war,并“就地”運行應用程序

在部署環(huán)境中,Spring Boot優(yōu)于Spring的一些優(yōu)點包括:

提供嵌入式容器支持 設置為使用命令java-jar獨立運行jar 用于排除依賴項的選項,以避免在外部容器中部署時發(fā)生潛在的jar沖突 用于在部署時指定活動配置文件的選項 集成測試的隨機端口生成 結論

在本文中,我們了解了Spring和Spring Boot之間的區(qū)別。

簡而言之,我們可以說springboot只是Spring本身的一個擴展,它使開發(fā)、測試和部署更加方便。

以上就是Spring和SpringBoot之間的區(qū)別的詳細內容,更多關于Spring和SpringBoot區(qū)別的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: www.国产.com | 中文字幕第66页 | 日韩福利视频导航 | 日韩五月 | 北条麻妃一区二区在线 | 艳妇荡乳豪妇荡淫 | 成人精品视频一区二区三区 | 日韩高清不卡一区二区三区 | 91电影在线 | 国产精品久久久久久久午夜 | 久久久久无码国产精品一区 | 亚洲日本中文 | 国产一区二区三区久久久久久久久 | 日韩欧美一区二区三区免费观看 | 免费毛片在线播放 | 欧美一级二级视频 | 亚洲一区二区三区在线视频 | 日韩欧美精品一区二区三区 | 福利视频一区二区 | 欧美在线小视频 | 在线99热 | 亚洲高清在线观看 | 日穴视频在线观看 | 91破解版在线 | 亚洲 | 17c一起操| 日韩一级免费在线观看 | 欧美天堂在线观看 | 精品久久久久久国产 | 天天躁日日躁aaaaxxxx | 免费高清av | 国产精品久久久一区二区 | 久久久a| 国产精品美女久久久久久久久久久 | 中文字幕久久久 | 久久国产精彩视频 | 最新高清无码专区 | 91极品视频在线观看 | 欧美成人免费在线视频 | 最新日韩精品在线观看 | 欧美午夜精品久久久 | 精产国产伦理一二三区 |