1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        Spring 和 SpringBoot 之間到底有啥區(qū)別?

        共 7525字,需瀏覽 16分鐘

         ·

        2021-07-21 11:59

        參考地址:https://blog.csdn.net/zhenghongcs/article/details/105154257

        概述


        對(duì)于 SpringSpringBoot到底有什么區(qū)別,我聽(tīng)到了很多答案,剛開(kāi)始邁入學(xué)習(xí) SpringBoot的我當(dāng)時(shí)也是一頭霧水,隨著經(jīng)驗(yàn)的積累、我慢慢理解了這兩個(gè)框架到底有什么區(qū)別,相信對(duì)于用了 SpringBoot很久的同學(xué)來(lái)說(shuō),還不是很理解 SpringBoot到底和 Spring有什么區(qū)別,看完文章中的比較,或許你有了不同的答案和看法!


        什么是Spring


        作為 Java開(kāi)發(fā)人員,大家都 Spring都不陌生,簡(jiǎn)而言之, Spring框架為開(kāi)發(fā) Java應(yīng)用程序提供了全面的基礎(chǔ)架構(gòu)支持。它包含一些很好的功能,如依賴注入和開(kāi)箱即用的模塊,如:SpringJDBC、SpringMVC、SpringSecurity、SpringAOP、SpringORM、SpringTest,這些模塊縮短應(yīng)用程序的開(kāi)發(fā)時(shí)間,提高了應(yīng)用開(kāi)發(fā)的效率例如,在 JavaWeb開(kāi)發(fā)的早期階段,我們需要編寫(xiě)大量的代碼來(lái)將記錄插入到數(shù)據(jù)庫(kù)中。但是通過(guò)使用 SpringJDBC模塊的 JDBCTemplate,我們可以將操作簡(jiǎn)化為幾行代碼。


        什么是Spring Boot


        SpringBoot基本上是 Spring框架的擴(kuò)展,它消除了設(shè)置 Spring應(yīng)用程序所需的 XML配置,為更快,更高效的開(kāi)發(fā)生態(tài)系統(tǒng)鋪平了道路。
        SpringBoot中的一些特征:
        1、創(chuàng)建獨(dú)立的 Spring應(yīng)用。
        2、嵌入式
        Tomcat、 Jetty、 Undertow容器(無(wú)需部署war文件)。
        3、提供的
        starters 簡(jiǎn)化構(gòu)建配置
        4、盡可能自動(dòng)配置
        spring應(yīng)用。
        5、提供生產(chǎn)指標(biāo),例如指標(biāo)、健壯檢查和外部化配置
        6、完全沒(méi)有代碼生成和
        XML配置要求


        從配置分析

        Maven依賴


        首先,讓我們看一下使用Spring創(chuàng)建Web應(yīng)用程序所需的最小依賴項(xiàng)


        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只需要一個(gè)依賴項(xiàng)來(lái)啟動(dòng)和運(yùn)行Web應(yīng)用程序:


        1. <dependency>

        2. <groupId>org.springframework.boot</groupId>

        3. <artifactId>spring-boot-starter-web</artifactId>

        4. <version>2.0.6.RELEASE</version>

        5. </dependency>


        在進(jìn)行構(gòu)建期間,所有其他依賴項(xiàng)將自動(dòng)添加到項(xiàng)目中。
        另一個(gè)很好的例子就是測(cè)試庫(kù)。我們通常使用 SpringTest, JUnit, HamcrestMockito庫(kù)。在 Spring項(xiàng)目中,我們應(yīng)該將所有這些庫(kù)添加為依賴項(xiàng)。但是在 SpringBoot中,我們只需要添加 spring-boot-starter-test依賴項(xiàng)來(lái)自動(dòng)包含這些庫(kù)。
        Spring Boot為不同的Spring模塊提供了許多依賴項(xiàng)。一些最常用的是:
        spring-boot-starter-data-jpaspring-boot-starter-securityspring-boot-starter-testspring-boot-starter-webspring-boot-starter-thymeleaf
        有關(guān) starter的完整列表,請(qǐng)查看Spring文檔。


        MVC配置


        讓我們來(lái)看一下 SpringSpringBoot創(chuàng)建 JSPWeb應(yīng)用程序所需的配置。
        Spring需要定義調(diào)度程序 servlet,映射和其他支持配置。我們可以使用 web.xml 文件或 Initializer類來(lái)完成此操作:


        1. publicclassMyWebAppInitializerimplementsWebApplicationInitializer{


        2. @Override

        3. publicvoid onStartup(ServletContext container) {

        4. AnnotationConfigWebApplicationContext context = newAnnotationConfigWebApplicationContext();

        5. context.setConfigLocation("com.pingfangushi");

        6. container.addListener(newContextLoaderListener(context));

        7. ServletRegistration.Dynamic dispatcher = container

        8. .addServlet("dispatcher", newDispatcherServlet(context));

        9. dispatcher.setLoadOnStartup(1);

        10. dispatcher.addMapping("/");

        11. }

        12. }


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


        1. @EnableWebMvc

        2. @Configuration

        3. publicclassClientWebConfigimplementsWebMvcConfigurer{

        4. @Bean

        5. publicViewResolver viewResolver() {

        6. InternalResourceViewResolver bean

        7. = newInternalResourceViewResolver();

        8. bean.setViewClass(JstlView.class);

        9. bean.setPrefix("/WEB-INF/view/");

        10. bean.setSuffix(".jsp");

        11. return bean;

        12. }

        13. }


        再來(lái)看 SpringBoot一旦我們添加了 Web啟動(dòng)程序, SpringBoot只需要在 application配置文件中配置幾個(gè)屬性來(lái)完成如上操作:


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

        2. spring.mvc.view.suffix=.jsp


        上面的所有Spring配置都是通過(guò)一個(gè)名為auto-configuration的過(guò)程添加 Bootweb starter來(lái)自動(dòng)包含的。
        這意味著 SpringBoot將查看應(yīng)用程序中存在的依賴項(xiàng),屬性和 bean,并根據(jù)這些依賴項(xiàng),對(duì)屬性和 bean進(jìn)行配置。當(dāng)然,如果我們想要添加自己的自定義配置,那么 SpringBoot自動(dòng)配置將會(huì)退回。


        配置模板引擎


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


        1. @Configuration

        2. @EnableWebMvc

        3. publicclassMvcWebConfigimplementsWebMvcConfigurer{


        4. @Autowired

        5. privateApplicationContext applicationContext;


        6. @Bean

        7. publicSpringResourceTemplateResolver templateResolver() {

        8. SpringResourceTemplateResolver templateResolver = newSpringResourceTemplateResolver();

        9. templateResolver.setApplicationContext(applicationContext);

        10. templateResolver.setPrefix("/WEB-INF/views/");

        11. templateResolver.setSuffix(".html");

        12. return templateResolver;

        13. }


        14. @Bean

        15. publicSpringTemplateEngine templateEngine() {

        16. SpringTemplateEngine templateEngine = newSpringTemplateEngine();

        17. templateEngine.setTemplateResolver(templateResolver());

        18. templateEngine.setEnableSpringELCompiler(true);

        19. return templateEngine;

        20. }


        21. @Override

        22. publicvoid configureViewResolvers(ViewResolverRegistry registry) {

        23. ThymeleafViewResolver resolver = newThymeleafViewResolver();

        24. resolver.setTemplateEngine(templateEngine());

        25. registry.viewResolver(resolver);

        26. }

        27. }


        SpringBoot1X只需要 spring-boot-starter-thymeleaf的依賴項(xiàng)來(lái)啟用 Web應(yīng)用程序中的 Thymeleaf支持。??但是由于 Thymeleaf3.0中的新功能,我們必須將 thymeleaf-layout-dialect 添加為 SpringBoot2XWeb應(yīng)用程序中的依賴項(xiàng)。配置好依賴,我們就可以將模板添加到 src/main/resources/templates文件夾中, SpringBoot將自動(dòng)顯示它們。


        Spring Security 配置


        為簡(jiǎn)單起見(jiàn),我們使用框架默認(rèn)的 HTTPBasic身份驗(yàn)證。讓我們首先看一下使用 Spring啟用 Security所需的依賴關(guān)系和配置。
        Spring首先需要依賴 spring-security-webspring-security-config 模塊。接下來(lái), 我們需要添加一個(gè)擴(kuò)展 WebSecurityConfigurerAdapter的類,并使用 @EnableWebSecurity注解:


        1. @Configuration

        2. @EnableWebSecurity

        3. publicclassCustomWebSecurityConfigurerAdapterextendsWebSecurityConfigurerAdapter{


        4. @Autowired

        5. publicvoid configureGlobal(AuthenticationManagerBuilder auth) throwsException{

        6. auth.inMemoryAuthentication()

        7. .withUser("admin")

        8. .password(passwordEncoder()

        9. .encode("password"))

        10. .authorities("ROLE_ADMIN");

        11. }


        12. @Override

        13. protectedvoid configure(HttpSecurity http) throwsException{

        14. http.authorizeRequests()

        15. .anyRequest().authenticated()

        16. .and()

        17. .httpBasic();

        18. }


        19. @Bean

        20. publicPasswordEncoder passwordEncoder() {

        21. returnnewBCryptPasswordEncoder();

        22. }

        23. }


        這里我們使用 inMemoryAuthentication來(lái)設(shè)置身份驗(yàn)證。同樣, SpringBoot也需要這些依賴項(xiàng)才能使其工作。但是我們只需要定義 spring-boot-starter-security的依賴關(guān)系,因?yàn)檫@會(huì)自動(dòng)將所有相關(guān)的依賴項(xiàng)添加到類路徑中。
        SpringBoot中的安全配置與上面的相同 。


        應(yīng)用程序啟動(dòng)引導(dǎo)配置


        SpringSpringBoot中應(yīng)用程序引導(dǎo)的基本區(qū)別在于 servlet。Spring使用 web.xmlSpringServletContainerInitializer作為其引導(dǎo)入口點(diǎn)。SpringBoot僅使用 Servlet3功能來(lái)引導(dǎo)應(yīng)用程序,下面讓我們?cè)敿?xì)來(lái)了解下


        Spring 引導(dǎo)配置


        Spring支持傳統(tǒng)的 web.xml引導(dǎo)方式以及最新的 Servlet3+方法。
        配置 web.xml方法啟動(dòng)的步驟
        Servlet容器(服務(wù)器)讀取 web.xml
        web.xml中定義的 DispatcherServlet由容器實(shí)例化
        DispatcherServlet通過(guò)讀取 WEB-INF/{servletName}-servlet.xml來(lái)創(chuàng)建 WebApplicationContext。最后, DispatcherServlet注冊(cè)在應(yīng)用程序上下文中定義的 bean
        使用 Servlet3+方法的 Spring啟動(dòng)步驟
        容器搜索實(shí)現(xiàn) ServletContainerInitializer的類并執(zhí)行 SpringServletContainerInitializer找到實(shí)現(xiàn)所有類 WebApplicationInitializer``WebApplicationInitializer創(chuàng)建具有XML或上下文 @ConfigurationWebApplicationInitializer創(chuàng)建 DispatcherServlet與先前創(chuàng)建的上下文。


        SpringBoot 引導(dǎo)配置


        Spring Boot應(yīng)用程序的入口點(diǎn)是使用@SpringBootApplication注釋的類


        1. @SpringBootApplication

        2. publicclassApplication{

        3. publicstaticvoid main(String[] args) {

        4. SpringApplication.run(Application.class, args);

        5. }

        6. }


        默認(rèn)情況下, SpringBoot使用嵌入式容器來(lái)運(yùn)行應(yīng)用程序。在這種情況下, SpringBoot使用 publicstaticvoidmain入口點(diǎn)來(lái)啟動(dòng)嵌入式 Web服務(wù)器。此外,它還負(fù)責(zé)將 ServletFilterServletContextInitializerbean從應(yīng)用程序上下文綁定到嵌入式 servlet容器。SpringBoot的另一個(gè)特性是它會(huì)自動(dòng)掃描同一個(gè)包中的所有類或 Main類的子包中的組件
        SpringBoot提供了將其部署到外部容器的方式。我們只需要擴(kuò)展 SpringBootServletInitializer即可:


        1. /**

        2. * War部署

        3. *

        4. * @author SanLi

        5. * Created by [email protected] on 2018/4/15

        6. */

        7. publicclassServletInitializerextendsSpringBootServletInitializer{


        8. @Override

        9. protectedSpringApplicationBuilder configure(SpringApplicationBuilder application) {

        10. return application.sources(Application.class);

        11. }


        12. @Override

        13. publicvoid onStartup(ServletContext servletContext) throwsServletException{

        14. super.onStartup(servletContext);

        15. servletContext.addListener(newHttpSessionEventPublisher());

        16. }

        17. }


        這里外部 servlet容器查找在war包下的 META-INF文件夾下MANIFEST.MF文件中定義的 Main-classSpringBootServletInitializer將負(fù)責(zé)綁定 Servlet, FilterServletContextInitializer


        打包和部署


        最后,讓我們看看如何打包和部署應(yīng)用程序。這兩個(gè)框架都支持 MavenGradle等通用包管理技術(shù)。但是在部署方面,這些框架差異很大。例如,Spring Boot Maven插件在 Maven中提供 SpringBoot支持。它還允許打包可執(zhí)行 jarwar包并 就地運(yùn)行應(yīng)用程序。
        在部署環(huán)境中 SpringBoot 對(duì)比 Spring的一些優(yōu)點(diǎn)包括:
        1、提供嵌入式容器支持
        2、使用命令java -jar獨(dú)立運(yùn)行jar
        3、在外部容器中部署時(shí),可以選擇排除依賴關(guān)系以避免潛在的jar沖突
        4、部署時(shí)靈活指定配置文件的選項(xiàng)
        5、用于集成測(cè)試的隨機(jī)端口生成


        結(jié)論


        簡(jiǎn)而言之,我們可以說(shuō) SpringBoot只是 Spring本身的擴(kuò)展,使開(kāi)發(fā),測(cè)試和部署更加方便。


        瀏覽 76
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            成人影视网址国产在线观看 | 日本xxxx在线观看 | 一对浑圆的胸乳被揉捏动态图 | 撕开奶罩揉吃奶高潮av在线 | 《波多野结衣之欲乱上班族》 | 亚洲AV成人无码18禁在线 | 国产午夜激情视频 | 日本护士多人吞精囗交视频 | 精油按摩做爰hd | 全黄一级裸片视频在线观看 |