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. window 窗口编辑

    package com.chuangkohua; import java.awt.FileDialog; import java.awt.FlowLayout; import java.awt.Fra ...

  2. Alpha 冲刺报告3

    队名 massivehard 组员一(组长:晓辉) 今天完成了哪些任务 .整理昨天的两个功能,补些bug 写了一个初步的loyaut github 还剩哪些任务: 后台的用来处理自然语言的服务器还没架 ...

  3. Alpha版本冲刺(四)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:何家伟 组员8:政演 组员9:鸿杰 组员10:刘一好 组员:何宇恒 展示组内最新 ...

  4. 在win7 64位操作系统下 arduino驱动安装问题的解决

    主要参考以下两个博客: 操作教程:http://blog.csdn.net/u013926582/article/details/24442583 文件下载:http://www.arduino.cn ...

  5. [BUAA_SE_2017]个人作业-Week1

    个人作业-Week1 疑问 教材中说,PM在衡量需求时需要方方面面的能力与研究.可是,当下许多互联网IT公司只承担外包业务,即客户给什么需求就实现什么需求,甚至可能不要求其它先进的功能.此时,开发团队 ...

  6. 服务 在初始化安装时发生异常:System.IO.FileNotFoundException: 未能加载文件或******

    这个问题是在用installutil.exe安装服务时候碰到的 解决方法就是把installutil.exe文件直接复制到要安装的目录下 installutil.exe的所在位置 windows/mi ...

  7. Linux adduser

  8. python web调用docker-py

    在 /etc/init.d/docker的start()函数末尾加入:chmod 777 /var/run/docker.sock 否则web程序会没有权限去操作  

  9. ACM数论之旅14---抽屉原理,鸽巢原理,球盒原理(叫法不一又有什么关系呢╮(╯▽╰)╭)

    这章没有什么算法可言,单纯的你懂了原理后会不会运用(反正我基本没怎么用过 ̄ 3 ̄) 有366人,那么至少有两人同一天出生(好孩子就不要在意闰年啦( ̄▽ ̄")) 有13人,那么至少有两人同一月 ...

  10. Android ComponentName的用法

    ComponentName(组件名称)是用来打开其他应用程序中的Activity或服务的. 用法: Intent it=new Intent(); it.setComponent(new Compon ...