项目结构

项目基于Maven管理,注意使用了父pom

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.3.0.RELEASE</version>
  5. <relativePath />
  6. </parent>

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4.  
  5. <groupId>test.demo</groupId>
  6. <artifactId>springboot</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9.  
  10. <name>springboot</name>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>1.3.0.RELEASE</version>
  15. <relativePath />
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. </properties>
  20.  
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. </dependencies>
  27. </project>

  App.class

  1. package hello;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RestController;
  8.  
  9. @EnableAutoConfiguration
  10. @RestController
  11. public class App
  12. {
  13. public static void main(String[] args)
  14. {
  15. SpringApplication.run(App.class, args);
  16. }
  17.  
  18. @RequestMapping(value = "/test", method = RequestMethod.GET)
  19. public String get()
  20. {
  21. return "get";
  22. }
  23.  
  24. @RequestMapping(value = "/post", method = RequestMethod.POST)
  25. public String post()
  26. {
  27. return "post";
  28. }
  29. }

@EnableAutoConfiguration注解的作用在于让 Spring Boot 根据应用所声明的依赖来对 Spring 框架进行自动配置

@EnableAutoConfiguration注解上注释的翻译

自动配置Spring上下文,企图猜测所需要的Bean,将classpath和你所定义的Bean加入到IOC容器,比如你有tomcat-embedded.jar在你的classpath上

那么看起来你需要一个TomcatEmbeddedServletContainerFactory,那么SpringBoot会给你生成一个这个类的Bean(除非你已经生成这个类的Bean)

你可以使用exclude来指定那些Bean不需要SpringBoot帮你生成Bean

  1. public @interface EnableAutoConfiguration {
  2.  
  3. /**
  4. * Exclude specific auto-configuration classes such that they will never be applied.
  5. * @return the classes to exclude
  6. */
  7. Class<?>[] exclude() default {};
  8.  
  9. /**
  10. * Exclude specific auto-configuration class names such that they will never be
  11. * applied.
  12. * @return the class names to exclude
  13. * @since 1.3.0
  14. */
  15. String[] excludeName() default {};
  16.  
  17. }

在你的root package上使用这个注解,它会将这个目录下所有的子目录和类扫描一遍

@RestController和@RequestMapper注解由SpringMvc提供,用于创建Rest服务

关于@RequestMapper注解

value表示路径映射,method说明请求这个路径的Http方法

启动日志解读

  1. 2016-03-11 13:58:02.399 INFO 5776 --- [ main] hello.App : Starting App on zzzzzz with PID 5776 (E:\space\spring\springboot\target\classes started by zzzzz in E:\space\spring\springboot)
  2. 2016-03-11 13:58:02.404 INFO 5776 --- [ main] hello.App : No profiles are active
  3. 2016-03-11 13:58:02.508 INFO 5776 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@481a15ff: startup date [Fri Mar 11 13:58:02 CST 2016]; root of context hierarchy
  4. 2016-03-11 13:58:03.807 INFO 5776 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
  5. 2016-03-11 13:58:05.107 INFO 5776 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
  6. 2016-03-11 13:58:05.126 INFO 5776 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
  7. 2016-03-11 13:58:05.128 INFO 5776 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.28
  8. 2016-03-11 13:58:05.268 INFO 5776 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  9. 2016-03-11 13:58:05.268 INFO 5776 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2769 ms
  10. 2016-03-11 13:58:05.673 INFO 5776 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
  11. 2016-03-11 13:58:05.680 INFO 5776 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
  12. 2016-03-11 13:58:05.681 INFO 5776 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
  13. 2016-03-11 13:58:05.681 INFO 5776 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
  14. 2016-03-11 13:58:05.682 INFO 5776 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
  15. 2016-03-11 13:58:06.022 INFO 5776 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@481a15ff: startup date [Fri Mar 11 13:58:02 CST 2016]; root of context hierarchy
  16. 2016-03-11 13:58:06.115 INFO 5776 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test],methods=[GET]}" onto public java.lang.String hello.App.get()
  17. 2016-03-11 13:58:06.116 INFO 5776 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/post],methods=[POST]}" onto public java.lang.String hello.App.post()
  18. 2016-03-11 13:58:06.121 INFO 5776 --- [ 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.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
  19. 2016-03-11 13:58:06.121 INFO 5776 --- [ 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.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
  20. 2016-03-11 13:58:06.178 INFO 5776 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  21. 2016-03-11 13:58:06.178 INFO 5776 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  22. 2016-03-11 13:58:06.225 INFO 5776 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  23. 2016-03-11 13:58:06.363 INFO 5776 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
  24. 2016-03-11 13:58:06.468 INFO 5776 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
  25. 2016-03-11 13:58:06.473 INFO 5776 --- [ main] hello.App : Started App in 4.736 seconds (JVM running for 5.798)

 注意查看红色的部分,dispatcherServlet是SpringWeb的核心Servlet,所有进入SpringWeb的请求都由这个Servelt的service方法进入

  1. 2016-03-11 13:58:06.115 INFO 5776 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test],methods=[GET]}" onto public java.lang.String hello.App.get()
  2. 2016-03-11 13:58:06.116 INFO 5776 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/post],methods=[POST]}" onto public java.lang.String hello.App.post()
    表示我们声明的路径映射

