Spring boot是Spring推出的一个轻量化web框架,主要解决了Spring对于小型项目饱受诟病的配置和开发速度问题。

Spring Boot 包含的特性如下:

创建可以独立运行的 Spring 应用。

直接嵌入 Tomcat 或 Jetty 服务器,不需要部署 WAR 文件。

提供推荐的基础 POM 文件来简化 Apache Maven 配置。

尽可能的根据项目依赖来自动配置 Spring 框架。

提供可以直接在生产环境中使用的功能,如性能指标、应用信息和应用健康检查。

没有代码生成,也没有 XML 配置文件。

第一个SpringBoot应用:

1.构建maven项目

IDEA:

Create New Project -> Maven -> 选择JDK ->

GroupId : com.example, ArtifactId: springBootDemo -> Project name : springBootDemo

2.编制pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
<artifactId>springBootDemo</artifactId>
<version>1.0-SNAPSHOT</version> <!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent> <!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies> <!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

使用父pom虽然简单,但是有些情况我们已经有父pom,不能直接增加时,可以通过如下方式:

<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

3.编制Application.java存于myFirstProject\src\main\java\com\example下

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

4.编制Example.java存于myFirstProject\src\main\java\com\example\web下

package com.example.web;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@EnableAutoConfiguration
public class Example { @RequestMapping("/")
String home() {
return "Hello World!";
} @RequestMapping("/hello/{myName}")
String index(@PathVariable String myName) {
return "Hello " + myName;
}
}

5.运行

在Application.java文件上(或文件中) 选择Run 'Application '

如下图:

6.访问

运行成功后,打开浏览器,输入http://localhost:8080

页面展示Hello World!


更加快速的搭建:

spring提供了一个非常好的辅助工具,直接为我们生成Maven架构的工程。

一、在浏览器中打开http://start.spring.io/,如图

点击“Switch to the full version.”,勾选"web",然后点击“ Generate Project alt +”按钮,把文件保存到本地某个位置

二、下载文件导入IDEA,添加上文的Example

三、运行,打开浏览器,输入http://localhost:8080

页面展示Hello World!


注解详解:

Spring boot 中常用的注解如下:

@ResponseBody

用该注解修饰的函数,会将结果直接填充到HTTP的响应体中,一般用于构建RESTful的api;

@Controller

用于定义控制器类,在spring 项目中由控制器负责将用户发来的URL请求转发到对应的服务接口(service层)。

@RestController

@ResponseBody和@Controller的合集

@RequestMapping

提供路由信息,负责URL到Controller中的具体函数的映射。

@EnableAutoConfiguration

Spring Boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的Spring应用。例如,如果你的classpath下存在HSQLDB,并且你没有手动配置任何数据库连接beans,那么我们将自动配置一个内存型(in-memory)数据库”。你可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到一个@Configuration类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@EnableAutoConfiguration注解的排除属性来禁用它们.

@ComponentScan

表示将该类自动发现(扫描)并注册为Bean,可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。

@Configuration

相当于传统的xml配置文件,如果有些第三方库需要用到xml文件,建议仍然通过@Configuration类作为项目的配置主类——可以使用@ImportResource注解加载xml配置文件。

@SpringBootApplication

相当于@EnableAutoConfiguration、@ComponentScan和@Configuration的合集。

@Import

用来导入其他配置类。

@ImportResource

用来加载xml配置文件。

@Autowired

自动导入依赖的bean

@Service

一般用于修饰service层的组件

@Repository

使用@Repository注解可以确保DAO或者repositories提供异常转译,这个注解修饰的DAO或者repositories类会被ComponetScan发现并配置,同时也不需要为它们提供XML配置项。

