今天本篇文章主要聚焦說說,spring boot和spring究竟有啥區別,重點對比MVC模塊以及Security模塊在兩大框架使用時的區別。html
簡而言之,Spring框架爲開發Java應用程序提供了全面的基礎架構支持。java
它包含一些很好的功能,如依賴注入和開箱即用的模塊,如:程序員
這些模塊能夠大大縮短應用程序的開發時間。web
例如,在Java Web開發的早期階段,咱們須要編寫大量的重複代碼來將記錄插入到數據源中。可是經過使用Spring JDBC模塊的JDBCTemplate,咱們能夠將它簡化爲只需幾個簡單配置或者幾行代碼。面試
Spring Boot基本上是Spring框架的擴展,它消除了設置Spring應用程序所需的複雜例行配置。spring
它的目標和Spring的目標是一致的,爲更快,更高效的開發生態系統鋪平了道路。安全
如下是Spring Boot中的一些功能:服務器
下面進一步詳述一下二者的區別:架構
首先,讓咱們看一下使用Spring建立Web應用程序所需的最小依賴項:mvc
<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應用程序:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
在構建期間,全部其餘依賴項將自動添加到最終歸檔中。
另外一個很好的例子是測試庫。咱們一般使用Spring Test,JUnit,Hamcrest和Mockito庫集。在Spring項目中,咱們應該將全部這些庫添加爲依賴項。
可是在Spring Boot中,咱們只須要用於測試的啓動器依賴項來自動包含這些庫。
Spring Boot爲不一樣的Spring模塊提供了許多入門依賴項。一些最經常使用的是:
下面來探討一下使用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("/");
}
}
咱們還須要將@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;
}
}
與全部這些相比,一旦咱們添加了Spring boot web starter,Spring Boot只須要一些屬性來使上面的事情正常工做:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
上面的全部Spring配置都是經過一個名爲auto-configuration的進程添加Boot web starter來自動包含的。
這意味着Spring Boot將自動掃描應用程序中存在的依賴項,屬性和bean,並根據這些內容啓用相應的配置。
再來看看如何在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);
}
}
Spring Boot 只須要spring-boot-starter-thymeleaf的依賴項 來啓用Web應用程序中的Thymeleaf支持。
一旦依賴關係添加成功後,咱們就能夠將模板添加到src / main / resources / templates文件夾中,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();
}
}
一樣,Spring Boot也須要這些依賴項才能使其工做。可是咱們只須要定義spring-boot-starter-security的依賴關係,它會自動將全部相關的依賴項添加到類路徑中。
Spring和Spring Boot中應用程序引導的基本區別在於servlet。
Spring使用web.xml 或SpringServletContainerInitializer 做爲其引導入口點。
spring boot僅僅使用Servlet 3來引導程序
來講說spring引導
web.xml引導方法
servlet 3+引導方法
再來講說spring boot引導
Spring Boot應用程序的入口點是使用@SpringBootApplication註釋的類:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
默認狀況下,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 {
// ...
}
外部servlet容器查找在Web歸檔文件的META-INF文件中定義的Main-class,SpringBootServletInitializer將負責綁定Servlet,Filter和ServletContextInitializer。
最後,讓咱們看看如何打包和部署應用程序。這兩個框架都支持Maven和Gradle等常見的包管理技術。可是在部署方面,這些框架差別很大。
例如,Spring Boot Maven插件在Maven中提供Spring Boot支持。它還容許打包可執行jar或war檔案並「就地」運行應用程序。
與spring相比,在部署環境中Spring Boot的一些優勢包括
本篇,講述了Spring boot和Spring之間的區別。用一句話概況就是:Spring Boot只是Spring自己的擴展,使開發,測試和部署更加方便。
牛皮了,馬士兵老師全網首播阿里P8級技術、實現大型淘寶實戰落
面試美團被JVM慘虐?阿里P9架構師用500分鐘把JVM從入門講到實戰#合集
清華啓蒙架構師馬士兵針對應屆生到開發十年的Java程序員作職業把脈