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

spring boot 加快springmvc开发

2016-11-25 13:14 253 查看
最近接触了spring boot对其理念非常认同,spring 4.0提倡约定优于配置,spring boot对spring的配置进行简化,几乎零配置。同时对spring
需要的jar 也进行了整合,解决jar冲突的问题。下面是从spring boot 官方的开源代码,写的demo,spring boot github地址是(https://github.com/spring-projects/spring-boot)在sample是一些简单的demo.
package org.peng.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

/**
* Created by caicai on 2016/6/15.
*/
@Controller
public class SampleController {
@Value("${application.message:Hello World}")
String message="hello spring boot";
@RequestMapping("/")
@ResponseBody
String hello(){
return "Hello World";
}
@RequestMapping("/user")
String userInfo(Map<String,Object> model){
model.put("time",new Date());
model.put("message", message);
return "user_add";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

上面的controller就是简单的controller,然后@Value就是将application的值赋值message
package org.peng.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

/**
* Created by caicai on 2016/6/21.
*/
@SpringBootApplication
public class SampleApplication extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SampleApplication.class);
}

public static void main(String[] args) throws Exception {
SpringApplication.run(SampleApplication.class, args);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

上面是spring boot 的项目启动的,是用main方法启动的
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<h1>${message}</h1>
<h2>time:${time}</h2>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10

application.properties是这个配置
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
application.message=Hello Phil
1
2
3
1
2
3

spring boot的启动的图也很有特点 



本人用的是idea +maven写的项目 

完整代码在Git@oschina,项目同时是开源的希望大家帮忙完善ssh
git连接 

持续更新中………
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: