springBoot项目是spring的一个子项目,使用约定由于配置的思想省去了以往在开发过程中许多的配置工作(其实使用springBoot并不是零配置,只是使用了注解完全省去了XML文件的配置),达到了开箱即用的目的使我们专注于业务逻辑的快速开发

一下的demo是我总结网上的一些配置自己实践过得来的:

我们使用maven来作为依赖管理的工具,首先,我们使用maven-archtype-quickstart模板建立一个普通java项目 以下是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>com.jiaoyiping</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 根据自己的需要决定是打jar包还是war包-->
<packaging>war</packaging>
<name>springboot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.0.2.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> </dependencies>
</project>

以rest方式的接口开发为例,如果我们不需要用打war包的方式进行部署(pom中打包方式要改成jar) 可以通过main()方法来运行:

package com.jiaoyiping.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import com.jiaoyiping.springboot.service.MailService; /**
* Hello world!
*
*/
@Configuration
@ComponentScan
@EnableAutoConfiguration
@RequestMapping(value = "/hello")
public class App{
@RequestMapping(value = "/hello", method = RequestMethod.GET)
@ResponseBody
public String hello() { return "hello world"; }public static void main(String[] args) { SpringApplication.run(App.class, args); }
}

启动项目、访问http://localhost:8080/hello/hello 就可以访问我们定义的接口

如果需要通过打包的方式在web容器中进行部署,则需要继承 SpringBootServletInitializer 覆盖configure(SpringApplicationBuilder)方法

代码如下:

package com.jiaoyiping.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import com.jiaoyiping.springboot.service.MailService; /**
* Hello world!
*
*/
@Configuration
@ComponentScan
@EnableAutoConfiguration
@RequestMapping(value = "/hello")
public class App extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(App.class
);
}
@RequestMapping(value = "/hello", method = RequestMethod.GET)
@ResponseBody
public String hello() { return "hello world"; }
}

需要注意一下几点:

1.jar包中的打包方式根据自己的需要进行修改

2.若打包成war包,则需要继承 org.springframework.boot.context.web.SpringBootServletInitializer类,覆盖其config(SpringApplicationBuilder)方法

3.打包成war的话,如果打包之后的文件中没有web.xml文件的话自己可以加进去一个最简单的web.xml(只有根节点的定义,而没有子元素),防止因缺乏web.xml文件而部署失败

使用springBoot进行快速开发的更多相关文章

  1. spring-boot开发:使用内嵌容器进行快速开发及测试

    一.简述一下spring-boot微框架 1.spring-boot微框架是什么? 大家都知道,在使用spring框架进行应用开发时需要很多*.xml的初始化配置文件,而springBoot就是用来简 ...

  2. 基于SpringBoot+Mybatis+AntDesign快速开发平台,Jeecg-Boot 1.1 版本发布

    Jeecg-Boot 1.1 版本发布,初成长稳定版本 导读     平台首页UI升级,精美的首页支持多模式 提供4套代码生成器模板(支持单表.一对多) 集成Excel简易工具类,支持单表.一对多导入 ...

  3. fieldmeta 基于springboot的字段元数据管理,通用代码生成,快速开发引擎

    fieldmeta: 基于springboot的字段元数据管理 version:Alpha 0.0.1 ,码云地址:https://gitee.com/klguang/fieldmeta 元数据(Me ...

  4. 快速开发第一个SpringBoot应用

    通过笔者这段实践SpringBoot的学习,发现自从使用了SpringBoot后,就再也回不去SpringMVC了,因为相比于SpringMVC,SpringBoot真是太高效率了.下面我们看看它效率 ...

  5. springboot 快速开发的定制补充

    增强 SpringBoot 快速开发工具 项目地址:https://gitee.com/sanri/web-ui 优点:这是一个 web 通用配置的组件,即插即用,可用于新项目或私活.是对 Sprin ...

  6. JeecgBoot 2.1.1 代码生成器AI版本发布,基于SpringBoot+AntDesign的JAVA快速开发平台

    此版本重点升级了 Online 代码生成器,支持更多的控件生成,所见即所得,极大的提高开发效率:同时做了数据库兼容专项工作,让 Online 开发兼容更多数据库:Mysql.SqlServer.Ora ...

  7. 在线Online表单来了!JeecgBoot 2.1 版本发布——基于SpringBoot+AntDesign的快速开发平台

    项目介绍 Jeecg-Boot 是一款基于SpringBoot+代码生成器的快速开发平台! 采用前后端分离架构:SpringBoot,Ant-Design-Vue,Mybatis,Shiro,JWT. ...

  8. 基于SpringBoot+AntDesign的快速开发平台,JeecgBoot 2.0.2 版本发布

    Jeecg-Boot 是一款基于SpringBoot+代码生成器的快速开发平台! 采用前后端分离架构:SpringBoot,Ant-Design-Vue,Mybatis,Shiro,JWT. 强大的代 ...

  9. 开发阶段,将SpringBoot应用快速部署到K8S

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

随机推荐

  1. ip辅助和别名的区别

    更流畅 IP 别名和辅助 IP 地址 2017-01-25 12:05             838人阅读             评论(0)             收藏              ...

  2. 篇三:XPath--解析Html

    导语:爬虫爬取的界面,大致分为静态界面.ajax异步加载.动态界面.静态界面直接获取HTML对象,然后使用XPath获取值 最有用的路径表达式: 表达式 描述 nodename 选取此节点的所有子节点 ...

  3. win7/win8下手工搭建WAMP环境

    win7/win8下手工搭建WAMP环境. 最近学习wamp,看了好多教程,出来好多问题,终于成功搞定,这里集合了一下最好的教程,写了一些自己的经验,希望大家有用 这里不能上传图片,我就写了个带pdf ...

  4. js学习笔记31----工厂方式

    工厂方式构造对象: 1.原料---构造函数,创建一个对象 “构造函数”,就是专门用来生成“对象”的函数.它提供模板,作为对象的基本结构.一个构造函数,可以生成多个对象,这些对象都有相同的结构.   2 ...

  5. 类的数据成员加前缀 m_(表示 member)

    类的数据成员加前缀 m_(表示 member),这样可以避免数据成员与 成员函数的参数同名. 例如: void Object::SetValue(int width, int height) { m_ ...

  6. linux常用命令中篇

    1.打印当月的日期

  7. Buff系统

    BUFF状态可以通过游戏道具.使用技能.被攻击技能.NPC.宠物等等实现.BUFF状态,有很多技能在释放后,会对目标产生一定时间的额外影响,这些影响有的是增益的,有的是减免的.比如法师的“熔岩地”,会 ...

  8. Spark Streaming源码分析 – Checkpoint

    PersistenceStreaming没有做特别的事情,DStream最终还是以其中的每个RDD作为job进行调度的,所以persistence就以RDD为单位按照原先Spark的方式去做就可以了, ...

  9. Android Bigmap的操作(绘制,裁剪)

    绘制 依据某一种颜色绘制Bitmap Bitmap resultBitmap = Bitmap.createBitmap(600,800,Bitmap.Config.ARGB_8888); resul ...

  10. Java 类设计----Java类的继承

    Java类的继承 为描述和处理个人信息,定义类Person: public class Person { public String name; public inat age; public Dat ...