Spring Boot和Spring的比较和区别
对于Spring和SpringBoot到底有什么区别,我听到了很多答案,刚开始迈入学习SpringBoot的我当时也是一头雾水,随着经验的积累、我慢慢理解了这两个框架到底有什么区别,我相信对于用了SpringBoot很久的开发人员来说,有绝大部分还不是很理解SpringBoot到底和Spring有什么区别,看完文章中的比较,或许你有了不同的答案和看法!
什么是Spring
先来聊一聊Spring作为Java开发人员,大家都Spring可不陌生,简而言之,Spring框架为开发Java应用程序提供了全面的基础架构支持。它包含一些很好的功能,如依赖注入和开箱即用的模块,如:
Spring JDBC 、Spring MVC 、Spring Security、 Spring AOP 、Spring ORM 、Spring Test
这些模块大家应该都用过吧,这些模块缩短应用程序的开发时间,提高了应用开发的效率
例如,在Java Web开发的早期阶段,我们需要编写大量的代码来将记录插入到数据源中。但是通过使用Spring JDBC模块的JDBCTemplate,我们可以将这操作简化为只需配置几行代码。
什么是Spring Boot
Spring Boot基本上是Spring框架的扩展,它消除了设置Spring应用程序所需的XML配置,为更快,更高效的开发生态系统铺平了道路。
以下是Spring Boot中的一些特点:
1:创建独立的spring应用。 2:嵌入Tomcat, Jetty Undertow 而且不需要部署他们。 3:提供的“starters” poms来简化Maven配置 4:尽可能自动配置spring应用。 5:提供生产指标,健壮检查和外部化配置 6:绝对没有代码生成和XML配置要求
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>
与Spring不同,Spring Boot只需要一个依赖项来启动和运行Web应用程序:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>
在进行构建期间,所有其他依赖项将自动添加到项目中。
另一个很好的例子就是测试库。我们通常使用Spring Test,JUnit,Hamcrest和Mockito库。在Spring项目中,我们应该将所有这些库添加为依赖项。但是在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
有关starter的完整列表,请查看Spring文档。
MVC配置
让我们来看一下Spring和Spring Boot创建JSP Web应用程序所需的配置。
Spring需要定义调度程序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将查看应用程序中存在的依赖项,属性和bean,并根据这些依赖项,对属性和bean进行配置。当然,如果我们想要添加自己的自定义配置,那么Spring Boot自动配置将会退回。
配置模板引擎
现在我们来看下如何在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应用程序中的 Thymeleaf支持。但是由于Thymeleaf3.0中的新功能, 我们必须将thymeleaf-layout-dialect 添加 为SpringBoot2XWeb应用程序中的依赖项。一旦依赖关系到位,我们就可以将模板添加到src/main/resources/templates文件夹中,SpringBoot将自动显示它们。
Spring Security的配置
为简单起见,我们使用框架默认的HTTP Basic身份验证。让我们首先看一下使用Spring启用Security所需的依赖关系和配置。
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来设置身份验证。同样,Spring Boot也需要这些依赖项才能使其工作。但是我们只需要定义spring-boot-starter-security的依赖关系,因为这会自动将所有相关的依赖项添加到类路径中。
Spring Boot中的安全配置与上面的相同。
打包和部署
最后,让我们看看如何打包和部署应用程序。这两个框架都支持Maven和Gradle等通用包管理技术。但是在部署方面,这些框架差异很大。例如,Spring Boot Maven插件在Maven中提供Spring Boot支持。它还允许打包可执行jar或war包并就地运行应用程序。
在部署环境中Spring Boot 对比Spring的一些优点包括:
提供嵌入式容器支持 使用命令java -jar独立运行jar 在外部容器中部署时,可以选择排除依赖关系以避免潜在的jar冲突 部署时灵活指定配置文件的选项 用于集成测试的随机端口生成
结论
我们可以说Spring Boot只是Spring本身的扩展,使开发,测试和部署更加方便。