POM 文件,注意红色部分:
4.0.0 spring-boot-starter-data-mongodb spring-boot-starter-data-mongodb 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-parent 1.5.1.RELEASE org.springframework.boot spring-boot-starter-web src org.springframework.boot spring-boot-maven-plugin repackage
程序入口:
package com.cui;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) throws Exception { System.out.println("Spring加载完后执行的逻辑,可以直接使用Spring上下文,以及依赖注入@Autowired"); }}
最简单的Web应用 (然而这并不是重点,仅仅是POM中spring-boot-starter-web组建的简单示例, 更多组件参考 ):
package com.cui;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/user")public class UserController { @RequestMapping("/{id}") public String view(@PathVariable("id") Long id) { return id + " ~~"; }}