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. python三大神器之virtualenv pip, virtualenv, fabric通称为pythoner的三大神器。

    python三大神器之virtualenv   pip, virtualenv, fabric通称为pythoner的三大神器. virtualenv virtualenv------用来建立一个虚拟 ...

  2. zend opcache的最佳设置

    2016-01-21 在网上无意中看到的一篇文章,这哥们非常简洁地谈论了zend opcache的最佳设置,他说他为此花了大量的时间探索zend opcache的每个设置选项的细节,甚至是阅读它的源代 ...

  3. *.ashx一般处理程序不能访问Session值的解决方法

    <%@ WebHandler Language="C#" Class="productHandler" %> using System; using ...

  4. HBase系统入门--整体介绍

    转自:http://www.aboutyun.com/thread-8957-1-2.html 问题导读:1.HBase查询与写入哪个更好一些?2.HBase面对复杂操作能否实现?3.Region服务 ...

  5. 《FPGA全程进阶---实战演练》第三章之接地设计

    信号回路的电位基准点,(直流电源的负极或者零伏点)在单板上可以分为数字地和模拟地.理想的工作地是电路参考点的等电位平面,然而在实际中,工作地被认为信号电流的低阻抗回路和电源的供电回路,这样就会有三个方 ...

  6. C 字符串操作函数

    针对C风格的字符串(char p[n];): 长度(strlen).追加(strcat, strncat).比较(strcmp, strncmp).查找(strchr, strstr)等. --带n的 ...

  7. 一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) set 注入

    一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) ...

  8. 嵌入式驱动开发之usb 无线网卡驱动---RT2870STA dm368

    RT2870STA 368的无线网卡驱动! http://www.linuxidc.com/Linux/2014-02/96979.htm

  9. Qt的窗口的最大化。

    1.window.showFullScreen()//此方法只对顶级窗口有效,对子窗口无效 QT中窗口部件QWidget成员函数showFullScreen();是用于将窗口部件全屏显示,但是他只对窗 ...

  10. javascript 高级选择器:querySelector 和 querySelectorAll

    querySelector 和 querySelectorAll 方法是 W3C Selectors API 规范中定义的.他们的作用是根据 CSS 选择器规范,便捷定位文档中指定元素. 目前几乎主流 ...