您的位置:首页 > 编程语言 > Java开发

spring boot快速入门

2017-04-23 15:30 393 查看
opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.You can use Spring Boot to create Java applications that can be started using java -jar or more trbaditional war deployments. We also provide a command line tool that runs “spring scripts”. ——来自 spring-boot-reference

spring boot 使得开发人员创建一个独立的,标准的,基于spring,并能够正常运行的应用变得无比简单。这使得你能够用最少的烦恼 去整合spring平台和第三方的jar包。 基本上的spring boot 只拥有极少数的配置。你能够使用spring boot 去创建java应用通过一个fat jar,或者传统的war部署。我们还提供了命令行工具去执行sping 脚本。

2.系统支持

spring boot 1.5.2.RELEASE 需要java 7 和spring framework 4.3.7.RELEASE或者以上

而且还提供了对maven 3.2+和gradle 2.9+支持。

3.我使用的IDEA + maven

使用的工具是IDEA2017版

maven 3.3.9

4.开始搭建第一个spring boot 项目

导入srping boot可谓是非常简单的,只需要新建一个maven普通项目,并导入spring boot的依赖就可以了。比如

<properties>
<spring.boot.version>1.5.2.RELEASE</spring.boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
</plugin>
</plugins>
</build>


在spring boot 知道文档中提到的启动方式是直接引用

<?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>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.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>


spring-boot-starter-parent是srping boot是父类,但是有时候需要引入自己的父类,所以并不适合,我个人喜欢依赖管理大方式引入spring boot来管理spring boot版本。

在pom文件引入之后,需要添加一个spring boot的启动类。

在官方文档中的案例:

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}


@EnableAutoConfiguration指明这个是springboot的启动入口。

@RestController是mvc controller注解。

在配置一个main方法。通过main 方法启动该boot ,并加载一个controller类。

spring boot 是注入tomcat的方式加载web 容器。这个在spring-boot-starter-web这个pom文件中指出了对tomcat的依赖和对spring mvc的依赖。

项目启动完成之后。访问localhost:8080 能访问到该web 容器。

事实上一般项目启动main方法不会在controller中,所以这时候光@EnableAutoConfiguration是不够的。需要其他两个注解

@ComponentScan 配置注解扫描,用法可自行百度

@Configuration 这个是将该类作为配置类注入,可不需要。

这个三个注解可归为一个类

@SpringBootApplication 。只需要这个注解并可以完成spring boot 项目的配置





默认扫描该路劲下所有的包。

spring boot 的配置。

spring boot配置文件默认支持yml或者properties两种格式。如果你想配置web访问端口,只需要创建配置文件application.yml。并在文件中配置



即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring boot 入门