luojinbai 2017-03-28
1.新建项目springboot.world
2.pom.xml加入父级依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent>
添加spring boot 的父级依赖,这样当前的项目就是spring boot项目了,spring-boot-starter-parent是一个特殊的starter,它用来提供相关的Maven默认依赖
3.pom.xml添加Web依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
加入这个,以下注解方可使用,至于为什么,用屁股想都应该清楚,这里就不详加叙述
@RestController
@RequestMapping("/simple")
............等等
4.写个简单的controller体验一把
package com.basic.demo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * desc:简单controller
 * @date 2017-03-28 11:28:56
 * @author 磨练中龙
 *
 */
@RestController
@RequestMapping("/simple")
public class SimpleController {
	@RequestMapping("/hello/{id}")
	public String hello(@PathVariable("id") Long id){
		return "你好啊:"+id;
	}
}
项目启动入口
package com.basic.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * desc:项目启动入口
 * @date 2017-03-28 11:29:39
 * @author 磨练中龙
 *
 */
@SpringBootApplication
public class App {
	
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}运行main方法,这样我们就启动项目了,还等什么,测试一把看看
输入地址:http://localhost:8080/simple/hello/666666
    效果如下:
 
成功!!!废话不多说。现在扯淡下
@SpringBootApplication是spring boot项目的核心注解,主要目的是开启自动配置。
@SpringBootApplication注解主要组合了@Configuration、@EnableAutoConfiguration、@ComponentScan
Spring Boot会自动扫描@SpringBootApplication 所在类的同级包,以及下级包里的Bean