Spring Boot

SpringBoot是一个社区反馈推动的项目。SpringBoot可以说是至少五年来Spring乃至整个Java社区最有影响力的项目之一。SpringBoot主要包括以下特性:

  1. 直接嵌入Tomcat,Jetty或者Undertow作为Servlet container。从此之后再也不用将应用程序打包成war然后上传到application server里面了。
  2. 提供了starter POM,能够非常方便的进行包管理,很大程度上减少了jar hell或者dependency hell。
  3. 自动进行Spring框架的配置,节省程序员大量的时间和精力,能够让程序员专注在业务逻辑代码的编写上。
  4. 不需要任何第三方系统,Spring Boot自带了可以用于生产环境的程序状态信息和健康状态。同时可以让应用程序非常方便的读取外部的配置信息。
  5. 完全不需要任何代码的自动生成。更不需要用xml来进行框架配置

基于Maven工程使用SpringBoot

新建Maven工程,编写pom.xml

<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> <groupId>20171111</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>springboot</name>
<url>http://maven.apache.org</url> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath></relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

编写main函数:

在这里标记HelloworldDemoApplication class为SpringBootApplication,SpringBoot在后台会根据这个标记进行很多自动配置,比如配置MVC,配置包扫描,注入必要的类,注入自动配置的类等等。

这里的main函数是一个java标准的main函数,这个相当于应用程序入口,servlet container会在启动的时候找到这个入口,启动Spring container,完成初始化。

package springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; @SpringBootApplication
public class HelloworldDemoApplication implements EmbeddedServletContainerCustomizer{
public static void main(String[] args){
SpringApplication.run(HelloworldDemoApplication.class, args);
} public void customize(ConfigurableEmbeddedServletContainer container) {
// TODO 自动生成的方法存根
container.setPort(8088);
}
}

编写 HelloworldRestController类

package springboot;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloworldRestController {
@RequestMapping("/")
public String helloworld(){
return "hello world";
}
}

选择运行方式为:Maven Build ->对应的窗口里面设置Goals:clean package spring-boot:run,顺便为了方便把下面的Skip Tests也勾上,然后run。

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.2.RELEASE) 2017-11-11 23:41:56.105 INFO 8407 --- [ main] springboot.HelloworldDemoApplication : Starting HelloworldDemoApplication on pdeMacBook-Pro.local with PID 8407 (/Users/pg/Documents/workspace/springboot/target/classes started by pg in /Users/pg/Documents/workspace/springboot)
2017-11-11 23:41:56.107 INFO 8407 --- [ main] springboot.HelloworldDemoApplication : No active profile set, falling back to default profiles: default
2017-11-11 23:41:56.147 INFO 8407 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@563b3f89: startup date [Sat Nov 11 23:41:56 CST 2017]; root of context hierarchy
2017-11-11 23:41:57.262 INFO 8407 --- [ 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]]
2017-11-11 23:41:58.061 INFO 8407 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8088 (http)
2017-11-11 23:41:58.091 INFO 8407 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-11-11 23:41:58.093 INFO 8407 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.30
2017-11-11 23:41:58.224 INFO 8407 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-11-11 23:41:58.224 INFO 8407 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2077 ms
2017-11-11 23:41:58.590 INFO 8407 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-11-11 23:41:58.595 INFO 8407 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-11-11 23:41:58.596 INFO 8407 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-11-11 23:41:58.596 INFO 8407 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-11-11 23:41:58.596 INFO 8407 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-11-11 23:41:58.947 INFO 8407 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@563b3f89: startup date [Sat Nov 11 23:41:56 CST 2017]; root of context hierarchy
2017-11-11 23:41:59.028 INFO 8407 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String springboot.HelloworldRestController.helloworld()
2017-11-11 23:41:59.032 INFO 8407 --- [ 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)
2017-11-11 23:41:59.032 INFO 8407 --- [ 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,javax.servlet.http.HttpServletResponse)
2017-11-11 23:41:59.056 INFO 8407 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-11-11 23:41:59.056 INFO 8407 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-11-11 23:41:59.091 INFO 8407 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-11-11 23:41:59.207 INFO 8407 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-11-11 23:41:59.291 ERROR 8407 --- [ main] o.a.coyote.http11.Http11NioProtocol : Failed to start end point associated with ProtocolHandler ["http-nio-8088"]

