Spring和SpringBoot 的區(qū)別是什么?

作者:云揚(yáng)四海 來源:cnblogs.com/rolandlee/p/11343848.html
對(duì)于Spring和SpringBoot到底有什么區(qū)別,我聽到了很多答案,剛開始邁入學(xué)習(xí)SpringBoot的我當(dāng)時(shí)也是一頭霧水,隨著經(jīng)驗(yàn)的積累、我慢慢理解了這兩個(gè)框架到底有什么區(qū)別,相信對(duì)于用了SpringBoot很久的同學(xué)來說,還不是很理解SpringBoot到底和Spring有什么區(qū)別,看完文章中的比較,或許你有了不同的答案和看法!
什么是Spring
作為Java開發(fā)人員,大家都Spring都不陌生,簡(jiǎn)而言之,Spring框架為開發(fā)Java應(yīng)用程序提供了全面的基礎(chǔ)架構(gòu)支持。它包含一些很好的功能,如依賴注入和開箱即用的模塊,如:Spring JDBC 、Spring MVC 、Spring Security、 Spring AOP 、Spring ORM 、Spring Test,這些模塊縮短應(yīng)用程序的開發(fā)時(shí)間,提高了應(yīng)用開發(fā)的效率例如,在Java Web開發(fā)的早期階段,我們需要編寫大量的代碼來將記錄插入到數(shù)據(jù)庫中。但是通過使用Spring JDBC模塊的JDBCTemplate,我們可以將操作簡(jiǎn)化為幾行代碼。
什么是Spring Boot
Spring Boot基本上是Spring框架的擴(kuò)展,它消除了設(shè)置Spring應(yīng)用程序所需的XML配置,為更快,更高效的開發(fā)生態(tài)系統(tǒng)鋪平了道路。
Spring Boot中的一些特征:
1、 創(chuàng)建獨(dú)立的Spring應(yīng)用。
2、 嵌入式Tomcat、Jetty、 Undertow容器(無需部署war文件)。
3、 提供的starters 簡(jiǎn)化構(gòu)建配置
4、 盡可能自動(dòng)配置spring應(yīng)用。
5、 提供生產(chǎn)指標(biāo),例如指標(biāo)、健壯檢查和外部化配置
6、 完全沒有代碼生成和XML配置要求
從配置分析
Maven依賴
首先,讓我們看一下使用Spring創(chuàng)建Web應(yīng)用程序所需的最小依賴項(xiàng)
<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只需要一個(gè)依賴項(xiàng)來啟動(dòng)和運(yùn)行Web應(yīng)用程序:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>
在進(jìn)行構(gòu)建期間,所有其他依賴項(xiàng)將自動(dòng)添加到項(xiàng)目中。
另一個(gè)很好的例子就是測(cè)試庫。我們通常使用Spring Test,JUnit,Hamcrest和Mockito庫。在Spring項(xiàng)目中,我們應(yīng)該將所有這些庫添加為依賴項(xiàng)。但是在Spring Boot中,我們只需要添加spring-boot-starter-test依賴項(xiàng)來自動(dòng)包含這些庫。
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配置
讓我們來看一下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類,并定義一個(gè)視圖解析器來解析從控制器返回的視圖:
@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;
}
}
再來看SpringBoot一旦我們添加了Web啟動(dòng)程序,Spring Boot只需要在application配置文件中配置幾個(gè)屬性來完成如上操作:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
上面的所有Spring配置都是通過一個(gè)名為auto-configuration的過程添加Boot web starter來自動(dòng)包含的。
這意味著Spring Boot將查看應(yīng)用程序中存在的依賴項(xiàng),屬性和bean,并根據(jù)這些依賴項(xiàng),對(duì)屬性和bean進(jìn)行配置。當(dāng)然,如果我們想要添加自己的自定義配置,那么Spring Boot自動(dòng)配置將會(huì)退回。
配置模板引擎
現(xiàn)在我們來看下如何在Spring和Spring Boot中配置Thymeleaf模板引擎。
在Spring中,我們需要為視圖解析器添加thymeleaf-spring5依賴項(xiàng)和一些配置:
@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的依賴項(xiàng)來啟用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)單起見,我們使用框架默認(rèn)的HTTP Basic身份驗(yàn)證。讓我們首先看一下使用Spring啟用Security所需的依賴關(guān)系和配置。
Spring首先需要依賴 spring-security-web和spring-security-config 模塊。接下來, 我們需要添加一個(gè)擴(kuò)展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è)置身份驗(yàn)證。同樣,Spring Boot也需要這些依賴項(xiàng)才能使其工作。但是我們只需要定義spring-boot-starter-security的依賴關(guān)系,因?yàn)檫@會(huì)自動(dòng)將所有相關(guān)的依賴項(xiàng)添加到類路徑中。
Spring Boot中的安全配置與上面的相同 。
應(yīng)用程序啟動(dòng)引導(dǎo)配置
Spring和Spring Boot中應(yīng)用程序引導(dǎo)的基本區(qū)別在于servlet。Spring使用web.xml 或SpringServletContainerInitializer作為其引導(dǎo)入口點(diǎn)。Spring Boot僅使用Servlet 3功能來引導(dǎo)應(yīng)用程序,下面讓我們?cè)敿?xì)來了解下
Spring 引導(dǎo)配置
Spring支持傳統(tǒng)的web.xml引導(dǎo)方式以及最新的Servlet 3+方法。
配置web.xml方法啟動(dòng)的步驟
Servlet容器(服務(wù)器)讀取web.xml
web.xml中定義的DispatcherServlet由容器實(shí)例化
DispatcherServlet通過讀取WEB-INF / {servletName} -servlet.xml來創(chuàng)建WebApplicationContext。最后,DispatcherServlet注冊(cè)在應(yīng)用程序上下文中定義的bean
使用Servlet 3+方法的Spring啟動(dòng)步驟
容器搜索實(shí)現(xiàn)ServletContainerInitializer的類并執(zhí)行SpringServletContainerInitializer找到實(shí)現(xiàn)所有類WebApplicationInitializer``WebApplicationInitializer創(chuàng)建具有XML或上下文@Configuration類WebApplicationInitializer創(chuàng)建DispatcherServlet與先前創(chuàng)建的上下文。
SpringBoot 引導(dǎo)配置
Spring Boot應(yīng)用程序的入口點(diǎn)是使用@SpringBootApplication注釋的類
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
默認(rèn)情況下,Spring Boot使用嵌入式容器來運(yùn)行應(yīng)用程序。在這種情況下,Spring Boot使用public static void main入口點(diǎn)來啟動(dòng)嵌入式Web服務(wù)器。此外,它還負(fù)責(zé)將Servlet,Filter和ServletContextInitializer bean從應(yīng)用程序上下文綁定到嵌入式servlet容器。Spring Boot的另一個(gè)特性是它會(huì)自動(dòng)掃描同一個(gè)包中的所有類或Main類的子包中的組件。
Spring Boot提供了將其部署到外部容器的方式。我們只需要擴(kuò)展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將負(fù)責(zé)綁定Servlet,Filter和ServletContextInitializer。
打包和部署
最后,讓我們看看如何打包和部署應(yīng)用程序。這兩個(gè)框架都支持Maven和Gradle等通用包管理技術(shù)。但是在部署方面,這些框架差異很大。例如,Spring Boot Maven插件在Maven中提供Spring Boot支持。它還允許打包可執(zhí)行jar或war包并就地運(yùn)行應(yīng)用程序。
在部署環(huán)境中Spring Boot 對(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)而言之,我們可以說Spring Boot只是Spring本身的擴(kuò)展,使開發(fā),測(cè)試和部署更加方便。