一个最小化的SpringBoot项目的更多相关文章

  1. 实现iOS图片等资源文件的热更新化(四): 一个最小化的补丁更新逻辑

    简介 以前写过一个补丁更新的文章,此处会做一个更精简的最小化实现,以便于集成.为了使逻辑具有通用性,将剥离对AFNetworking和ReativeCocoa的依赖.原来的文章,可以先看这里: htt ...

  2. 快速搭建一个restful风格的springboot项目

    1.创建一个工程. 2.引入pom.xml依赖,如下 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...

  3. IntelliJ IDEA 2017.3 搭建一个多模块的springboot项目(二)

    上一篇我成功搭建了一个项目,名叫bale-project,下面我们继续搭建子模块. 在项目名称上右键,New->Module,新建一个模块. 这次我们选择Spring Initializr 起个 ...

  4. 关于吴恩达机器学习支持向量机的问题,讲到对偶前有一个最小化f(w)使用拉格朗日求解时转化成一个最大的相等式的理解和一些困惑

    (纯属个人理解) 参考: https://www.zhihu.com/question/267482928 https://www.cnblogs.com/90zeng/p/Lagrange_dual ...

  5. IntelliJ IDEA 2017.3 搭建一个多模块的springboot项目(一)

    新人接触springboot,IDE使用的是IntelliJ IDEA 2017.3 ,自己摸索了很久,现在自己整理一下,里面有些操作我自己也不懂是为什么这样,只是模仿公司现有的项目,自己搭建了一个简 ...

  6. Minus-C 一个最小化的C语言规范

    资深C++程序员都不会对C++编程规范太陌生,C++实在太复杂,以至于所有项目都需要裁剪一个子集共项目组内使用.经过在家休息这一小段时间,我发现其实C语言更需要一个相同的规范,这就是本文的目标,最大可 ...

  7. IntelliJ IDEA 2017.3 搭建一个多模块的springboot项目(三)

    你得先看完前两篇文章才可以进行第三章.这章我又新建了一个模块,起名叫project-core.建立方式与(一)里面一致,一个普通的maven模块.不需要勾选archetype的那种. 大家自己建好后, ...

  8. 分享我自己的一个最小化安装CentOS6的初始化脚本

    在自己的虚拟机上使用的基于CentOS6的系统初始化脚本 #!/bin/bash # #Filename:system_init.sh #Description:系统安装完成后,对系统进行一些配置,以 ...

  9. springboot项目配置logback日志系统

    记录springboot项目配置logback日志文件管理: logback依赖jar包 SpringBoot项目配置logback理论上需要添加logback-classic依赖jar包: < ...

随机推荐

  1. 《Java中的包机制》

    /* 包的机制:(1) */ package lee; public class PackageTest { public void Test(int num) { System.out.printl ...

  2. MySQL数据库1 - 基本概念及安装

    一.数据管理技术的产生和发展: 1.人工管理阶段 - 效率低,成本高(文字) 2.文件系统阶段 - 易于存储,处理速度快,数据形式丰富(文字,声音,图片...磁带,磁盘) 3.数据库系统阶段 - 易于 ...

  3. Android Studio Reference local .aar files

    repositories { flatDir { dirs 'libs' }} dependencies { compile 'com.android.support:support-v4:22.2. ...

  4. Jquery EasyUI DataGrid .net实例

    前台界面:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  5. MySQL数据单个数据太大,导入不进去

    mysql导入数据,navicat报错: MySQL server has gone away Table Restored: act_ge_bytearray Rolling back... Fin ...

  6. javascript confirm()函数的用法

    javascript confirm()函数的用法 confirm():确认消息对话框.用于允许用户做选择的动作.弹出的对话框中包含一确定按钮和一取消按钮. confirm(str) 参数说明: st ...

  7. web安全之sql注入联合查询

    联合查询的条件: 有显示位.当然要有注入点!! 提前需要了解的函数: union可合并两个或多个select语句的结果集,前提是两个select必有相同列.且各列的数据类型也相同 distinct 去 ...

  8. tableView

    Table View简单描述: 在iPhone和其他iOS的很多程序中都会看到Table View的出现,除了一般的表格资料展示之外,设置的属性资料往往也用到Table View,Table View ...

  9. #MySQL for Python(MySQLdb) Note

    #MySQL for Python(MySQLdb) Note #切记不要在python中创建表,只做增删改查即可. #步骤:(0)引用库 -->(1)创建连接 -->(2)创建游标 -- ...

  10. Sprint第一个冲刺(第九天)

    一.Sprint介绍 建立云端数据库,把注册的内容保存到云端,不易丢失. 实验截图: 任务进度: 二.Sprint周期 看板: 燃尽图: