在项目开发这一领域,大家普遍追求更简单、更高效的开发途径。以往,繁琐的配置文件成为了开发过程中的一个难题,但框架的问世则带来了极大的便利。它简化了操作流程,对开发者来说,真可称得上是天赐福音。
框架的出现背景
过去,项目运行需要众多配置文件,这一过程颇为费时费力。开发人员需在众多配置文件中进行调试和修改,耗时且成本高昂。正是在这种需求下,框架应运而生,彻底改变了开发模式。在许多开发团队中,成员们常被繁杂的配置文件拖累。这次变革不仅简化了配置文件的管理,还显著提升了开发效率。
技术进步催生了框架的诞生,众多项目纷纷开始试验这一新兴的解决途径。面对繁杂的配置难题,一些小型团队难以迅速推进项目,于是框架成了他们新的优选方案。
独立运行项目功能
框架具备独立执行项目的特性,这一功能极为实用。它以jar包的形式即可启动。应用场景丰富多样。比如,某些小型内部系统,无需繁琐的部署步骤。开发者只需将项目打包成jar包,便能在指定环境中顺利运行。对于资源有限的环境,这一特性尤为适宜。
这种独立运作的功能,能够加快各个模块的拆分与重组。设想一个由众多模块构成的大项目,若将某个子模块以框架形式制作成独立的jar包,那么无论是进行测试还是后续的集成工作,都会显得更为高效。
内嵌容器的优势
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.8.RELEASE
com.spring
springBoot-demo1
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
项目部署时,可以选择使用内嵌的容器,这样的设计使得部署过程更加便捷。我们不再需要将项目打包成war包进行部署。在企业级应用开发中,常常需要迅速将多个应用投入运行。若继续使用传统的war包部署方式,还需处理容器兼容性等一系列复杂问题。
package com.spring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@EnableAutoConfiguration
@RestController
public class IndexController {
// 访问路径 http://localhost:8080/index
@RequestMapping("/index")
public String index(){
System.out.println("我进来了");
return "index controller";
}
public static void main(String[] args) {
// 启动springboot
SpringApplication.run(IndexController.class,args);
}
}
例如,某电商平台的促销活动后台系统,在促销阶段会频繁切换功能模块。利用框架内置的容器,可以省去许多复杂的准备工作。不论是Tomcat还是Jetty,都能轻松应对各种应用场景。
简化Maven配置
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _
( ( )___ | '_ | '_| | '_ / _` |
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.8.RELEASE)
2021-06-03 20:37:26.010 INFO 942 --- [ main] com.spring.IndexController : Starting IndexController on xiexindongdeMacBook-Pro.local with PID 942 (/Users/yexindong/Documents/java/java_project/springBootdemo1/target/classes started by yexindong in /Users/yexindong/Documents/java/java_project)
2021-06-03 20:37:26.014 INFO 942 --- [ main] com.spring.IndexController : No active profile set, falling back to default profiles: default
2021-06-03 20:37:26.064 INFO 942 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@105fece7: startup date [Thu Jun 03 20:37:26 CST 2021]; root of context hierarchy
2021-06-03 20:37:27.329 INFO 942 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-06-03 20:37:27.365 INFO 942 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-06-03 20:37:27.365 INFO 942 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.37
2021-06-03 20:37:27.375 INFO 942 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/yexindong/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2021-06-03 20:37:27.474 INFO 942 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-06-03 20:37:27.474 INFO 942 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1413 ms
2021-06-03 20:37:27.520 INFO 942 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2021-06-03 20:37:27.523 INFO 942 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2021-06-03 20:37:27.523 INFO 942 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2021-06-03 20:37:27.523 INFO 942 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2021-06-03 20:37:27.523 INFO 942 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2021-06-03 20:37:27.619 INFO 942 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2021-06-03 20:37:27.750 INFO 942 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@105fece7: startup date [Thu Jun 03 20:37:26 CST 2021]; root of context hierarchy
2021-06-03 20:37:27.805 INFO 942 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/index]}" onto public java.lang.String com.spring.IndexController.index()
2021-06-03 20:37:27.808 INFO 942 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2021-06-03 20:37:27.808 INFO 942 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2021-06-03 20:37:27.825 INFO 942 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2021-06-03 20:37:27.825 INFO 942 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2021-06-03 20:37:27.938 INFO 942 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2021-06-03 20:37:27.986 INFO 942 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-06-03 20:37:27.990 INFO 942 --- [ main] com.spring.IndexController : Started IndexController in 2.649 seconds (JVM running for 3.063)
2021-06-03 20:37:40.475 INFO 942 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2021-06-03 20:37:40.475 INFO 942 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2021-06-03 20:37:40.491 INFO 942 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms
这个功能提供了推荐的基础POM文件,从而简化了Maven的配置过程,大大降低了配置所需的工作量。在具体项目的Maven管理中,许多配置项目都较为复杂,不易理解。此时,若能采用官方推荐的基准配置,就如同拥有了一个现成的模板。
新入职的程序员初涉复杂的Maven设置时会感到困惑,但有了这个简化版的配置文件,他们便能迅速掌握项目。众多开源项目若想吸引更多开发者加入,此类简化措施无疑起到了推动作用。
自动配置功能
自动配置功能让项目依赖变得直观易懂。框架可自动对项目依赖进行配置。若多个成员共同开发大型项目,各自引入的库若需大量手动调整,无疑是一场灾难。借助自动配置,项目所需配置大幅减少,出错的可能性也相应降低。
该社交网络开发项目涉及多种技术背景的开发者,他们通过自动配置手段,使得各个模块可以迅速结合,无需担忧配置上的杂乱无章。
生产就绪型功能
package com.spring.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@EnableAutoConfiguration // 开启自动装配
@ComponentScan("com.spring.controller")
public class App {
public static void main(String[] args) {
// 启动springboot
SpringApplication.run(App.class,args);
}
}
框架所提供的功能可直接应用于生产环境,这些功能十分关键。比如性能指标、应用信息和健康检查等。在庞大的互联网生产环境中,任何微小的异常都可能导致严重后果。
金融交易平台需确保系统状况可即时查询。若缺乏此类成熟功能,从头开始开发,工作量将极为庞大。
package com.spring.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@RequestMapping("/order")
public String order(){
System.out.println("我进来了 order controller 的 index 方法");
return "order controller";
}
}
是否曾尝试过采用框架来构建项目?若愿意,不妨在评论区交流你的心得体会。觉得本文对你有帮助的话,不妨点个赞并转发一下。