Spring Boot踩坑之路一
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配置
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踩坑之路一的更多相关文章
- Spring Boot 踩坑之路之 Configuration Annotation Proessor not found in classpath
1. 出现spring boot Configuration Annotation Proessor not found in classpath的提示是在用了@ConfigurationProper ...
- spring boot踩坑记
Resolved exception caused by handler execution: org.springframework.http.converter.HttpMessageNotWri ...
- Spring WebSocket踩坑指南
Spring WebSocket踩坑指南 本次公司项目中需要在后台与安卓App间建立一个长连接,这里采用了Spring的WebSocket,协议为Stomp. 关于Stomp协议这里就不多介绍了,网上 ...
- html2canvas的踩坑之路
html2canvas的踩坑之路 前言 早有耳闻这个html2canvas比较坑,但无奈于产品需求的压迫,必须实现html转图片的功能,自此走上了填坑之路,好在最后的效果还算令人满意,这才没有误了产品 ...
- MySQL Connector/NET 使用小结(踩坑之路)
背景描述 根据项目的需要,需连接MySQL获取数据. 首先,先了解一下项目的情况: 之前的代码是C#编写的的, 运行时:.NETFramework3.5. 由于项目已经部署上线,因此不能升级运行时,这 ...
- Spring MVC 到 Spring Boot 的简化之路(山东数漫江湖)
背景 从Servlet技术到Spring和Spring MVC,开发Web应用变得越来越简捷.但是Spring和Spring MVC的众多配置有时却让人望而却步,相信有过Spring MVC开发经验的 ...
- Spring Boot的学习之路(02):和你一起阅读Spring Boot官网
官网是我们学习的第一手资料,我们不能忽视它.却往往因为是英文版的,我们选择了逃避它,打开了又关闭. 我们平常开发学习中,很少去官网上看.也许学完以后,我们连官网长什么样子,都不是很清楚.所以,我们在开 ...
- Android 上传开源项目到 jcenter 实战踩坑之路
本文微信公众号「AndroidTraveler」首发. 背景 其实 Android 上传开源项目到 jcenter 并不是一件新鲜事,网上也有很多文章. 包括我本人在将开源项目上传到 jcenter ...
- Spring MVC 到 Spring BOOT 的简化之路
背景 Spring vs Spring MVC vs Spring Boot Spring FrameWork Spring 还能解决什么问题 Spring MVC 为什么需要Spring Boot ...
随机推荐
- DOM对象和JQuery对象进行转换
var btn=document.getElementById("btn"); $(btn).click(function(){}); DOM对象转换为JQuery对象: 用$符号 ...
- android怎么打开wifi的组播功能
http://android.tgbus.com/Android/tutorial/201204/418987.shtml
- mysql问题: alter导致速度慢
1 如何并行执行. 数据库里有一个数据量非常大的表,当对这个表进行alter的时候,这时候该表是只读的,由于数据量很大alter时间很长,如果这时候有insert或update操作,有什么办法在这期间 ...
- UVA 11149 Power of Matrix
矩阵快速幂. 读入A矩阵之后,马上对A矩阵每一个元素%10,否则会WA..... #include<cstdio> #include<cstring> #include< ...
- jascript基础教程学习总结(2)
摘自:javascript基础教程 用链接对用户进行重定向: js001.html 这个HTML页面基于链接对用户进行重定向 <!DOCTYPE html PUBLIC "-//W3C ...
- Android NDK开发及调用标准linux动态库.so文件
源:Android NDK开发及调用标准linux动态库.so文件 预备知识及环境搭建 1.NDK(native development Kit)原生开发工具包,用来快速开发C.C++动态库,并能自动 ...
- java系列--类和对象
一.成员属性,构造方法,成员方法 1.类名首字母一般大写 2.方法名的首字母一般是小写,使用驼峰法(匈牙利法) myCry, 下划线法 my_cry 3.方法的声明没有函数体(接口,抽象类),数据类型 ...
- pku2104
传送门:http://poj.org/problem?id=2104 题目大意:给定一个长度为N的数组{A[i]},你的任务是解决Q个询问.每次询问在A[l], A[l+1], ...... , A[ ...
- Python装饰器学习(九步入门)
这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 ? 1 2 3 4 5 6 7 8 # -*- coding:gbk -*- '''示 ...
- Intel为什么做不好手机CPU?
Intel大名鼎鼎,在CPU界无人不知无人不晓,然而在当前主流的手机CPU市场上却是远远落后日本的ARM公司,这到底是Intel技术不足,还是ARM过于强大呢,今天我们就来探讨一下. 故事要从2006 ...