如果你遇到这种问题:Failed to start end point associated with ProtocolHandler ["http-nio-8088"]

这表明8088端口被占用了,可以通过实现EmbeddedServletContainerCustomizer接口修改SpringBoot内置的tomcat端口来解决端口被占用的问题:

package springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; @SpringBootApplication
public class HelloworldDemoApplication implements EmbeddedServletContainerCustomizer{
public static void main(String[] args){
SpringApplication.run(HelloworldDemoApplication.class, args);
} public void customize(ConfigurableEmbeddedServletContainer container) {
// TODO 自动生成的方法存根
container.setPort(8089);
}
}

修改端口为8089:再次运行:

 .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.2.RELEASE) 2017-11-11 23:47:48.204 INFO 8449 --- [ main] springboot.HelloworldDemoApplication : Starting HelloworldDemoApplication on pdeMacBook-Pro.local with PID 8449 (/Users/pg/Documents/workspace/springboot/target/classes started by pg in /Users/pg/Documents/workspace/springboot)
2017-11-11 23:47:48.209 INFO 8449 --- [ main] springboot.HelloworldDemoApplication : No active profile set, falling back to default profiles: default
2017-11-11 23:47:48.363 INFO 8449 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@48af20f4: startup date [Sat Nov 11 23:47:48 CST 2017]; root of context hierarchy
2017-11-11 23:47:49.230 INFO 8449 --- [ 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]]
2017-11-11 23:47:50.197 INFO 8449 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8089 (http)
2017-11-11 23:47:50.223 INFO 8449 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-11-11 23:47:50.225 INFO 8449 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.30
2017-11-11 23:47:50.360 INFO 8449 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-11-11 23:47:50.362 INFO 8449 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2001 ms
2017-11-11 23:47:50.761 INFO 8449 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-11-11 23:47:50.767 INFO 8449 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-11-11 23:47:50.767 INFO 8449 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-11-11 23:47:50.767 INFO 8449 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-11-11 23:47:50.768 INFO 8449 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-11-11 23:47:51.092 INFO 8449 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@48af20f4: startup date [Sat Nov 11 23:47:48 CST 2017]; root of context hierarchy
2017-11-11 23:47:51.161 INFO 8449 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String springboot.HelloworldRestController.helloworld()
2017-11-11 23:47:51.164 INFO 8449 --- [ 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)
2017-11-11 23:47:51.165 INFO 8449 --- [ 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,javax.servlet.http.HttpServletResponse)
2017-11-11 23:47:51.193 INFO 8449 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-11-11 23:47:51.194 INFO 8449 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-11-11 23:47:51.237 INFO 8449 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-11-11 23:47:51.365 INFO 8449 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-11-11 23:47:51.463 INFO 8449 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8089 (http)
2017-11-11 23:47:51.471 INFO 8449 --- [ main] springboot.HelloworldDemoApplication : Started HelloworldDemoApplication in 14.261 seconds (JVM running for 19.706)
2017-11-11 23:51:29.888 INFO 8449 --- [nio-8089-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-11-11 23:51:29.890 INFO 8449 --- [nio-8089-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-11-11 23:51:29.920 INFO 8449 --- [nio-8089-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 30 ms

打开浏览器输入:localhost:8089

基于Maven的SpringBoot简单使用就完成了。

SpringBoot-基于Maven工程使用SpringBoot的更多相关文章

  1. (三)创建基于maven的javaFX+springboot项目创建

    创建基于maven的javaFx+springboot项目有两种方式,第一种为通过非编码的方式来设计UI集成springboot:第二种为分离用户界面(UI)和后端逻辑集成springboot,其中用 ...

  2. (四)创建基于maven的javaFX+springboot项目,用户界面与后台逻辑分离方式

    下面来介绍创建maven的javaFX+springboot项目,基于用户界面与后天逻辑分离的方式,用户界面使用fxml文件来常见,类似于jsp,可以引入css文件修饰界面 maven依赖 <d ...

  3. maven工程仿springboot手写代码区分开发测试生产

    读取代码: package com.jz.compute.mc.v2.config; import java.util.Enumeration; import java.util.ResourceBu ...

  4. 【java工具类】java做的一个xml转Excel工具,基于maven工程

    说明:适合数据库导出为xml时转成Excel 本工具将上传至GitHub:https://github.com/xiaostudy/xiaostudyAPI3 doc4j的maven依赖 <!- ...

  5. 菜鸟——springboot+mybatis+maven

    网上找了很多资料,学习如何搭建springboot,由于刚刚接触springboot,不是很熟练,通过参考网上别人搭建的例子,自己也搭建了一个简单的springboot+mybaits+maven 网 ...

  6. 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建

    基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...

  7. SpringBoot 1.快速搭建一个 SpringBoot Maven工程

    一.新建一个Maven工程 (1)选择创建简单MAVNE工程 (2)输入你自己的MAVEN工程的Group Id(必填).Artifact Id(必填).Version(必填).Packaging(必 ...

  8. SpringBoot热部署配置(基于Maven)

    热部署的意思是只要类中的代码被修改了,就能实时生效,而不用重启项目.spring-boot-devtools 是一个为开发者服务的一个模块,其中最重要的功能就是自动应用代码更改到最新的App上面去.原 ...

  9. 基于Maven的SpringBoot项目实现热部署的两种方式

    转载:http://blog.csdn.net/tengxing007/article/details/72675168 前言 JRebel是JavaEE中比较流行的热部署插件,可快速实现热部署,节省 ...

随机推荐

  1. Unix系统编程()信号:概念和概述

    这篇将一口气学完信号的基本概念,但是有很多的细节,所以篇幅较长,请做好心理准备. (他大爷的,一口气没有学完,太懒了) 有以下主题: 各种不同信号及其用途 内核可能为进程产生信号的环境,以及某一进程向 ...

  2. [Kernel]理解System call系统调用

    转自:http://os.51cto.com/art/200512/13510.htm 现在,您或许正在查看设备驱动程序,并感到奇怪:“函数 foo_read() 是如何被调用的?”或者可能疑惑: “ ...

  3. Spring MVC之@RequestParam @RequestBody @RequestHeader 等详解<转>

    简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...

  4. JavaScrip——简单练习(输出方式,简单表单验证)

    <script> //输出方式 document.write(Date());//获取当前时间 document.write(1); document.write("<p& ...

  5. elasticsearch安装与使用(2)-- centos7 安装测试的集群工具elasticsearch head

    elasticsearch-head是elasticsearch(下面称ES)比较普遍使用的可监控.测试等功能的集群管理工具,是由H5编写的单独的网页程序.使用方法网上很多,这里教大家一个超简单安装h ...

  6. Android SDK代理server解决国内不能更新下载问题

    读者须知:本篇文章中最靠谱的是第三种方式,近期有读者反映第三种方式也不行了,以下提供一点其它途径的开源镜像网站: 国内高校的开源镜像站 中国科学技术大学(debian.ustc.edu.cn) 上海交 ...

  7. hdu 4003(树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 思路:dp[i][j]表示以i为根选择j个机器人的最小花费,然后就是背包了:dp[u][i]=m ...

  8. 九度OJ 上剑指 offer 习题目录

    <剑指Offer>面试题集收录汇总 面试题1 赋值运算符函数 不适合在线模式 面试题2 实现Singleton模式 不适合在线模式 面试题3 二维数组中的查找 已收录 面试题4 替换空格 ...

  9. ios开发之--PDF文件生成

    写项目的时候,碰到一个需求,就是在手机端根据指定的文件内容生成PDF文件,并可以保存到手机上,因为以前只是听说过,没有真正的去了解过这个需求,通过查阅资料,可以实现这个功能,话不多说,代码如下: -( ...

  10. Android 安卓真机调试 出现Installation error: INSTALL_FAILED_UPDATE_INCOMPATIBLE....

    [2017-03-24 13:30:04 - DataVDemo06] Installing DataVDemo06.apk...[2017-03-24 13:30:08 - DataVDemo06] ...