暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

SpringBoot HelloWorld实现

秃头架构 2018-09-02
212

随着时代进步Spring boot框架也火了一把成为开发主流框架之一,不稍微熟习下Spring boot都不好意思出门了,于是我也试着看下Spring boot的相关配置。

概述:

  Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。一句话概括,“spring boot它就是一些人为了快速配置一套系统框架而进行的封装,里面根据各种类型的项目功能封装了各种模块框架的引用,广大老百姓在使用的时候就不需要一一的去添加具体非常细的引用了,只需要添加他们封装好的spring boot相关模块插件即可spring boot其实不是什么新的框架,就如同一个盒子,盒子里面装了很多的细致的jar,我们只要引用这个盒子就可以实现各种框架引用与搭建了!


相关资源

官网地址:http://projects.spring.io/spring-boot/

创建maven项目

配置工具:

1、JDK1.7

2、Maven(也可以使用非maven工具搭建)

3、IDE(Eclipse、IntelliJ 或者其它的)


代码配置:

1、新建一个maven项目,可以是web工程也可以是java基本工程


2、pom.xml中配置

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>


3、新建个java类

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;



@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最最简单的配置,

运行该main方法


  1. .   ____          _            __ _ _

  2. /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \

  3. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

  4. \\/  ___)| |_)| | | | | || (_| |  ) ) ) )

  5. '  |____| .__|_| |_|_| |_\__, | / / / /

  6. =========|_|==============|___/=/_/_/_/

  7. :: Spring Boot ::        (v1.4.0.RELEASE)


  8. 2017-08-30 16:05:10.511  INFO 5180 --- [           main] c.s.project.controller.SampleController  : Starting SampleController on lenovo2017 with PID 5180 (E:\Eclipse4_WorkPlace\spring_boot\spring_boot_first\target\classes started by lenovo in E:\Eclipse4_WorkPlace\spring_boot\spring_boot_first)

  9. 2017-08-30 16:05:10.512  INFO 5180 --- [           main] c.s.project.controller.SampleController  : No active profile set, falling back to default profiles: default

  10. 2017-08-30 16:05:10.540  INFO 5180 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1b4e829: startup date [Wed Aug 30 16:05:10 CST 2017]; root of context hierarchy

  11. 2017-08-30 16:05:11.371  INFO 5180 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)

  12. 2017-08-30 16:05:11.378  INFO 5180 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat

  13. 2017-08-30 16:05:11.379  INFO 5180 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.4

  14. 2017-08-30 16:05:11.441  INFO 5180 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/aaa]    : Initializing Spring embedded WebApplicationContext

  15. 2017-08-30 16:05:11.441  INFO 5180 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 902 ms

  16. 2017-08-30 16:05:11.554  INFO 5180 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]

  17. 2017-08-30 16:05:11.557  INFO 5180 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]

  18. 2017-08-30 16:05:11.557  INFO 5180 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]

  19. 2017-08-30 16:05:11.558  INFO 5180 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]

  20. 2017-08-30 16:05:11.558  INFO 5180 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]

  21. 2017-08-30 16:05:12.066  INFO 5180 --- [ost-startStop-1] o.a.c.util.SessionIdGeneratorBase        : Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [495] milliseconds.

  22. 2017-08-30 16:05:12.301  INFO 5180 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1b4e829: startup date [Wed Aug 30 16:05:10 CST 2017]; root of context hierarchy

  23. 2017-08-30 16:05:12.346  INFO 5180 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String com.sam.project.controller.SampleController.home()

  24. 2017-08-30 16:05:12.351  INFO 5180 --- [           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)

  25. 2017-08-30 16:05:12.352  INFO 5180 --- [           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)

  26. 2017-08-30 16:05:12.370  INFO 5180 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

  27. 2017-08-30 16:05:12.370  INFO 5180 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

  28. 2017-08-30 16:05:12.397  INFO 5180 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

  29. 2017-08-30 16:05:12.486  INFO 5180 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup

  30. 2017-08-30 16:05:12.530  INFO 5180 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)

  31. 2017-08-30 16:05:12.532  INFO 5180 --- [           main] c.s.project.controller.SampleController  : Started SampleController in 2.398 seconds (JVM running for 2.642)




可以看到上面打印了各种信息,表示启动成功!


浏览器访问

http://localhost:8080/


即可看到输出hello world信息!


相关说明:


@EnableAutoConfiguration 和 SpringApplication 。

1、@EnableAutoConfiguration 用于自动配置。它会根据你的pom配置(实际上应该是根据具体的依赖)来判断这是一个什么应用,并创建相应的环境。

在上面这个例子中,@EnableAutoConfiguration 会判断出这是一个web应用,所以会创建相应的web环境。

 

2、SpringApplication 则是用于从main方法启动Spring应用的类。默认,它会执行以下步骤

  1. 创建一个合适的ApplicationContext实例 (取决于classpath)。

  2. 注册一个CommandLinePropertySource,以便将命令行参数作为Spring properties。

  3. 刷新application context,加载所有单例beans。

  4. 激活所有CommandLineRunner beans。


文章转载自秃头架构,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论