SpringBoot:簡述SpringBoot和Spring的區別
一、Spring的介紹
簡而言之,Spring框架為開發Java應用程序提供了全面的基礎架構支持。它包含一些很好的功能,如依賴注入和開箱即用的模塊,如:
- Spring JDBC
- Spring MVC
- Spring Security
- Spring AOP
- Spring ORM
- Spring Test
這些模塊可以大大縮短應用程序的開發時間。例如,在Java Web開發的早期階段,我們需要編寫大量的重復代碼來將記錄插入到數據源中。但是通過使用Spring JDBC模塊的JDBCTemplate,我們可以將它簡化為只需幾個簡單配置或者幾行代碼。
二、SpringBoot的介紹
Spring Boot基本上是Spring框架的擴展,它消除了設置Spring應用程序所需的復雜例行配置。
它的目標和Spring的目標是一致的,為更快,更高效的開發生態系統鋪平了道路。以下是Spring Boot中的一些功能:
- 通過starter這一個依賴,以簡化構建和復雜的應用程序配置。
- 可以直接main函數啟動,嵌入式web服務器,避免了應用程序部署的復雜性,Metrics度量,Helth check健康檢查和外部化配置。
- 盡可能的自動化配置Spring功能。
三、Spring與SpringBoot的比較
3.1 Maven依賴
首先,讓我們看一下使用Spring創建Web應用程序所需的最小依賴項:
<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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
與Spring不同,Spring Boot只需要一個依賴項來啟動和運行Web應用程序:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
在構建期間,所有其他依賴項將自動添加到最終歸檔中。
Spring Boot為不同的Spring模塊提供了許多入門依賴項。一些最常用的是:
- spring-boot-starter-data-jpa
- spring-boot-starter-security
- spring-boot-starter-test
- spring-boot-starter-web
- spring-boot-starter-thymeleaf
3.2 MVC配置
下面來探討一下使用Spring和Spring Boot創建JSP Web應用程序所需的配置。
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override public void onStartup(ServletContext container) { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation("com.test.package"); container.addListener(new ContextLoaderListener(context)); ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(context)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
我們還需要將@EnableWebMvc注解添加到@Configuration注解類,並定義一個視圖解析器來解析從控制器返回的視圖:
@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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
與所有這些相比,一旦我們添加了Spring boot web starter,Spring Boot只需要一些屬性來使上面的事情正常工作:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
- 1
- 2
上面的所有Spring配置都是通過一個名為auto-configuration的進程添加Boot web starter來自動包含的。
這意味着Spring Boot將自動掃描應用程序中存在的依賴項,屬性和bean,並根據這些內容啟用相應的配置。
3.3 模板引擎配置
再來看看如何在Spring和Spring Boot中配置Thymeleaf模板引擎,兩者有啥區別?
在Spring中,我們需要為視圖解析器添加 thymeleaf-spring5依賴項和一些配置:
@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); }
}
- 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
Spring Boot 只需要spring-boot-starter-thymeleaf的依賴項 來啟用Web應用程序中的Thymeleaf支持。
一旦依賴關系添加成功后,我們就可以將模板添加到src / main / resources / templates文件夾中,Spring Boot將自動顯示它們。
3.4 安全配置
為簡單起見,我們將看到如何使用Spring和Spring Boot框架啟用默認的HTTP Basic身份驗證。
讓我們首先看一下使用Spring啟用Security所需的依賴關系和配置。
Spring需要標准的 spring-security-web和spring-security-config 依賴項來在應用程序中設置Security。
接下來, 我們需要添加一個擴展WebSecurityConfigurerAdapter的類,並使用@EnableWebSecurity注解:
@Configuration @EnableWebSecurity public 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(); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
同樣,Spring Boot也需要這些依賴項才能使其工作。但是我們只需要定義spring-boot-starter-security的依賴關系,它會自動將所有相關的依賴項添加到類路徑中。
3.5 應用引導Application Bootstrap
Spring和Spring Boot中應用程序引導的基本區別在於servlet。
Spring使用web.xml 或SpringServletContainerInitializer 作為其引導入口點。
spring boot僅僅使用Servlet 3來引導程序。
首先來說說spring引導
方法一:web.xml引導方法
- Servlet容器(服務器)讀取web.xml
- web.xml中定義的DispatcherServlet由容器實例化
- DispatcherServlet通過讀取WEB-INF / {servletName} -servlet.xml來創建WebApplicationContext
- 最后,DispatcherServlet注冊在應用程序上下文中定義的bean
方法二:servlet 3+引導方法
- 容器搜索實現ServletContainerInitializer的 類並執行
- SpringServletContainerInitializer找到實現類WebApplicationInitializer的子類
- WebApplicationInitializer創建會話使用XML或上下文@Configuration類
- WebApplicationInitializer創建DispatcherServlet,使用先前創建的上下文。
再來說說Spring Boot引導
Spring Boot應用程序的入口點是使用@SpringBootApplication注釋的類:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
默認情況下,Spring Boot使用嵌入式容器來運行應用程序。在這種情況下,Spring Boot使用public static void main入口點來啟動嵌入式Web服務器。
此外,它還負責將Servlet,Filter和ServletContextInitializer bean從應用程序上下文綁定到嵌入式servlet容器。
Spring Boot的另一個特性是它會自動掃描同一個包中的所有類或Main類的子包中的組件。
Spring Boot提供了將其部署為外部容器中的Web存檔的選項。在這種情況下,我們必須擴展SpringBootServletInitializer:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
// ...
}
- 1
- 2
- 3
- 4
外部Servlet容器查找在Web歸檔文件的META-INF文件中定義的Main-class,SpringBootServletInitializer將負責綁定Servlet,Filter和ServletContextInitializer。
3.6 打包和部署
最后,讓我們看看如何打包和部署應用程序。這兩個框架都支持Maven和Gradle等常見的包管理技術。但是在部署方面,這些框架差異很大。
例如,Spring Boot Maven插件在Maven中提供Spring Boot支持。它還允許打包可執行jar或war檔案並“就地”運行應用程序。
與spring相比,在部署環境中Spring Boot的一些優點包括
- 提供嵌入式容器支持
- 使用命令java -jar獨立運行jar
- 在外部容器中部署時,可以選擇排除依賴關系以避免潛在的jar沖突
- 部署時靈活指定配置文件的選項
- 用於集成測試的隨機端口生成
四、總結:
綜上所述:Spring Boot只是Spring本身的擴展,使開發,測試和部署更加方便。
</div>