Takes an opinionated view of building production-ready Spring applications. Spring Boot favors convention over configuration and is designed to get you up and running as quickly as possible.

旨在简化创建产品级的 Spring应用和服务。Spring Boot 引导优先于配置,它可以让你避免繁杂的配置,尽可能的帮助你快速建站。

为什么使用Spring Boot

经过十多年的发展Spring家族已经壮大,要灵活使用Spring家族的产品已经变得有些困难,尤其是要维护一大堆的配置文件,在项目开发中令人头疼。Spring Boot解决了这个问题,并大大简化了我们的开发成本

其优点如下:

  • 不用看一大坨的xml。用java config可以让你很容易明白一些框架的关键
  • Spring Boot 要解决的问题, 精简配置是一方面, 另外一方面是如何方便的让spring生态圈和其他工具链整合(比如redis, email, elecsearch)
  • 配合各种starter使用,基本上可以做到自动化配置
  • 配合Maven或Gradle等构件工具打成Jar包后,Java -jar 简化部署运行

建立maven web项目

使用maven建立web项目,并参考官方文档进行版本选择和pom配置

官方maven配置,请根据需求选择版本

POM中添加parent标签

添加parent后添加相关依赖不需要version

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>

添加依赖

web工程的依赖,包括spring mvc tomcat等,spring boot会在需要时使用

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

添加插件

用来在main方法中启动工程

<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
</plugin>
</plugins>

编写代码 HELLO WORLD

// 其中@SpringBootApplication申明让spring boot自动给程序进行必要的配置,等价于使用@Configuration,@EnableAutoConfiguration和@ComponentScan
@RestController
@SpringBootApplication
public class HelloWorld { @RequestMapping("/")
public String hello() {
return "Hello World";
} public static void main(String[] args) {
SpringApplication.run(HelloWorld.class,args);
}
}

检验成果

运行main函数之后访问http://localhost:8080/即可看到结果

我遇到的问题

问题出现在我建立maven项目编写java代码的时候在默认包中写的application类,并没有建立包。这种做法让Spring Boot每次都会扫描默认类及下属的所有类,浪费大量时间。所以在启动时会报警告,启动不成功

Your Application class should be placed in a specific package and not in the default (top-level) package. For example, put it in com.example and place all your application code in this package or in sub-packages like com.example.foo and com.example.bar.

Placing your Application class in the default package, i.e. directly in src/main/java isn't a good idea and it will almost certainly cause your application to fail to start. If you do so, you should see this warning:

** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.

Spring Boot踩坑之路一的更多相关文章

  1. Spring Boot 踩坑之路之 Configuration Annotation Proessor not found in classpath

    1. 出现spring boot Configuration Annotation Proessor not found in classpath的提示是在用了@ConfigurationProper ...

  2. spring boot踩坑记

    Resolved exception caused by handler execution: org.springframework.http.converter.HttpMessageNotWri ...

  3. Spring WebSocket踩坑指南

    Spring WebSocket踩坑指南 本次公司项目中需要在后台与安卓App间建立一个长连接,这里采用了Spring的WebSocket,协议为Stomp. 关于Stomp协议这里就不多介绍了,网上 ...

  4. html2canvas的踩坑之路

    html2canvas的踩坑之路 前言 早有耳闻这个html2canvas比较坑,但无奈于产品需求的压迫,必须实现html转图片的功能,自此走上了填坑之路,好在最后的效果还算令人满意,这才没有误了产品 ...

  5. MySQL Connector/NET 使用小结(踩坑之路)

    背景描述 根据项目的需要,需连接MySQL获取数据. 首先,先了解一下项目的情况: 之前的代码是C#编写的的, 运行时:.NETFramework3.5. 由于项目已经部署上线,因此不能升级运行时,这 ...

  6. Spring MVC 到 Spring Boot 的简化之路(山东数漫江湖)

    背景 从Servlet技术到Spring和Spring MVC,开发Web应用变得越来越简捷.但是Spring和Spring MVC的众多配置有时却让人望而却步,相信有过Spring MVC开发经验的 ...

  7. Spring Boot的学习之路(02):和你一起阅读Spring Boot官网

    官网是我们学习的第一手资料,我们不能忽视它.却往往因为是英文版的,我们选择了逃避它,打开了又关闭. 我们平常开发学习中,很少去官网上看.也许学完以后,我们连官网长什么样子,都不是很清楚.所以,我们在开 ...

  8. Android 上传开源项目到 jcenter 实战踩坑之路

    本文微信公众号「AndroidTraveler」首发. 背景 其实 Android 上传开源项目到 jcenter 并不是一件新鲜事,网上也有很多文章. 包括我本人在将开源项目上传到 jcenter ...

  9. Spring MVC 到 Spring BOOT 的简化之路

    背景 Spring vs Spring MVC vs Spring Boot Spring FrameWork Spring 还能解决什么问题 Spring MVC 为什么需要Spring Boot ...

随机推荐

  1. 深度学习 for java http://deeplearning4j.org/

    http://deeplearning4j.org/ http://deeplearning4j.org/zh-neuralnet-overview

  2. mac系统不能使用127.0.0.2的解决方案

    英语学得不好,国外这位大神的精彩解释不是特能看的懂.我模仿的试了一下. 解决方案: 1.打开mac终端 2.输入:sudo ifconfig lo0 alias 127.1.1.1 netmask 0 ...

  3. form表单传递下拉框的Value和Text值,不适用Jquery传递

    同时获取下拉框的Value和Text值的解决办法:添加一个<input type="text" >文本框,用户选中一项后就将该项的value值给他 然后接受页面获取该文 ...

  4. 求两个字符串最大的子字符串C#

    此代码由Java改写而来,字符串支持中文格式的. string str1 = "中国ab-15"; string str2 = "中国ab-23"; byte[ ...

  5. Java6 WebService学习

    首先,建立一个WebService: package garfield; import javax.jws.WebService; import javax.xml.ws.Endpoint; @Web ...

  6. BZOJ 1653 [Usaco2006 Feb]Backward Digit Sums ——搜索

    [题目分析] 劳逸结合好了. 杨辉三角+暴搜. [代码] #include <cstdio> #include <cstring> #include <cmath> ...

  7. *CCF 201612-2 工资计算(80)

    试题编号:201612-2 试题名称:工资计算 时间限制:1.0s 内存限制:256.0MB 问题描述 小明的公司每个月给小明发工资,而小明拿到的工资为交完个人所得税之后的工资.假设他一个月的税前工资 ...

  8. html5 安卓拨打电话 发短信

    方法一: <input name=”phone_no” format=”*m” value=”13″/> <do type=”option” label=”呼出号”> < ...

  9. 如何使用python timeit模块使用实践

    其实平时使用测试应用运行时间的情况 细算一下还真的很少.很久没有做性能优化的工作,不管是cProfile还是timeit模块都已经生疏了很久没有使用,我在以前的文章里面有提到过cPfile的性能测试使 ...

  10. CentOS 7 上面安装PowerShell

    看了文章 爱上PowerShell , 就想在CentOS 7上面试试PowerShell , 本文记录了在CentOS 7.2上安装Powershell 的过程. 首先我们要从github上下载最新 ...