Spring Boot 是 Spring 的基础上提供的一套全新的开源框架,它能够简化 Spring 应用的搭建和开发过程。因此很多企业在开发的过程中都会使用它,那springboot怎么启动项目?下面来我们就来给大家讲解一下。
第一种启动方式 @EnableAutoConfiguration
@EnableAutoConfiguration 作用
开启自动装配,帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot,并创建对应配置类的Bean,并把该Bean实体交给IoC容器进行管理。
在使用springboot之前,我们需要先导入相关的jar包
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.spring</groupId> <artifactId>springBoot-demo1</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
新建文件 IndexController.java
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); } }
运行main方法,后即可启动项目了
.____ _ __ _ _ / \\/ ___'_ __ _ _(_)_ __ __ _ \ \ \ \ (()\ ___ | '_ | ' _ | | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / === === === | _ |= === === === === = | ___ /= /_/_ / _ / ::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 < java.lang.String, java.lang.Object >> 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
在浏览器输入网址访问 controller :http://localhost:8080/index 页面结果如下:
控制台也输出了打印的内容,是不是很简单呢?
第二种启动方式 @ComponentScan
@ComponentScan() 注解作用
根据定义的扫描路径,将符合规则的类加载到spring容器中,比如在类中加入了以下注解 @Controller、@Service、@Mapper 、@Component、@Configuration 等等;
接下来我们测试一下,创建启动类 App.java
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); } }
控制层 OrderController.java
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"; } }
启动后访问接口 http://localhost:8080/order
控制台打印结果
springboot启动项目我们可以使用@EnableAutoConfiguration以及@ComponentScan这两种方法,Spring Boot会根据项目依赖来自动配置Spring 框架,极大地减少项目要使用的配置。最后大家如果想要了解更多java架构师知识,敬请关注奇Q工具网。
推荐阅读: