最近接触了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";
}
}
上面的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);
}
}
上面是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>
application.properties是这个配置
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
application.message=Hello Phil
spring boot的启动的图也很有特点
本人用的是idea +maven写的项目
完整代码在git@oschina,项目同时是开源的希望大家帮忙完善ssh git连接
持续更新中………