快速构建--Spring-Boot (quote)

阅读更多

Spring Boot使我们更容易去创建基于Spring的独立和产品级的可以”即时运行“的应用和服务。支持约定大于配置,目的是尽可能快地构建和运行Spring应用。

 

之前我们创建基于Spring的项目需要考虑添加哪些Spring依赖和第三方的依赖。使用Spring Boot后,我们可以以最小化的依赖开始spring应用。大多数Spring Boot应用需要很少的配置即可运行,比如我们可以创建独立独立大Java应用,然后通过java -jar运行启动或者传统的WAR部署。其也提供了命令行工具来直接运行Spring脚本(如groovy脚本)。也就是说Spring Boot让Spring应用从配置到运行变的更加简单,但不对Spring本身提供增强(即额外的功能)。

 

目的:

让所有Spring开发变得更快,且让更多的人更快的进行Spring入门体验,提供“starter” POM来简化我们的Maven配置(也就是说使用Spring Boot只有配合maven/gradle等这种依赖管理工具才能发挥它的能力),不像以前,构建一个springmvc项目需要进行好多配置等

开箱即用,快速开始需求开发而不被其他方面影响(如果可能会自动配置Spring)

 

提供一些非功能性的常见的大型项目类特性(如内嵌服务器、安全、度量、健康检查、外部化配置),如可以直接地内嵌Tomcat/Jetty(不需要单独去部署war包)

绝无代码生成,且无需XML配置

 

我的构建环境

JDK 7

Maven 3

Servlet3容器 

 

创建项目

首先使用Maven创建一个普通Maven应用即可,不必是web的。

 

添加Spring Boot相关POM配置

在pom.xml中添加如下配置

查看 复制到剪贴板 打印
  1.   
  2.   
  3.     org.springframework.boot   
  4.     spring-boot-starter-parent   
  5.     0.5.0.BUILD-SNAPSHOT   
  6.   
  7.   
  8.   
  9.   
  10.        
  11.         org.springframework.boot   
  12.         spring-boot-starter-web   
  13.        
  14.   
  15.   
  16.   
  17.   
  18.        
  19.            
  20.             org.springframework.boot   
  21.             spring-boot-maven-plugin   
  22.            
  23.        
  24.   
  25.   
  26.   
  27.   
  28.   
  29.        
  30.         spring-snapshots   
  31.         http://repo.spring.io/snapshot   
  32.         true   
  33.        
  34.        
  35.         spring-milestones   
  36.         http://repo.spring.io/milestone   
  37.         true   
  38.        
  39.   
  40.   
  41.        
  42.         spring-snapshots   
  43.         http://repo.spring.io/snapshot   
  44.        
  45.        
  46.         spring-milestones   
  47.         http://repo.spring.io/milestone   
  48.        
  49.   
    
    
        org.springframework.boot
        spring-boot-starter-parent
        0.5.0.BUILD-SNAPSHOT
    

    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

    
    
    
        
            spring-snapshots
            http://repo.spring.io/snapshot
            true
        
        
            spring-milestones
            http://repo.spring.io/milestone
            true
        
    
    
        
            spring-snapshots
            http://repo.spring.io/snapshot
        
        
            spring-milestones
            http://repo.spring.io/milestone
        
    

继承spring-boot-starter-parent后我们可以继承一些默认的依赖,这样就无需添加一堆相应的依赖,把依赖配置最小化;spring-boot-starter-web提供了对web的支持,spring-boot-maven-plugin提供了直接运行项目的插件,我们可以直接mvn spring-boot:run运行。

 

实体

查看 复制到剪贴板 打印
  1. package com.sishuok.entity;   
  2.   
  3. /**  
  4.  * 

    User: Zhang Kaitao  

  5.  * 

    Date: 13-12-22  

  6.  * 

    Version: 1.0  

  7.  */  
  8. public class User {   
  9.     private Long id;   
  10.     private String name;   
  11.   
  12.     public Long getId() {   
  13.         return id;   
  14.     }   
  15.   
  16.     public void setId(Long id) {   
  17.         this.id = id;   
  18.     }   
  19.   
  20.     public String getName() {   
  21.         return name;   
  22.     }   
  23.   
  24.     public void setName(String name) {   
  25.         this.name = name;   
  26.     }   
  27.   
  28.     @Override  
  29.     public boolean equals(Object o) {   
  30.         if (this == o) return true;   
  31.         if (o == null || getClass() != o.getClass()) return false;   
  32.   
  33.         User user = (User) o;   
  34.   
  35.         if (id != null ? !id.equals(user.id) : user.id != nullreturn false;   
  36.   
  37.         return true;   
  38.     }   
  39.   
  40.     @Override  
  41.     public int hashCode() {   
  42.         return id != null ? id.hashCode() : 0;   
  43.     }   
  44. }  
package com.sishuok.entity;

/**
 * 

User: Zhang Kaitao *

Date: 13-12-22 *

Version: 1.0 */ public class User { private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (id != null ? !id.equals(user.id) : user.id != null) return false; return true; } @Override public int hashCode() { return id != null ? id.hashCode() : 0; } }

 

