简介

为了简化开发Spring的复杂度,Spring提供了SpringBoot可以快速开发一个应用,这里就简单介绍下SpringBoot如何快速开发一个J2EE应用

HelloWorld

首先在gradle配置文件中引入SpringBoot

compile("org.springframework.boot:spring-boot-starter-web:1.4.0.RELEASE")

刷新项目后他会默认引入Spring的依赖以及内嵌tomcat,配置就这一步,下面就开始开发了

@SpringBootApplication
public class App {
public static void main(String[] args) throws Exception {
SpringApplication.run(new Object[]{App.class}, args);
}
}

以上就配置好了应用可以运行了,这里简单说下加的注解

@SpringBootApplication=@SpringBootConfiguration+@EnableAutoConfiguration+@ComponentScan

SpringBootConfiguration是以Configuration类的方式配置Spring相当于spring.xml可以使用@Bean注册一个实例

EnableAutoConfiguration是告诉SpringBoot自动配置Spring应用

ComponentScan是让spring扫描注解

下面就可以开发Controller了,这个和SpringMVC一样,其中RestController是Controller和ResponseBody的合体。

@RestController
public class MyController{
@RequestMapping("/")
String home() {
return "Hello World!";
}
}

运行App的main方法,就可以看到应用启动,可以访问localhost:8080了

热部署

IDE中的server都支持热部署,只要改了代码就会自动重新发布,这里使用SpringBoot由于是直接运行的main方法,所以没有这个功能。

不过Spring团队提供了dev-tool可以实现这个功能,使用也很方便,只要引入相应jar就行

compile 'org.springframework.boot:spring-boot-devtools:1.4.0.RELEASE'

这样只要有类改动,IDE会自动重编译(需要打开该功能),SpringBoot就会自动重新部署

总结

SpringBoot提供了一个很简单的开发流程,大大减少了繁琐的Spring配置,新项目可以尝试使用

注意

在运行springboot应用时遇到一个问题

** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
2016-11-13 21:16:43.923 WARN 8292 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [App]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'errorPageFilter' for bean class [org.springframework.boot.web.support.ErrorPageFilter] conflicts with existing, non-compatible bean definition of same name and class [org.springframework.boot.context.web.ErrorPageFilter]

这个是由于app类建在default包中,一定不要将app类放在默认包中,不然会导致各种奇怪的异常

springboot之HelloWorld的更多相关文章

  1. SpringBoot学习helloworld

    这几天开始学习springBoot记录一下(Hello World) pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0 ...

  2. SpringBoot的HelloWorld 应用及解释

    参考链接: Spring Data JPA - Reference Documentation Spring Data JPA--参考文档 中文版 纯洁的微笑:http://www.ityouknow ...

  3. [一]SpringBoot 之 HelloWorld

    (1)新建一个Maven Java工程 (2)在pom.xml文件中添加Spring BootMaven依赖 2.1在pom.xml中引入spring-boot-start-parent spring ...

  4. SpringBoot入门学习(一): Idea 创建 SpringBoot 的 HelloWorld

    创建项目: 项目结构: 程序启动入口: 正式开始: package com.example.demo; import org.springframework.boot.SpringApplicatio ...

  5. redis整合springboot的helloworld

    引入依赖 compile 'org.springframework.boot:spring-boot-starter-data-redis' 使用redis有两种方法 1.Jedis Jedis je ...

  6. SpringBoot——探究HelloWorld【三】

    前言 前面我们写了helloworld的一个,这里我们对他进行分析 探究 那么下面就开始我们的探究之旅吧,首先从POM文件来,在POM文件中我们导入了项目所需要的依赖 POM文件 父项目 <pa ...

  7. spring-boot的helloWorld详解

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 3.2.5 2.Maven Plugin管理 pom.xml配置代码: <project xml ...

  8. eclipse springboot运行helloworld错误: 找不到或无法加载主类 xxx.xxx.xxx

    这个错误,在网上搜找了好久,说是什么jar包冲突,什么环境配置,我经过验证均是正确的,javac java java -version 都没问题,环境变量也OK,各种解释均没有能够解决我的问题,最后好 ...

  9. SpringBoot的 HelloWorld

    SpringBoot HelloWorld 功能需求 ​ 浏览器发送hello请求,服务器接收请求并处理,相应HelloWorld字符串 1.创建一个maven工程:(jar) 2.导入SpringB ...

随机推荐

  1. Eclipse启动时发生An internal error occurred during: "Initializing Java Tooling".错误的解决方法

    问题描述: Eclipse启动时发生An internal error occurred during: "Initializing JavaTooling".错误的解决方法 解决 ...

  2. Python学习日志(三)

    运算补充(因为之前看书看过的我又忘了...) python3 里 / 直接是浮点除.python2的 / 是直接整除,取整数部分,小数不要了,python3也可以这样整除,用//实现. **是乘方!! ...

  3. java常用设计模式

    一个程序员对设计模式的理解: "不懂"为什么要把很简单的东西搞得那么复杂. 后来随着软件开发经验的增加才开始明白我所看到的"复杂"恰恰就是设计模式的精髓所在,我 ...

  4. Euler猜想

    这是从http://duodaa.com/blog/index.php/archives/538/截得图,以下是代码 package math; import java.math.BigDecimal ...

  5. 1_MVC+EF+Autofac(dbfirst)轻型项目框架_core层(以登陆为例)

    前言 在上一篇0_MVC+EF+Autofac(dbfirst)轻型项目框架_基本框架中,我已经介绍了这个轻型框架的层次结构,在下面的这篇文章中,我将以教师登陆功能为例,具体来扩充下我的core层的代 ...

  6. 二分 题目 压缩打包 Special Judge? 不不不 当然不是

    http://noi.openjudge.cn/ch0111/ No 题目 分数 01 查找最接近的元素 10 3176 02 二分法求函数的零点 10 2181 03 矩形分割 10 1420 04 ...

  7. ThinkPHP框架的一些基础应用

    这是俺滴师傅给俺传授了的知识,特在此分享. TP框架,做PHP开发的都应该有所耳闻.下面,我们就来说说入口文件的生成: 创建新项目时,首先,在目录文件下创建一个新的文件夹.然后将Thinkphp框架文 ...

  8. echarts3 -arcgis echarts.js修改

     在echarts.js中修改修改 clone 方法    其中 source instance of Array 修改为Object.prototype.toString.call(source)  ...

  9. ThreadPoolExecutor源码学习(1)-- 主要思路

    ThreadPoolExecutor是JDK自带的并发包对于线程池的实现,从JDK1.5开始,直至我所阅读的1.6与1.7的并发包代码,从代码注释上看,均出自Doug Lea之手,从代码上看JDK1. ...

  10. PHP去重算法的优化过程

    最近公司在做一个项目,需要对爬取到的数据进行去重,方法就是根据数据的id,去除掉id重复的数据. 下面是这个方法的演化过程. // 去重 $arr_id = array(); $LeTVFeedLis ...