SpringBoot(1) 入门使用

  • SpringBoot是一个快速整合第三方框架,简化XML配置完全采用注解化,内置Http服务器(Jetty 和 Tomcat),最终以Java应用程序的方式进行执行。那么如何做到快速整合第三方框架?如何实现完全无配置文件(采用注解化)?内置Tomcat 服务器怎么实现?
  • SpringBoot采用Maven子父依赖关系,封装好需要整合的Jar的依赖信
    息。使用的时候只需要我们直接申明一下就行了。
  • Spring3.0以上(提供注解),SpringBoot通过Spring Mvc 内置注解加载整个SpringMvc容器,使用Java代码的方式编写SPrngMvc配置初始化。而Web项目,则是通过Web.xml文件加载整个项目流程的class文件
  • 内置Tomcat 服务器
    如果看过《How tomcat to work》就会明白Tomcat本质上就是一个Servlet容器,所以Tomcat本质上也是相当于一个Java项目,SpringBoot就是通过Java语言创建Tomcat容器的。
  • 使用SpringBoot快速搭建一个web项目
  1. 使用IDEA新建一个Meven项目


    image.png
  2. 添加核心依赖




  4.0.0

  com.ggr.springboot2
  springboot2-hello
  1.0-SNAPSHOT
  war

  
  
    org.springframework.boot
    spring-boot-starter-parent
    2.0.0.RELEASE
  

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

  
    UTF-8
    1.8
    1.8
  
  
    springboot2-hello
    
      
        
          maven-clean-plugin
          3.0.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.7.0
        
        
          maven-surefire-plugin
          2.20.1
        
        
          maven-war-plugin
          3.2.0
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  

  1. 创建一个App.java,并且在App的main函数中启动SpringBoot
package com.ggr.hello;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}
  1. 创建IndexController 进行测试
package com.ggr.hello.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

    @RequestMapping("index")
    public String Index(){
        return "主页";
    }
}

5.点击启动按钮进行启动,当控制台出现如下界面是说明我们最基本的一个项目就已经做好了。

