部署父工程

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.hadu.try</groupId>
<artifactId>try-spring-boot</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>try-web</module>
</modules> <!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent> </project>

  建立一个父工程,方便接下来分模块尝试spring boot。在父工程中引入spring boot的默认pom:spring-boot-starter-parent。


建立web子模块

pom配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <artifactId>try-spring-boot</artifactId>
<groupId>com.hadu.try</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>try-web</artifactId> <!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies> <!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

引入spring boot的web boot,接下来去定义controller就可以快速建立一个web程序,完全避开web复杂的xml配置。

第一个controller

/**
* Created by lili on 16/5/16.
*/
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*; @Controller
@EnableAutoConfiguration
public class SampleController { @RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
} public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}

spring boot的神奇之处就在这里,我们完全可以按照普通java程序一样去run,不需要将web应用打包到诸如tomcat的web容器中就可以启动web应用了。

@EnableAutoConfiguration 这个注解让spring boot根据你引入jar的依赖关系去猜测你需要构建的程序,由于spring-boot-starter-web添加了Tomcat和Spring MVC

的jar,所以这里spring boot就认为你要构建一个web应用,所以会自动为你配置web相关的一些配置。


效果演示

run起来的效果如下:

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.5.RELEASE) 2016-05-17 11:25:28.359 INFO 1571 --- [ main] SampleController : Starting SampleController on Lilis-MacBook-Pro.local with PID 1571 (started by lili in /Users/lili/百度云同步盘/gitDir/try-spring-boot)
2016-05-17 11:25:28.364 INFO 1571 --- [ main] SampleController : No active profile set, falling back to default profiles: default
2016-05-17 11:25:28.522 INFO 1571 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@17baae6e: startup date [Tue May 17 11:25:28 CST 2016]; root of context hierarchy
2016-05-17 11:25:31.307 INFO 1571 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-05-17 11:25:31.335 INFO 1571 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-05-17 11:25:31.337 INFO 1571 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.33
2016-05-17 11:25:31.621 INFO 1571 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2016-05-17 11:25:31.622 INFO 1571 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3109 ms
2016-05-17 11:25:32.243 INFO 1571 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2016-05-17 11:25:32.252 INFO 1571 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-05-17 11:25:32.253 INFO 1571 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-05-17 11:25:32.253 INFO 1571 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-05-17 11:25:32.253 INFO 1571 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2016-05-17 11:25:32.925 INFO 1571 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@17baae6e: startup date [Tue May 17 11:25:28 CST 2016]; root of context hierarchy
2016-05-17 11:25:33.178 INFO 1571 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String SampleController.home()
2016-05-17 11:25:33.181 INFO 1571 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-05-17 11:25:33.181 INFO 1571 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-05-17 11:25:33.244 INFO 1571 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-05-17 11:25:33.244 INFO 1571 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-05-17 11:25:33.302 INFO 1571 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-05-17 11:25:33.490 INFO 1571 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-05-17 11:25:33.618 INFO 1571 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-05-17 11:25:33.624 INFO 1571 --- [ main] SampleController : Started SampleController in 6.365 seconds (JVM running for 7.385)

下面来看看这个项目的依赖:

[INFO] com.hadu.try:try-web:jar:1.0-SNAPSHOT
[INFO] \- org.springframework.boot:spring-boot-starter-web:jar:1.3.5.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter:jar:1.3.5.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot:jar:1.3.5.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:1.3.5.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:1.3.5.RELEASE:compile
[INFO] | | +- ch.qos.logback:logback-classic:jar:1.1.7:compile
[INFO] | | | +- ch.qos.logback:logback-core:jar:1.1.7:compile
[INFO] | | | \- org.slf4j:slf4j-api:jar:1.7.21:compile
[INFO] | | +- org.slf4j:jcl-over-slf4j:jar:1.7.21:compile
[INFO] | | +- org.slf4j:jul-to-slf4j:jar:1.7.21:compile
[INFO] | | \- org.slf4j:log4j-over-slf4j:jar:1.7.21:compile
[INFO] | +- org.springframework:spring-core:jar:4.2.6.RELEASE:compile
[INFO] | \- org.yaml:snakeyaml:jar:1.16:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.3.5.RELEASE:compile
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.0.33:compile
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.0.33:compile
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:8.0.33:compile
[INFO] | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.0.33:compile
[INFO] +- org.springframework.boot:spring-boot-starter-validation:jar:1.3.5.RELEASE:compile
[INFO] | \- org.hibernate:hibernate-validator:jar:5.2.4.Final:compile
[INFO] | +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] | +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
[INFO] | \- com.fasterxml:classmate:jar:1.1.0:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.6.6:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.6.6:compile
[INFO] | \- com.fasterxml.jackson.core:jackson-core:jar:2.6.6:compile
[INFO] +- org.springframework:spring-web:jar:4.2.6.RELEASE:compile
[INFO] | +- org.springframework:spring-aop:jar:4.2.6.RELEASE:compile
[INFO] | | \- aopalliance:aopalliance:jar:1.0:compile
[INFO] | +- org.springframework:spring-beans:jar:4.2.6.RELEASE:compile
[INFO] | \- org.springframework:spring-context:jar:4.2.6.RELEASE:compile
[INFO] \- org.springframework:spring-webmvc:jar:4.2.6.RELEASE:compile
[INFO] \- org.springframework:spring-expression:jar:4.2.6.RELEASE:compile

