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 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的一些特点:
org.springframework
spring-web
5.3.5
org.springframework
spring-webmvc
5.3.5
org.springframework.boot
spring-boot-starter-web
2.4.4
很简单对吧,springboot提供了一个starter来简化依赖。在starter中会将spring相关的依赖自动引入。Springboot提供了多种starter。
现在假设我们要创建一个JSP的web应用。分别通过Spring和Springboot进行配置。
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
@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类相同包或子包中的所有类,以寻找组件。