springboot系列
springboot是spring的升级版,把传统的xml和property配置改为代码配置,使项目变得更简单易上手,学习springboot不需要有spring的基础,如果有更好,下面来看下怎么快速上手springboot吧。
使用maven构建springboot项目,我是用的eclipse开发,所以介绍下eclipse下的springboot的开发。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>1.5.3.RELEASE</version>
</dependency>
pom.xml下引入这两个依赖,分别是springboot的web容器和boot的测试jar,web容器在不指定的情况下默认使用内置tomcat启动,下面是代码启动boot项目的入口,右键run as javaapplication 就可以在控制台看到项目启动,默认端口是8080,可以自行修改哦,好了,介绍到这里,附代码可供下载,项目需配置maven环境和jdk。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RedisApplication {
public static void main(String[] args) {
SpringApplication.run(RedisApplication.class, args);
}
}
boot项目的测试包和测试用例
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class BootApplicationTests {
@Test
public void contextLoads() {
}
}
下面写个简单的例子
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping
@RestController
public class HelloController {
@RequestMapping
public Map<String,Object> hello(){
Map<String,Object> map=new HashMap<String,Object>();
map.put("name", "張三");
map.put("age", "25");
map.put("job", "程序猿");
return map;
}
}
* 启动项目访问 >http://127.0.0.1:8080 试试吧。想要了解更多关于springboot请大家关注我的博客。