控制器

查看 复制到剪贴板 打印
  1. package com.sishuok.controller;   
  2.   
  3. import com.sishuok.entity.User;   
  4. import org.springframework.boot.SpringApplication;   
  5. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;   
  6. import org.springframework.web.bind.annotation.PathVariable;   
  7. import org.springframework.web.bind.annotation.RequestMapping;   
  8. import org.springframework.web.bind.annotation.RestController;   
  9.   
  10. /**  
  11.  * 

    User: Zhang Kaitao  

  12.  * 

    Date: 13-12-22  

  13.  * 

    Version: 1.0  

  14.  */  
  15. //@EnableAutoConfiguration   
  16. @RestController  
  17. @RequestMapping("/user")   
  18. public class UserController {   
  19.   
  20.     @RequestMapping("/{id}")   
  21.     public User view(@PathVariable("id") Long id) {   
  22.         User user = new User();   
  23.         user.setId(id);   
  24.         user.setName("zhang");   
  25.         return user;   
  26.     }   
  27.   
  28.     //public static void main(String[] args) {   
  29.     //    SpringApplication.run(UserController.class);   
  30.     //}   
  31.   
  32. }  
package com.sishuok.controller;

import com.sishuok.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 

User: Zhang Kaitao *

Date: 13-12-22 *

Version: 1.0 */ //@EnableAutoConfiguration @RestController @RequestMapping("/user") public class UserController { @RequestMapping("/{id}") public User view(@PathVariable("id") Long id) { User user = new User(); user.setId(id); user.setName("zhang"); return user; } //public static void main(String[] args) { // SpringApplication.run(UserController.class); //} }

 

运行  

第一种方式

通过在UserController中加上@EnableAutoConfiguration开启自动配置,然后通过SpringApplication.run(UserController.class);运行这个控制器;这种方式只运行一个控制器比较方便;

第二种方式

通过@Configuration+@ComponentScan开启注解扫描并自动注册相应的注解Bean

查看 复制到剪贴板 打印
  1. package com.sishuok;   
  2.   
  3. import com.sishuok.controller.UserController;   
  4. import org.springframework.boot.SpringApplication;   
  5. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;   
  6. import org.springframework.context.annotation.ComponentScan;   
  7. import org.springframework.context.annotation.Configuration;   
  8.   
  9. /**  
  10.  * 

    User: Zhang Kaitao  

  11.  * 

    Date: 13-12-22  

  12.  * 

    Version: 1.0  

  13.  */  
  14. @Configuration  
  15. @ComponentScan  
  16. @EnableAutoConfiguration  
  17. public class Application {   
  18.     public static void main(String[] args) {   
  19.         SpringApplication.run(Application.class);   
  20.     }   
  21. }  
package com.sishuok;

import com.sishuok.controller.UserController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * 

User: Zhang Kaitao *

Date: 13-12-22 *

Version: 1.0 */ @Configuration @ComponentScan @EnableAutoConfiguration public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); } }

到此,一个基本的REST风格的web应用就构建完成了。

 

地址栏输入http://localhost:8080/user/1即可看到json结果。

 

 

如果大家查看其依赖,会发现自动添加了需要相应的依赖(不管你用/不用),但是开发一个应用确实变得非常快速,对于想学习/体验Spring的新手,快速建立项目模型等可以考虑用这种方式。当然如果不想依赖这么多的jar包,可以去掉parent,然后自己添加依赖。 

 

欢迎加入spring群134755960进行交流。

 

参考

https://github.com/spring-projects/spring-boot

 

form :http://sishuok.com/forum/posts/list/7870.html

你可能感兴趣的:(Spring,Boot,Maven)