spring-boot系列:初试spring-boot

部署父工程

<?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

 

posted @ 2016-05-17 11:29  FightingBoy&Gril  阅读(4626)  评论(1编辑  收藏  举报