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

spring boot 入门

2016-06-13 18:38 357 查看
 

一.使用 spring boot  好处?

1.使用spring boot 搭建项目,丢掉了繁冗的xml 配置文件,甚至连web.xml 都不要。使用注解,看起来很清爽,而且搭建的速度非常的快。

2.使项目变得更加管理。

3.应该还有很多好处,我还没有发现,所以记录下这边文章,开启发现之旅。

二.准备:我的环境Jdk1.8 +maven +tomcat8 新建maven 项目

springboot 默认是使用用tomcat8 容器,需要java7+ 才能支持。

项目建好好配置pom.xml 文件,下载spring boot 依赖的包即可

三.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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.it.guo</groupId>
<artifactId>sboot</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>sboot Maven Webapp</name>
<url>http://maven.apache.org</url>

<!-- spring-boot-starter-parent 包含了以下信息 - -->
<!--
1、使用java6编译级别
2、使用utf-8编码
3、实现了通用的测试框架 (JUnit, Hamcrest, Mockito).
4、智能资源过滤
5、智能的插件配置(exec plugin, surefire, Git commit ID, shade).
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath />
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring boot 核心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入web 模块 需要添加 -->
<!-- http://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>

</dependencies>
<build>
<finalName>sboot</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

四.代码编写
@RestController
@EnableAutoConfiguration
public class HelloController {

@RequestMapping("/hello")
public String say() {
System.err.println("这是我的第一个spring boot!");
return "hello word wo wowo";
}

public static void main(String[] args) {
SpringApplication.run(HelloController.class, args);
}
}


注:

SpringApplication.run
函数传入的HelloController
.class
类就是要读取的配置类,该函数通过传入配置类来进行Spring的基础配置加载。

SpringApplication.run(HelloController.class,
args);可以传入多个类,可以加载多个配置的class,从这里可以启动tomcat
.但是我不知道为什
么他可以启动Tomcat 需要看看为什么?

五.运行:
//
执行main 函数,启动tomcat 然后在浏览器输入localhost:8080/hello



六.结束

大功告成,创建一个web 项目就是这么的简单。

参考:

官网demo 下载地址如下:http://start.spring.io/
官网入门例子:http://projects.spring.io/spring-boot/#quick-start
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: