成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Spring和SpringBoot比較,解惑區別

新聞 前端
對于Spring和SpringBoot到底有什么區別,我聽到了很多答案,剛開始邁入學習SpringBoot的我當時也是一頭霧水,隨著經驗的積累、我慢慢理解了這兩個框架到底有什么區別。

 

1、概述:

       對于SpringSpringBoot到底有什么區別,我聽到了很多答案,剛開始邁入學習SpringBoot的我當時也是一頭霧水,隨著經驗的積累、我慢慢理解了這兩個框架到底有什么區別,我相信對于用了SpringBoot很久的開發人員來說,有絕大部分還不是很理解SpringBoot到底和Spring有什么區別,看完文章中的比較,或許你有了不同的答案和看法!

2、什么是Spring呢?

       先來聊一聊Spring作為Java開發人員,大家都Spring可不陌生,簡而言之,Spring框架為開發Java應用程序提供了全面的基礎架構支持。它包含一些很好的功能,如依賴注入和開箱即用的模塊,如:
       Spring JDBC 、Spring MVC 、Spring Security、 Spring AOP 、Spring ORM 、Spring Test
       這些模塊大家應該都用過吧,這些模塊縮短應用程序的開發時間,提高了應用開發的效率
       例如,在Java Web開發的早期階段,我們需要編寫大量的代碼來將記錄插入到數據源中。但是通過使用Spring JDBC模塊的JDBCTemplate,我們可以將這操作簡化為只需配置幾行代碼。

3、什么是Spring Boot呢?

       Spring Boot基本上是Spring框架的擴展,它消除了設置Spring應用程序所需的XML配置,為更快,更高效的開發生態系統鋪平了道路。

以下是Spring Boot中的一些特點:

 1:創建獨立的spring應用。
 2:嵌入Tomcat, Jetty Undertow 而且不需要部署他們。
 3:提供的“starters” poms來簡化Maven配置
 4:盡可能自動配置spring應用。
 5:提供生產指標,健壯檢查和外部化配置
 6:絕對沒有代碼生成和XML配置要求

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

4.1、 Maven依賴

首先,讓我們看一下使用Spring創建Web應用程序所需的最小依賴項

  1. <dependency> 
  2.     <groupId>org.springframework</groupId> 
  3.     <artifactId>spring-web</artifactId> 
  4.     <version>5.1.0.RELEASE</version> 
  5. </dependency> 
  6. <dependency> 
  7.     <groupId>org.springframework</groupId> 
  8.     <artifactId>spring-webmvc</artifactId> 
  9.     <version>5.1.0.RELEASE</version> 
  10. </dependency> 

與Spring不同,Spring Boot只需要一個依賴項來啟動和運行Web應用程序:

  1. <dependency> 
  2.     <groupId>org.springframework.boot</groupId> 
  3.     <artifactId>spring-boot-starter-web</artifactId> 
  4.     <version>2.0.6.RELEASE</version> 
  5. </dependency> 

在進行構建期間,所有其他依賴項將自動添加到項目中。

       另一個很好的例子就是測試庫。我們通常使用Spring TestJUnitHamcrestMockito庫。在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

有關starter的完整列表,請查看Spring文檔

4.2、MVC配置

讓我們來看一下SpringSpring Boot創建JSP Web應用程序所需的配置。

Spring需要定義調度程序servlet,映射和其他支持配置。我們可以使用 web.xml 文件或Initializer類來完成此操作:

  1. public class MyWebAppInitializer implements WebApplicationInitializer { 
  2.    
  3.     @Override 
  4.     public void onStartup(ServletContext container) { 
  5.         AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); 
  6.         context.setConfigLocation("com.pingfangushi"); 
  7.           container.addListener(new ContextLoaderListener(context)); 
  8.           ServletRegistration.Dynamic dispatcher = container 
  9.           .addServlet("dispatcher"new DispatcherServlet(context)); 
  10.         dispatcher.setLoadOnStartup(1); 
  11.         dispatcher.addMapping("/"); 
  12.     } 