快速搭建Spring Boot项目的更多相关文章

  1. 基于 intellij IDEA 快速搭建Spring Boot项目

           在<一步步搭建 Spring Boot maven 框架的工程>一文中,已经介绍了如何使用Eclipse快速搭建Spring Boot项目.由于最近将开发工具由Eclipse ...

  2. Spring boot入门(一):快速搭建Spring boot项目

    (一)Spring boot介绍 本部分摘自:https://www.zhihu.com/question/64671972/answer/223383505 Spring Boot是由Pivotal ...

  3. 构建微服务:快速搭建Spring Boot项目

    Spring Boot简介: Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员 ...

  4. Spring Boot系列学习文章(一) -- Intellij IDEA 搭建Spring Boot项目

    前言: 最近做的一个项目是用Spring Boot来做的,所以把工作中遇到的一些知识点.问题点整理一下,做成一系列学习文章,供后续学习Spring Boot的同仁们参考,我也是第一次接触Spring ...

  5. Myeclipse下使用Maven搭建spring boot项目

    开发环境:Myeclipse2017.JDK1.6.Tomcat 8.0.Myeclipse下使用Maven搭建spring boot项目,详细过程如下: 1. New -> Project.. ...

  6. Spring Boot入门(一):搭建Spring Boot项目

    从本篇博客开始,我们开始进入Spring Boot的世界,它的出现使Spring的开发变得更加简洁,因此一经推出受到众多程序员的喜爱. 作为Spring Boot系列的第一篇博客,我们先来讲解下如何搭 ...

  7. 使用IDEA,Eclispe搭建Spring Boot项目

    如何创建一个Spring Boot项目?这里使用maven来进行依赖管理,根据常用的IDE,可以使用IDEA.Eclipse.或者访问官方网站搭建. 项目搭建环境准备 JDK:1.8 MAVEN:3. ...

  8. 快速搭建Spring Boot + Apache Shiro 环境

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.Apache Shiro 介绍及概念 概念:Apache Shiro是一个强大且易用的Java安全框 ...

  9. Myeclipse下使用Maven搭建spring boot项目(第二篇)

    现在需要搭建spring boot框架,并实现一个HelloWorld的项目,让程序真正运行起来. 一.在pom.xml中引入spring-boot-start-parent,spring官方的叫st ...

随机推荐

  1. Linux系统(X32)安装Oracle11g完整安装图文教程另附基本操作

    一.修改操作系统核心参数 在Root用户下执行以下步骤: )修改用户的SHELL的限制,修改/etc/security/limits.conf文件 输入命令:vi /etc/security/limi ...

  2. 增加ubuntu的内存——设置Swap增加内存

    1.查看一下当前Swap分区的状态: $cat /proc/meminfo SwapTotal: 0 kB SwapFree: 0 kB 如果上面二项目都为0,说明没有Swap分区:如果不为0,则说明 ...

  3. struts2 jsp提交对象数据要这么干

    不要每个属性都 setter getter .. 这样页面很难看... 直接 把对象变成一个成员变量会比较好. Java code ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  4. 个人作业 - Week2 - 代码复审

    代码复审Check List 概要部分 代码能符合需求和规格说明么? 能完成1~1000000个数独的求解与生成,并能处理异常输入,满足需求. 代码设计是否有周全的考虑? 为输入单独开设了一个输入检测 ...

  5. 基于 Jmeter 的 web 端接口自动化测试平台

    简介 基于之前的react+unittest+flask的接口自动化平台开发经验,和趟过的坑,我重新开发了这个接口自动化平台.平台前端采用的antd+dva+umi的antd-pro模板,结合平台业务 ...

  6. Longest Substring with At Most Two Distinct

    Given a string, find the length of the longest substring T that contains at most 2 distinct characte ...

  7. BZOJ3444 最后的晚餐(并查集)

    容易发现只要图中有非链部分则无解.剩下就非常简单了. #include<iostream> #include<cstdio> #include<cmath> #in ...

  8. BZOJ3252 攻略(贪心+dfs序+线段树)

    考虑贪心,每次选价值最大的链.选完之后对于链上点dfs序暴力修改子树.因为每个点最多被选一次,复杂度非常正确. #include<iostream> #include<cstdio& ...

  9. 小Z的袜子(hose) HYSBZ - 2038 (莫队算法)

    原文地址:http://hzwer.com/2782.html 「BZOJ2038」[2009国家集训队] 小Z的袜子(hose) 2014年4月26日13,34721 Description 作为一 ...

  10. oracle 慢查询

    一.查询执行最慢的sql select * from (select sa.SQL_TEXT, sa.SQL_FULLTEXT, sa.EXECUTIONS "执行次数", , ) ...