Spring和 Springboot的区别你了解吗

什么是Spring

The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform.

简单来说Spring framework为基于Java的企业应用程序提供了全面的编程和配置模型,并支持在任何部署平台上使用。Spring framework的一个关键要素是应用程序级别的基础设施支持。

他包含了很多非常棒的功能,比如依赖注入,还有一些拿来即用的依赖,通过这些以来,可以加快我们的开发效率。比如:

  • Spring JDBC
  • Spring MVC
  • Spring Security
  • Spring AOP
  • Spring ORM
  • Spring Test

什么是Springboot

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

简单来说Spring Boot使得创建独立的、生产级别的基于Spring的应用程序变得容易,你只需“运行“。理念即使约定大于配置。大多数Spring Boot应用程序只需要最少的Spring配置。

Springboot可以理解为对Spring的一种扩展。以下是Springboot的一些特点:

  • 创建独立的Spring应用程序
  • 直接嵌入Tomcat、Jetty或Undertow(无需部署WAR文件)
  • 提供“starter”依赖项,简化构建配置
  • 尽可能自动配置Spring和第三方库
  • 提供如指标、健康检查和将配置和代码分离等功能
  • 不需要XML配置

Maven依赖

Spring framework 


    org.springframework
    spring-web
    5.3.5


    org.springframework
    spring-webmvc
    5.3.5

Springboot


    org.springframework.boot
    spring-boot-starter-web
    2.4.4

很简单对吧,springboot提供了一个starter来简化依赖。在starter中会将spring相关的依赖自动引入。Springboot提供了多种starter。

  • spring-boot-starter-data-jpa
  • spring-boot-starter-security
  • spring-boot-starter-test
  • spring-boot-starter-web
  • spring-boot-starter-thymeleaf

MVC配置

现在假设我们要创建一个JSP的web应用。分别通过Spring和Springboot进行配置。

Spring

Spring需要配置dispatcher servlet,mapping等,同时需要配置视图解析器并且增加响应的注解。

public class MyWebAppInitializer implements WebApplicationInitializer {
 
    @Override
    public void onStartup(ServletContext container) {
        AnnotationConfigWebApplicationContext context
          = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation("com.csdn");
 
        container.addListener(new ContextLoaderListener(context));
 
        ServletRegistration.Dynamic dispatcher = container
          .addServlet("dispatcher", new DispatcherServlet(context));
         
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

@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只需要增加两三行配置即可。其他所有的配置都被自动配置。也就是约定大于配置。

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

启动

Spring

  1. Servlet容器(服务器)读取web.xml文件。
  2. 容器实例化在web.xml中定义的DispatcherServlet。
  3. DispatcherServlet通过读取WEB-INF/{servletName}-servlet.xml文件创建WebApplicationContext。
  4. 最后,DispatcherServlet将应用程序上下文中定义的Bean注册到Servlet容器中

Springboot

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

默认情况下,Spring Boot使用内嵌的容器来运行应用程序。它还负责将应用程序上下文中的Servlet、Filter和ServletContextInitializer bean绑定到内嵌的Servlet容器中。Spring Boot的另一个特性是它自动扫描与Main类相同包或子包中的所有类,以寻找组件。

你可能感兴趣的:(spring,spring,boot,java)