spring-boot系列:初试spring-boot的更多相关文章

  1. Spring框架系列(2) - Spring简单例子引入Spring要点

    上文中我们简单介绍了Spring和Spring Framework的组件,那么这些Spring Framework组件是如何配合工作的呢?本文主要承接上文,向你展示Spring Framework组件 ...

  2. Spring框架系列(6) - Spring IOC实现原理详解之IOC体系结构设计

    在对IoC有了初步的认知后,我们开始对IOC的实现原理进行深入理解.本文将帮助你站在设计者的角度去看IOC最顶层的结构设计.@pdai Spring框架系列(6) - Spring IOC实现原理详解 ...

  3. Spring框架系列(7) - Spring IOC实现原理详解之IOC初始化流程

    上文,我们看了IOC设计要点和设计结构:紧接着这篇,我们可以看下源码的实现了:Spring如何实现将资源配置(以xml配置为例)通过加载,解析,生成BeanDefination并注册到IoC容器中的. ...

  4. Spring框架系列(8) - Spring IOC实现原理详解之Bean实例化(生命周期,循环依赖等)

    上文,我们看了IOC设计要点和设计结构:以及Spring如何实现将资源配置(以xml配置为例)通过加载,解析,生成BeanDefination并注册到IoC容器中的:容器中存放的是Bean的定义即Be ...

  5. Spring框架系列(9) - Spring AOP实现原理详解之AOP切面的实现

    前文,我们分析了Spring IOC的初始化过程和Bean的生命周期等,而Spring AOP也是基于IOC的Bean加载来实现的.本文主要介绍Spring AOP原理解析的切面实现过程(将切面类的所 ...

  6. Spring框架系列(10) - Spring AOP实现原理详解之AOP代理的创建

    上文我们介绍了Spring AOP原理解析的切面实现过程(将切面类的所有切面方法根据使用的注解生成对应Advice,并将Advice连同切入点匹配器和切面类等信息一并封装到Advisor).本文在此基 ...

  7. Spring框架系列(11) - Spring AOP实现原理详解之Cglib代理实现

    我们在前文中已经介绍了SpringAOP的切面实现和创建动态代理的过程,那么动态代理是如何工作的呢?本文主要介绍Cglib动态代理的案例和SpringAOP实现的原理.@pdai Spring框架系列 ...

  8. Spring框架系列(12) - Spring AOP实现原理详解之JDK代理实现

    上文我们学习了SpringAOP Cglib动态代理的实现,本文主要是SpringAOP JDK动态代理的案例和实现部分.@pdai Spring框架系列(12) - Spring AOP实现原理详解 ...

  9. Spring Boot系列(一) Spring Boot准备知识

    本文是学习 Spring Boot 的一些准备知识. Spring Web MVC Spring Web MVC 的两个Context 如下图所示, 基于 Servlet 的 Spring Web M ...

  10. 【Spring Boot && Spring Cloud系列】Spring Boot的启动器Starter

    Spring Boot的内置Servlet Container: Name Servlet Version Java Version Tomcat8 3.1 Java 7+ Tomcat7 3.0 J ...

随机推荐

  1. 通俗易懂的讲解iphone视图控制器的生命周期

    IOS 视图控制器的生命周期非常非常重要,所以我有必要写个文章来和大家一起探讨问题. 今天在学习视图控制器的生命周期,也看了一下网上的一些资料,但总觉得不是那么好理解,首先我们来看一张图: 先粗略讲一 ...

  2. hdu 3359 Kind of a Blur (高斯消元 浮点型)

    题目链接 题意: H * W (W,H <= 10) 的矩阵A的某个元素A[i][j],从它出发到其他点的曼哈顿距离小于等于D的所有值的和S[i][j]除上可达点的数目,构成了矩阵B.给定矩阵B ...

  3. POJ 2516 最小费用流

    依然最小费用最大流模板题 建边麻烦了些 #include <cstdio> #include <cstring> #include <iostream> #incl ...

  4. UVa 11774 (置换 找规律) Doom's Day

    我看大多数人的博客只说了一句:找规律得答案为(n + m) / gcd(n, m) 不过神题的题解还须神人写.. We can associate at each cell a base 3-numb ...

  5. UVa 11889 (GCD) Benefit

    好吧,被大白书上的入门题给卡了.=_=|| 已知LCM(A, B) = C,已知A和C,求最小的B 一开始我想当然地以为B = C / A,后来发现这时候的B不一定满足gcd(A, B) = 1 A要 ...

  6. UVa 10003 (可用四边形不等式优化) Cutting Sticks

    题意: 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用. 分析: d(i, j)表示切割第i个切点到第j个切点这段所需的最小费用.则有d(i, j) = ...

  7. UVa 11389 (贪心) The Bus Driver Problem

    题意: 有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线. 给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d×r. 问如何分配路线才能使加班费最少. 分析: 感 ...

  8. android webview js alert对话框 不能弹出 解决办法

    在配置了webview的 setting属性后,以前设置的都是可以直接弹出来的,今天写一个小demo时候莫名其妙的发现alert怎么也出来,即使设置了这么多也不行: webSettings.setJa ...

  9. Share SDK 第三方登录

    import java.util.HashMap; import org.apache.http.Header; import android.app.Activity; import android ...

  10. uvalive 4255 Guess(拓扑排序)

    算好题目,反正我没想到可以用图论做(虽然现在做的是图论专题= =) 首先是要把求每个位置上的值转化为求 “前缀和之差”,这是一个很有用的技巧 其次,由输入的(n+(n-1)+...+2+1)个符号,可 ...