"E:\Program Files\Java\jdk1.8.0_111\bin\java.exe" "-javaagent:E:\Program Files\IDEA\IntelliJ IDEA 2018.2\lib\idea_rt.jar=8332:E:\Program Files\IDEA\IntelliJ IDEA 2018.2\bin" -Dfile.encoding=UTF-8 -classpath "E:\Program Files\Java\jdk1.8.0_111\jre\lib\charsets.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\deploy.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-64.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\dnsns.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\nashorn.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\zipfs.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\javaws.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\jce.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\jfr.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\jsse.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\management-agent.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\plugin.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\resources.jar;E:\Program Files\Java\jdk1.8.0_111\jre\lib\rt.jar;E:\Program Files\IDEASPACE\springboot2-hello\target\classes;E:\Maven\Repositories\org\springframework\boot\spring-boot-starter-web\2.0.0.RELEASE\spring-boot-starter-web-2.0.0.RELEASE.jar;E:\Maven\Repositories\org\springframework\boot\spring-boot-starter\2.0.0.RELEASE\spring-boot-starter-2.0.0.RELEASE.jar;E:\Maven\Repositories\org\springframework\boot\spring-boot\2.0.0.RELEASE\spring-boot-2.0.0.RELEASE.jar;E:\Maven\Repositories\org\springframework\boot\spring-boot-autoconfigure\2.0.0.RELEASE\spring-boot-autoconfigure-2.0.0.RELEASE.jar;E:\Maven\Repositories\org\springframework\boot\spring-boot-starter-logging\2.0.0.RELEASE\spring-boot-starter-logging-2.0.0.RELEASE.jar;E:\Maven\Repositories\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;E:\Maven\Repositories\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;E:\Maven\Repositories\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;E:\Maven\Repositories\org\apache\logging\log4j\log4j-to-slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar;E:\Maven\Repositories\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar;E:\Maven\Repositories\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;E:\Maven\Repositories\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;E:\Maven\Repositories\org\springframework\spring-core\5.0.4.RELEASE\spring-core-5.0.4.RELEASE.jar;E:\Maven\Repositories\org\springframework\spring-jcl\5.0.4.RELEASE\spring-jcl-5.0.4.RELEASE.jar;E:\Maven\Repositories\org\yaml\snakeyaml\1.19\snakeyaml-1.19.jar;E:\Maven\Repositories\org\springframework\boot\spring-boot-starter-json\2.0.0.RELEASE\spring-boot-starter-json-2.0.0.RELEASE.jar;E:\Maven\Repositories\com\fasterxml\jackson\core\jackson-databind\2.9.4\jackson-databind-2.9.4.jar;E:\Maven\Repositories\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;E:\Maven\Repositories\com\fasterxml\jackson\core\jackson-core\2.9.4\jackson-core-2.9.4.jar;E:\Maven\Repositories\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.4\jackson-datatype-jdk8-2.9.4.jar;E:\Maven\Repositories\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.4\jackson-datatype-jsr310-2.9.4.jar;E:\Maven\Repositories\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.4\jackson-module-parameter-names-2.9.4.jar;E:\Maven\Repositories\org\springframework\boot\spring-boot-starter-tomcat\2.0.0.RELEASE\spring-boot-starter-tomcat-2.0.0.RELEASE.jar;E:\Maven\Repositories\org\apache\tomcat\embed\tomcat-embed-core\8.5.28\tomcat-embed-core-8.5.28.jar;E:\Maven\Repositories\org\apache\tomcat\embed\tomcat-embed-el\8.5.28\tomcat-embed-el-8.5.28.jar;E:\Maven\Repositories\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.28\tomcat-embed-websocket-8.5.28.jar;E:\Maven\Repositories\org\hibernate\validator\hibernate-validator\6.0.7.Final\hibernate-validator-6.0.7.Final.jar;E:\Maven\Repositories\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;E:\Maven\Repositories\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;E:\Maven\Repositories\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;E:\Maven\Repositories\org\springframework\spring-web\5.0.4.RELEASE\spring-web-5.0.4.RELEASE.jar;E:\Maven\Repositories\org\springframework\spring-beans\5.0.4.RELEASE\spring-beans-5.0.4.RELEASE.jar;E:\Maven\Repositories\org\springframework\spring-webmvc\5.0.4.RELEASE\spring-webmvc-5.0.4.RELEASE.jar;E:\Maven\Repositories\org\springframework\spring-aop\5.0.4.RELEASE\spring-aop-5.0.4.RELEASE.jar;E:\Maven\Repositories\org\springframework\spring-context\5.0.4.RELEASE\spring-context-5.0.4.RELEASE.jar;E:\Maven\Repositories\org\springframework\spring-expression\5.0.4.RELEASE\spring-expression-5.0.4.RELEASE.jar" com.ggr.hello.App

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.0.RELEASE)

