点击上方“朱小厮的博客”,选择“
设为星标”
后台回复”
1024
“获取公众号专属1024GB资料
来源:uee.me/cNDC8
概述
对于
Spring
和
SpringBoot
到底有什么区别,我听到了很多答案,刚开始迈入学习
SpringBoot
的我当时也是一头雾水,随着经验的积累、我慢慢理解了这两个框架到底有什么区别,相信对于用了
SpringBoot
很久的同学来说,还不是很理解
SpringBoot
到底和
Spring
有什么区别,看完文章中的比较,或许你有了不同的答案和看法!
什么是Spring
作为
Java
开发人员,大家都
Spring
都不陌生,简而言之,
Spring
框架为开发
Java
应用程序提供了全面的基础架构支持。它包含一些很好的功能,如依赖注入和开箱即用的模块,如:
SpringJDBC、SpringMVC、SpringSecurity、SpringAOP、SpringORM、SpringTest
,这些模块缩短应用程序的开发时间,提高了应用开发的效率例如,在
JavaWeb
开发的早期阶段,我们需要编写大量的代码来将记录插入到数据库中。但是通过使用
SpringJDBC
模块的
JDBCTemplate
,我们可以将操作简化为几行代码
。整编:微信公众号,搜云库技术团队,ID:souyunku
什么是Spring Boot
SpringBoot
基本上是
Spring
框架的扩展,它消除了设置
Spring
应用程序所需的
XML配置
,为更快,更高效的开发生态系统铺平了道路。
SpringBoot
中的一些特征:
1、
创建独立的
Spring
应用。
2、
嵌入式
Tomcat
、
Jetty
、
Undertow
容器(无需部署war文件)。
3、
提供的
starters
简化构建配置
4、
尽可能自动配置
spring
应用。
5、
提供生产指标,例如指标、健壮检查和外部化配置
6、
完全没有代码生成和
XML
配置要求
从配置分析
Maven依赖
首先,让我们看一下使用Spring创建Web应用程序所需的最小依赖项
org.springframework
spring-web
5.1.0.RELEASE
org.springframework
spring-webmvc
5.1.0.RELEASE
与Spring不同,Spring Boot只需要一个依赖项来启动和运行Web应用程序:
org.springframework.boot
spring-boot-starter-web
2.0.6.RELEASE
在进行构建期间,所有其他依赖项将自动添加到项目中。
另一个很好的例子就是测试库。我们通常使用
SpringTest
,
JUnit
,
Hamcrest
和
Mockito
库。在
Spring
项目中,我们应该将所有这些库添加为依赖项。但是在
SpringBoot中
,我们只需要添加
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
有关
starter
的完整列表,请查看Spring文档。
MVC配置
让我们来看一下
Spring
和
SpringBoot
创建
JSPWeb
应用程序所需的配置。
Spring
需要定义调度程序
servlet
,映射和其他支持配置。我们可以使用
web.xml
文件或
Initializer
类来完成此操作:
publicclassMyWebAppInitializerimplementsWebApplicationInitializer{
@Override
publicvoid onStartup(ServletContext container) {
AnnotationConfigWebApplicationContext
context = newAnnotationConfigWebApplicationContext();
context.setConfigLocation("com.pingfangushi");
container.addListener(newContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = container
.addServlet("dispatcher", newDispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
还需要将
@EnableWebMvc
注释添加到
@Configuration
类,并定义一个视图解析器来解析从控制器返回的视图:
@EnableWebMvc
@Configuration
publicclassClientWebConfigimplementsWebMvcConfigurer{
@Bean
publicViewResolver viewResolver() {
InternalResourceViewResolver bean
= newInternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".jsp");
return bean;
}
}
再来看
SpringBoot
一旦我们添加了
Web
启动程序,
SpringBoot
只需要在
application
配置文件中配置几个属性来完成如上操作:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
上面的所有Spring配置都是通过一个名为auto-configuration的过程添加
Bootweb starter
来自动包含的。
这意味着
SpringBoot
将查看应用程序中存在的依赖项,属性和
bean
,并根据这些依赖项,对属性和
bean
进行配置。当然,如果我们想要添加自己的自定义配置,那么
SpringBoot
自动配置将会退回
。整编:微信公众号,搜云库技术团队,ID:souyunku
配置模板引擎
现在我们来看下如何在Spring和Spring Boot中配置Thymeleaf模板引擎。
在
Spring
中,我们需要为视图解析器添加
thymeleaf-spring5
依赖项和一些配置:
@Configuration
@EnableWebMvc
publicclassMvcWebConfigimplementsWebMvcConfigurer{
@Autowired
privateApplicationContext applicationContext;
@Bean
publicSpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = newSpringResourceTemplateResolver();
templateResolver.setApplicationContext(applicationContext);
templateResolver.setPrefix("/WEB-INF/views/");
templateResolver.setSuffix(".html");
return templateResolver;
}
@Bean
publicSpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = newSpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
@Override
publicvoid configureViewResolvers(ViewResolverRegistry registry) {
ThymeleafViewResolver resolver = newThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
registry.viewResolver(resolver);
}
}
SpringBoot1X
只需要
spring-boot-starter-thymeleaf
的依赖项来启用
Web
应用程序中的
Thymeleaf
支持。 但是由于
Thymeleaf3.0
中的新功能,我们必须将
thymeleaf-layout-dialect
添加为
SpringBoot2X
Web应用程序中的依赖项。配置好依赖,我们就可以将模板添加到
src/main/resources/templates
文件夹中,
SpringBoot
将自动显示它们。
Spring Security 配置
为简单起见,我们使用框架默认的
HTTPBasic
身份验证。让我们首先看一下使用
Spring
启用
Security
所需的依赖关系和配置。
Spring
首先需要依赖
spring-security-web
和
spring-security-config
模块。接下来, 我们需要添加一个扩展
WebSecurityConfigurerAdapter
的类,并使用
@EnableWebSecurity
注解:
@Configuration
@EnableWebSecurity
publicclassCustomWebSecurityConfigurerAdapterextendsWebSecurityConfigurerAdapter{
@Autowired
publicvoid configureGlobal(AuthenticationManagerBuilder auth) throwsException{
auth.inMemoryAuthentication()
.withUser("admin")
.password(passwordEncoder()
.encode("password"))
.authorities("ROLE_ADMIN");
}
@Override
protectedvoid configure(HttpSecurity http) throwsException{
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic();
}
@Bean
publicPasswordEncoder passwordEncoder() {
returnnewBCryptPasswordEncoder();
}
}
这里我们使用
inMemoryAuthentication
来设置身份验证。同样,
SpringBoot
也需要这些依赖项才能使其工作。但是我们只需要定义
spring-boot-starter-security
的依赖关系,因为这会自动将所有相关的依赖项添加到类路径中
。整编:微信公众号,搜云库技术团队,ID:souyunku
SpringBoot
中的安全配置与上面的相同
。
应用程序启动引导配置
Spring
和
SpringBoot
中应用程序引导的基本区别在于
servlet
。
Spring
使用
web.xml
或
SpringServletContainerInitializer
作为其引导入口点。
SpringBoot
仅使用
Servlet3
功能来引导应用程序,下面让我们详细来了解下
Spring 引导配置
Spring
支持传统的
web.xml
引导方式以及最新的
Servlet3+
方法。
配置
web.xml
方法启动的步骤
Servlet
容器(服务器)读取
web.xml
web.xml
中定义的
DispatcherServlet
由容器实例化
DispatcherServlet
通过读取
WEB-INF/{servletName}-servlet.xml
来创建
WebApplicationContext
。最后,
DispatcherServlet
注册在应用程序上下文中定义的
bean