天天在說Spring和SpringBoot, 這些區(qū)別我 終于理清楚了
點擊上方 Java學習之道,選擇 設(shè)為星標
來源: blog.51cto.com/u_13981400/2376587
Part1
對于Spring和SpringBoot到底有什么區(qū)別,我聽到了很多答案,剛開始邁入學習SpringBoot的我當時也是一頭霧水,隨著經(jīng)驗的積累、我慢慢理解了這兩個框架到底有什么區(qū)別,我相信對于用了SpringBoot很久的開發(fā)人員來說,有絕大部分還不是很理解SpringBoot到底和Spring有什么區(qū)別,看完文章中的比較,或許你有了不同的答案和看法!
Part2什么是Spring
先來聊一聊Spring作為Java開發(fā)人員,大家都Spring可不陌生,簡而言之,Spring框架為開發(fā)Java應(yīng)用程序提供了全面的基礎(chǔ)架構(gòu)支持。
它包含一些很好的功能,如依賴注入和開箱即用的模塊,如:
Spring JDBC Spring MVC Spring Security Spring AOP Spring ORM Spring Test
這些模塊大家應(yīng)該都用過吧,這些模塊縮短應(yīng)用程序的開發(fā)時間,提高了應(yīng)用開發(fā)的效率
例如,在Java Web開發(fā)的早期階段,我們需要編寫大量的代碼來將記錄插入到數(shù)據(jù)源中。但是通過使用Spring JDBC模塊的JDBCTemplate,我們可以將這操作簡化為只需配置幾行代碼。
Part3什么是Spring Boot
pring Boot基本上是Spring框架的擴展,它消除了設(shè)置Spring應(yīng)用程序所需的XML配置,為更快,更高效的開發(fā)生態(tài)系統(tǒng)鋪平了道路。
以下是Spring Boot中的一些特點:
創(chuàng)建獨立的spring應(yīng)用。 嵌入Tomcat, Jetty Undertow 而且不需要部署他們。 提供的“starters” poms來簡化Maven配置 盡可能自動配置spring應(yīng)用。 提供生產(chǎn)指標,健壯檢查和外部化配置 絕對沒有代碼生成和XML配置要求
Part4逐步熟悉這兩個框架
Maven依賴
<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>
與Spring不同,Spring Boot只需要一個依賴項來啟動和運行Web應(yīng)用程序:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>
在進行構(gòu)建期間,所有其他依賴項將自動添加到項目中。
另一個很好的例子就是測試庫。我們通常使用Spring Test,JUnit,Hamcrest和Mockito庫。在Spring項目中,我們應(yīng)該將所有這些庫添加為依賴項。但是在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
MVC配置
讓我們來看一下Spring和Spring Boot創(chuàng)建JSP Web應(yīng)用程序所需的配置。
Spring需要定義調(diào)度程序servlet,映射和其他支持配置。我們可以使用 web.xml 文件或Initializer類來完成此操作:
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.pingfangushi");
container.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = container
.addServlet("dispatcher", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
還需要將@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;
}
}
上述操作一比,一旦我們添加了Web啟動程序,Spring Boot只需要在application配置文件中配置幾個屬性來完成如上操作:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
上面的所有Spring配置都是通過一個名為auto-configuration的過程添加Boot web starter來自動包含的。
這意味著Spring Boot將查看應(yīng)用程序中存在的依賴項,屬性和bean,并根據(jù)這些依賴項,對屬性和bean進行配置。當然,如果我們想要添加自己的自定義配置,那么Spring Boot自動配置將會退回。
配置模板引擎
現(xiàn)在我們來看下如何在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);
}
}
SpringBoot1X只需要spring-boot-starter-thymeleaf的依賴 項 來啟用Web應(yīng)用程序中的 Thymeleaf支持。但是由于Thymeleaf3.0中的新功能, 我們必須將thymeleaf-layout-dialect 添加 為SpringBoot2XWeb應(yīng)用程序中的依賴項。一旦依賴關(guān)系到位,我們就可以將模板添加到src/main/resources/templates文件夾中,SpringBoot將自動顯示它們。
Spring Security 配置
為簡單起見,我們使用框架默認的HTTP Basic身份驗證。讓我們首先看一下使用Spring啟用Security所需的依賴關(guān)系和配置。Spring首先需要依賴 spring-security-web和spring-security-config 模塊。接下來, 我們需要添加一個擴展WebSecurityConfigurerAdapter的類,并使用@EnableWebSecurity注解:
@Configuration
@EnableWebSecurity
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin")
.password(passwordEncoder()
.encode("password"))
.authorities("ROLE_ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
這里我們使用inMemoryAuthentication來設(shè)置身份驗證。同樣,Spring Boot也需要這些依賴項才能使其工作。但是我們只需要定義spring-boot-starter-security的依賴關(guān)系,因為這會自動將所有相關(guān)的依賴項添加到類路徑中。
Spring Boot中的安全配置與上面的相同。
Part5應(yīng)用程序引導配置
Spring和Spring Boot中應(yīng)用程序引導的基本區(qū)別在于servlet。
Spring使用 web.xml 或 SpringServletContainerInitializer 作為其引導入口點。
Spring Boot僅使用 Servlet 3 功能來引導應(yīng)用程序,下面讓我們詳細來了解下
Spring 是怎樣引導配置的
Spring支持傳統(tǒng)的web.xml引導方式以及最新的Servlet 3+方法。
讓我們看一下 web.xml方法的步驟:
Servlet容器(服務(wù)器)讀取web.xml web.xml中定義的DispatcherServlet由容器實例化 DispatcherServlet通過讀取WEB-INF / {servletName} -servlet.xml來創(chuàng)建WebApplicationContext 最后,DispatcherServlet注冊在應(yīng)用程序上下文中定義的bean
以下是使用Servlet 3+方法的Spring引導:
容器搜索實現(xiàn)ServletContainerInitializer的類并執(zhí)行 SpringServletContainerInitializer找到實現(xiàn)所有類WebApplicationInitializer WebApplicationInitializer創(chuàng)建具有XML或上下文@Configuration類 WebApplicationInitializer創(chuàng)建DispatcherServlet的 與先前創(chuàng)建的上下文。
SpringBoot 有是如何配置的
Spring Boot應(yīng)用程序的入口點是使用@SpringBootApplication注釋的類:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
默認情況下,Spring Boot使用嵌入式容器來運行應(yīng)用程序。在這種情況下,Spring Boot使用public static void main入口點來啟動嵌入式Web服務(wù)器。此外,它還負責將Servlet,F(xiàn)ilter和ServletContextInitializer bean從應(yīng)用程序上下文綁定到嵌入式servlet容器。Spring Boot的另一個特性是它會自動掃描同一個包中的所有類或Main類的子包中的組件。Spring Boot提供了將其部署到外部容器的方式。在這種情況下,我們必須擴展SpringBootServletInitializer:
/**
* War部署
*
* @author SanLi
* Created by [email protected] on 2018/4/15
*/
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.addListener(new HttpSessionEventPublisher());
}
}
這里外部servlet容器查找在war包下的META-INF文件夾下MANIFEST.MF文件中定義的Main-class,SpringBootServletInitializer將負責綁定Servlet,F(xiàn)ilter和ServletContextInitializer。
Part6打包和部署
最后,讓我們看看如何打包和部署應(yīng)用程序。這兩個框架都支持Maven和Gradle等通用包管理技術(shù)。但是在部署方面,這些框架差異很大。例如,Spring Boot Maven插件在Maven中提供Spring Boot支持。它還允許打包可執(zhí)行jar或war包并就地運行應(yīng)用程序。在部署環(huán)境中Spring Boot 對比Spring的一些優(yōu)點包括:
提供嵌入式容器支持 使用命令java -jar獨立運行jar 在外部容器中部署時,可以選擇排除依賴關(guān)系以避免潛在的jar沖突 部署時靈活指定配置文件的選項 用于集成測試的隨機端口生成
簡而言之,我們可以說Spring Boot只是Spring本身的擴展,使開發(fā),測試和部署更加方便。
-
| 更多精彩文章 -
▽加我微信,交個朋友 長按/掃碼添加↑↑↑