2018-09-19 22:57:54.449  INFO 16688 --- [           main] com.ggr.hello.App                        : Starting App on GuiRuning with PID 16688 (started by GuiRunning in E:\Program Files\IDEASPACE\springboot2-hello)
2018-09-19 22:57:54.454  INFO 16688 --- [           main] com.ggr.hello.App                        : No active profile set, falling back to default profiles: default
2018-09-19 22:57:54.545  INFO 16688 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2ea6137: startup date [Wed Sep 19 22:57:54 CST 2018]; root of context hierarchy
2018-09-19 22:57:56.525  INFO 16688 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-09-19 22:57:56.560  INFO 16688 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-09-19 22:57:56.560  INFO 16688 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.28
2018-09-19 22:57:56.574  INFO 16688 --- [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: [E:\Program Files\Java\jdk1.8.0_111\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;.;E:\Program Files\Java\jdk1.8.0_111;\bin;E:\Program Files\Java\jdk1.8.0_111;\jre\bin;.;E:\Program Files\Java\jdk1.8.0_111;\bin;E:\Program Files\Java\jdk1.8.0_111;\jre\bin;.;E:\Program Files\Java\jdk1.8.0_111;\bin;E:\Program Files\Java\jdk1.8.0_111;\jre\bin;C:\WINDOWS\System32;E:\Oracle\app\oracle\product\10.2.0\server\bin;E:\Program Files\Petty;E:\Program Files\MySql\mysql-5.5.50-winx64\bin;E:\Maven\apache-maven-3.3.3\bin;E:\Program Files\NodeJs;E:\Program Files\Git\cmd;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;G:\apache-ant-1.9.9\bin;C:\Program Files (x86)\QuickTime\QTSystem\;E:\Program Files (x86)\Calibre2\;C:\Program Files (x86)\Windows Live\Shared;E:\Program Files\NodeJs\node_global;C:\WINDOWS\System32\OpenSSH\;E:\Oracle\app\oracle\product\10.2.0\server\bin;E:\Program Files\Petty;E:\Program Files\MySql\mysql-5.5.50-winx64\bin;E:\Maven\apache-maven-3.3.3\bin;E:\Program Files\NodeJs;E:\Program Files\Git\cmd;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;G:\apache-ant-1.9.9\bin;C:\Program Files (x86)\QuickTime\QTSystem\;E:\Program Files (x86)\Calibre2\;C:\Program Files (x86)\Windows Live\Shared;E:\Program Files\NodeJs\node_global;C:\WINDOWS\System32\OpenSSH\;E:\Oracle\app\oracle\product\10.2.0\server\bin;E:\Program Files\Petty;E:\Program Files\MySql\mysql-5.5.50-winx64\bin;E:\Maven\apache-maven-3.3.3\bin;E:\Program Files\NodeJs;E:\Program Files\Git\cmd;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;G:\apache-ant-1.9.9\bin;C:\Program Files (x86)\QuickTime\QTSystem\;E:\Program Files (x86)\Calibre2\;C:\Program Files (x86)\Windows Live\Shared;E:\Program Files\NodeJs\node_global;C:\WINDOWS\System32\OpenSSH\;.;E:\Program Files\Java\jdk1.8.0_111\bin;E:\Program Files\Java\jdk1.8.0_111\jre\bin;E:\Program Files\Petty;E:\Program Files\MySql\mysql-5.5.50-winx64\bin;E:\Maven\apache-maven-3.3.3\bin;E:\Program Files\NodeJs;C:\Users\GuiRunning\AppData\Local\Microsoft\WindowsApps;G:\apache-ant-1.9.9\bin;C:\Users\GuiRunning\AppData\Roaming\npm;;.]
2018-09-19 22:57:56.781  INFO 16688 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-09-19 22:57:56.781  INFO 16688 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2241 ms
2018-09-19 22:57:56.945  INFO 16688 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-09-19 22:57:56.950  INFO 16688 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-09-19 22:57:56.951  INFO 16688 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-09-19 22:57:56.951  INFO 16688 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-09-19 22:57:56.951  INFO 16688 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-09-19 22:57:57.304  INFO 16688 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2ea6137: startup date [Wed Sep 19 22:57:54 CST 2018]; root of context hierarchy
2018-09-19 22:57:57.400  INFO 16688 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/index]}" onto public java.lang.String com.ggr.hello.controller.IndexController.Index()
2018-09-19 22:57:57.405  INFO 16688 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-09-19 22:57:57.407  INFO 16688 --- [           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)
2018-09-19 22:57:57.450  INFO 16688 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-19 22:57:57.450  INFO 16688 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-19 22:57:57.499  INFO 16688 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-19 22:57:57.737  INFO 16688 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-09-19 22:57:57.793  INFO 16688 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-09-19 22:57:57.797  INFO 16688 --- [           main] com.ggr.hello.App                        : Started App in 3.673 seconds (JVM running for 4.21)

你可能感兴趣的:(SpringBoot(1) 入门使用)