還需要將@EnableWebMvc注釋添加到@Configuration類,并定義一個視圖解析器來解析從控制器返回的視圖:

  1. @EnableWebMvc 
  2. @Configuration 
  3. public class ClientWebConfig implements WebMvcConfigurer {  
  4.    @Bean 
  5.    public ViewResolver viewResolver() { 
  6.       InternalResourceViewResolver bean 
  7.         = new InternalResourceViewResolver(); 
  8.       bean.setViewClass(JstlView.class); 
  9.       bean.setPrefix("/WEB-INF/view/"); 
  10.       bean.setSuffix(".jsp"); 
  11.       return bean; 
  12.    } 

和上述操作一比,一旦我們添加了Web啟動程序,Spring Boot只需要在application配置文件中配置幾個屬性來完成如上操作:

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

上面的所有Spring配置都是通過一個名為auto-configuration的過程添加Boot web starter來自動包含的。

       這意味著Spring Boot將查看應用程序中存在的依賴項,屬性和bean,并根據這些依賴項,對屬性和bean進行配置。當然,如果我們想要添加自己的自定義配置,那么Spring Boot自動配置將會退回。

4.3、配置模板引擎

現在我們來看下如何在Spring和Spring Boot中配置Thymeleaf模板引擎。
Spring中,我們需要為視圖解析器添加thymeleaf-spring5依賴項和一些配置:

  1. @Configuration 
  2. @EnableWebMvc 
  3. public class MvcWebConfig implements WebMvcConfigurer { 
  4.   
  5.     @Autowired 
  6.     private ApplicationContext applicationContext; 
  7.   
  8.     @Bean 
  9.     public SpringResourceTemplateResolver templateResolver() { 
  10.         SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); 
  11.         templateResolver.setApplicationContext(applicationContext); 
  12.         templateResolver.setPrefix("/WEB-INF/views/"); 
  13.         templateResolver.setSuffix(".html"); 
  14.         return templateResolver; 
  15.     } 
  16.   
  17.     @Bean 
  18.     public SpringTemplateEngine templateEngine() { 
  19.         SpringTemplateEngine templateEngine = new SpringTemplateEngine(); 
  20.         templateEngine.setTemplateResolver(templateResolver()); 
  21.         templateEngine.setEnableSpringELCompiler(true); 
  22.         return templateEngine; 
  23.     } 
  24.   
  25.     @Override 
  26.     public void configureViewResolvers(ViewResolverRegistry registry) { 
  27.         ThymeleafViewResolver resolver = new ThymeleafViewResolver(); 
  28.         resolver.setTemplateEngine(templateEngine()); 
  29.         registry.viewResolver(resolver); 
  30.     } 

       SpringBoot1X只需要spring-boot-starter-thymeleaf的依賴 項 來啟用Web應用程序中的       Thymeleaf支持。但是由于Thymeleaf3.0中的新功能, 我們必須將thymeleaf-layout-dialect 添加 為SpringBoot2XWeb應用程序中的依賴項。一旦依賴關系到位,我們就可以將模板添加到src/main/resources/templates文件夾中,SpringBoot將自動顯示它們。

4.4、Spring Security 配置

       為簡單起見,我們使用框架默認的HTTP Basic身份驗證。讓我們首先看一下使用Spring啟用Security所需的依賴關系和配置。
       Spring首先需要依賴 spring-security-webspring-security-config 模塊。接下來, 我們需要添加一個擴展WebSecurityConfigurerAdapter的類,并使用@EnableWebSecurity注解:

  1. @Configuration 
  2. @EnableWebSecurity 
  3. public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { 
  4.    
  5.     @Autowired 
  6.     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
  7.         auth.inMemoryAuthentication() 
  8.           .withUser("admin"
  9.             .password(passwordEncoder() 
  10.             .encode("password")) 
  11.           .authorities("ROLE_ADMIN"); 
  12.     } 
  13.    
  14.     @Override 
  15.     protected void configure(HttpSecurity http) throws Exception { 
  16.         http.authorizeRequests() 
  17.           .anyRequest().authenticated() 
  18.           .and() 
  19.           .httpBasic(); 
  20.     } 
  21.       
  22.     @Bean 
  23.     public PasswordEncoder passwordEncoder() { 
  24.         return new BCryptPasswordEncoder(); 
  25.     } 

       這里我們使用inMemoryAuthentication來設置身份驗證。同樣,Spring Boot也需要這些依賴項才能使其工作。但是我們只需要定義spring-boot-starter-security的依賴關系,因為這會自動將所有相關的依賴項添加到類路徑中。

Spring Boot中的安全配置與上面的相同。

5、應用程序引導配置

SpringSpring Boot中應用程序引導的基本區別在于servlet
Spring使用web.xmlSpringServletContainerInitializer作為其引導入口點。
Spring Boot僅使用Servlet 3功能來引導應用程序,下面讓我們詳細來了解下

5.1、Spring 是怎樣引導配置的呢?

Spring支持傳統的web.xml引導方式以及***的Servlet 3+方法。

讓我們看一下 web.xml方法的步驟:

Servlet容器(服務器)讀取web.xml
web.xml中定義的DispatcherServlet由容器實例化
DispatcherServlet通過讀取WEB-INF / {servletName} -servlet.xml來創建WebApplicationContext
***,DispatcherServlet注冊在應用程序上下文中定義的bean

以下是使用Servlet 3+方法的Spring引導:

容器搜索實現ServletContainerInitializer的類并執行
SpringServletContainerInitializer找到實現所有類WebApplicationInitializer
WebApplicationInitializer創建具有XML或上下文@Configuration
WebApplicationInitializer創建DispatcherServlet的 與先前創建的上下文。

5.2、SpringBoot 有是如何配置的呢?

Spring Boot應用程序的入口點是使用@SpringBootApplication注釋的類:
  1. @SpringBootApplication 
  2. public class Application { 
  3.     public static void main(String[] args) { 
  4.         SpringApplication.run(Application.class, args); 
  5.     } 

       默認情況下,Spring Boot使用嵌入式容器來運行應用程序。在這種情況下,Spring Boot使用public static void main入口點來啟動嵌入式Web服務器。此外,它還負責將ServletFilterServletContextInitializer bean從應用程序上下文綁定到嵌入式servlet容器。
Spring Boot的另一個特性是它會自動掃描同一個包中的所有類或Main類的子包中的組件。
Spring Boot提供了將其部署到外部容器的方式。在這種情況下,我們必須擴展SpringBootServletInitializer

  1. /** 
  2. * War部署 
  3. * 
  4. * @author SanLi 
  5. * Created by 2689170096@qq.com on 2018/4/15 
  6. */ 
  7. public class ServletInitializer extends SpringBootServletInitializer { 
  8.  
  9.   @Override 
  10.   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
  11.       return application.sources(Application.class); 
  12.   } 
  13.  
  14.   @Override 
  15.   public void onStartup(ServletContext servletContext) throws ServletException { 
  16.       super.onStartup(servletContext); 
  17.       servletContext.addListener(new HttpSessionEventPublisher()); 
  18.   } 

       這里外部servlet容器查找在war包下的META-INF文件夾下MANIFEST.MF文件中定義的Main-classSpringBootServletInitializer將負責綁定ServletFilterServletContextInitializer

6、打包和部署

       ***,讓我們看看如何打包和部署應用程序。這兩個框架都支持MavenGradle等通用包管理技術。但是在部署方面,這些框架差異很大。例如,Spring Boot Maven插件在Maven中提供Spring Boot支持。它還允許打​​包可執行jarwar包并就地運行應用程序。

在部署環境中Spring Boot 對比Spring的一些優點包括:
  • 提供嵌入式容器支持
  • 使用命令java -jar獨立運行jar
  • 在外部容器中部署時,可以選擇排除依賴關系以避免潛在的jar沖突
  • 部署時靈活指定配置文件的選項
  • 用于集成測試的隨機端口生成

7、結論

        簡而言之,我們可以說Spring Boot只是Spring本身的擴展,使開發,測試和部署更加方便。

責任編輯:張燕妮 來源: leshalv.net
相關推薦

2017-07-11 16:45:51

Python整數比較

2019-03-25 22:31:22

開發者技能框架

2009-06-26 14:37:10

EJB和Spring

2020-10-28 09:50:33

SpringBootJava

2010-07-14 16:48:02

Perl字符串比較

2018-08-03 11:10:30

前端小程序vue.js

2009-08-26 14:27:54

C#委托和事件

2020-06-24 09:35:50

SpringSpring BooJava

2013-05-27 09:32:07

構建私有云OpenStack開源云計算

2025-03-10 09:30:00

SpringJava開發

2023-07-06 14:24:23

Spring接口自定義

2016-10-14 15:00:45

2017-09-12 15:56:43

邊緣計算云計算架構

2011-08-10 16:30:36

數據庫表分區表分割

2009-06-19 17:05:08

MVC框架Struts和Spri

2009-07-20 10:36:29

什么是JDBC

2012-03-23 14:02:11

云計算

2023-04-28 08:21:36

SpringBoot聲明式事務編程式事務

2023-09-15 11:26:16

2009-09-12 10:22:20

unixwindows操作系統
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 一区二区三区亚洲 | 欧美日韩在线综合 | 精品1区| 国产欧美在线一区 | 精品久久久久久久人人人人传媒 | 欧美 日韩 在线播放 | 亚洲精品视频观看 | 91久久 | 日本精品一区二区三区视频 | 免费精品| 伦理午夜电影免费观看 | 精品国产一区二区三区性色av | 久久91av | 精品伊人久久 | 91在线免费观看 | 欧美专区在线 | 久久精品电影 | 亚洲国产精品suv | 亚洲视频一区在线观看 | 综合欧美亚洲 | 亚洲精品日韩在线 | 天堂av中文在线 | 日韩免费一区二区 | 福利久久| 91精品国产91久久久久久最新 | 亚洲综合大片69999 | 欧美在线一区二区三区 | 国产精品一区一区 | 国产亚洲精品精品国产亚洲综合 | 欧美国产精品一区二区 | 999久久久| 欧美a∨ | 亚洲精品日韩一区二区电影 | 嫩草视频在线 | 综合色在线 | 国产精品小视频在线观看 | 国产重口老太伦 | 免费特黄视频 | 精品粉嫩aⅴ一区二区三区四区 | 99精品视频一区二区三区 | 国产精品九九九 |