最近一直在看spring boot的相关内容,在网上阅读了很多相关的文章,非常感谢他们,在此把自己的一些操作记录下来,希望可以帮到后面学习的人。
整个测试项目完成后的项目结构如图

在eclipse中新建一个maven项目,创建完成后,修改pom.xml文件,pom.xml文件
<pre name="code" class="html"><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.waylon</groupId>
<artifactId>spring-boot-simple</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-simple</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<properties>
<!-- 设置spring-boot的版本,java编码的格式,以及jdk的版本,防止每次update project时jdk的版本总是变成1.5 -->
<spring.boot.version>1.3.3.RELEASE</spring.boot.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
保存编译等所有架包下载完成之后,项目仍然有红叉,更新项目“maven” --> “update project”
完成之后创建一个测试文件Application.java,一般放在根包下
/****************************************
* Copyright (c) 2016 NiuWa.
* All rights reserved.
* Created on 2016年3月3日
*
* Contributors:
* BWaylon - initial implementation
****************************************/
package com.waylon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @title Application.java
* 项目启动main方法,一般单独放在根包下面
* @author BWaylon
* @version 1.0
* @created 2016年3月3日
*/
@SpringBootApplication //same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
在Controller包下创建一个测试的类SampleController.java
/****************************************
* Copyright (c) 2016 NiuWa.
* All rights reserved.
* Created on 2016年3月3日
*
* Contributors:
* BWaylon - initial implementation
****************************************/
package com.waylon.controller;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
* @title SampleController.java
*
* @author BWaylon
* @version 1.0
* @created 2016年3月3日
*/
@RestController
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/index")
public String home() {
return "hello world!";
}
}
完成后在Application.java中直接运行main方法,启动成功后控制台输出
打开浏览器,输入地址即可访问