码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot(1):Guide

时间:2018-01-26 20:56:50      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:ann   ref   color   conf   生产   content   rgs   []   world   

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置

Spring Boot有以下特点:

1. 创建独立的Spring应用程序

2. 嵌入的Tomcat,无需部署WAR文件

3. 简化Maven配置

4. 自动配置Spring

5. 提供生产就绪型功能,如指标,健康检查和外部配置

6. 绝对没有代码生成和对XML没有要求配置

下面是Spring Boot的Maven配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.mytest</groupId>
 7     <artifactId>springboot</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>springboot</name>
12     <description>Demo project for Spring Boot</description>
13 
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>1.5.9.RELEASE</version>
18         <relativePath /> <!-- lookup parent from repository -->
19     </parent>
20 
21     <properties>
22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24         <java.version>1.8</java.version>
25     </properties>
26 
27     <dependencies>
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-starter-web</artifactId>
31         </dependency>
32 
33         <dependency>
34             <groupId>org.springframework.boot</groupId>
35             <artifactId>spring-boot-starter-test</artifactId>
36             <scope>test</scope>
37         </dependency>
38     </dependencies>
39 
40     <build>
41         <plugins>
42             <plugin>
43                 <groupId>org.springframework.boot</groupId>
44                 <artifactId>spring-boot-maven-plugin</artifactId>
45             </plugin>
46         </plugins>
47     </build>
48 </project>

Java代码启动:

 1 package com.mytest.springboot;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RestController;
 7 
 8 /**
 9  * (1)@RestController<br>
10  * 相当于@ResponseBody+@Controller。<br>
11  * 如果只是使用@RestController,则Controller中的方法无法返回return对应的jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是return里的内容。<br>
12  * 如果需要返回到指定页面,则需要用@Controller配合视图解析器InternalResourceViewResolver才行。<br>
13  * 如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。<br>
14  * (2)@EnableAutoConfiguration<br>
15  * 这个注释告诉SpringBoot“猜”你将如何想配置Spring,基于你已经添加jar依赖项。如果spring-boot-starter-web已经添加Tomcat和Spring MVC,这个注释自动将假设您正在开发一个web应用程序并添加相应的spring设置。<br>
16  */
17 @RestController
18 @EnableAutoConfiguration
19 public class SpringbootApplication {
20 
21     @RequestMapping("/")
22     String home() {
23         return "Hello World!";
24     }
25 
26     public static void main(String[] args) {
27         SpringApplication.run(SpringbootApplication.class, args);
28     }
29 }

启动log:

 1   .   ____          _            __ _ _
 2  /\\ / ____ __ _ _(_)_ __  __ _ \ \ \ \
 3 ( ( )\___ | _ | _| | _ \/ _` | \ \ \ \
 4  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
 5     |____| .__|_| |_|_| |_\__, | / / / /
 6  =========|_|==============|___/=/_/_/_/
 7  :: Spring Boot ::        (v1.5.9.RELEASE)
 8 
 9 2018-01-26 05:09:34.615  INFO 9788 --- [           main] c.m.springboot.SpringbootApplication     : Starting SpringbootApplication on GCOTVMSW725771 with PID 9788 (C:\workspace_utils\springboot\target\classes started by yl58731 in C:\workspace_utils\springboot)
10 2018-01-26 05:09:34.619  INFO 9788 --- [           main] c.m.springboot.SpringbootApplication     : No active profile set, falling back to default profiles: default
11 2018-01-26 05:09:34.680  INFO 9788 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@133e16fd: startup date [Fri Jan 26 05:09:34 EST 2018]; root of context hierarchy
12 2018-01-26 05:09:36.898  INFO 9788 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
13 2018-01-26 05:09:36.915  INFO 9788 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
14 2018-01-26 05:09:36.917  INFO 9788 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23
15 2018-01-26 05:09:37.108  INFO 9788 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
16 2018-01-26 05:09:37.108  INFO 9788 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2430 ms
17 2018-01-26 05:09:37.352  INFO 9788 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: dispatcherServlet to [/]
18 2018-01-26 05:09:37.355  INFO 9788 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: characterEncodingFilter to: [/*]
19 2018-01-26 05:09:37.356  INFO 9788 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: ‘hiddenHttpMethodFilter‘ to: [/*]
20 2018-01-26 05:09:37.356  INFO 9788 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: ‘httpPutFormContentFilter‘ to: [/*]
21 2018-01-26 05:09:37.356  INFO 9788 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: ‘requestContextFilter‘ to: [/*]
22 2018-01-26 05:09:37.770  INFO 9788 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@133e16fd: startup date [Fri Jan 26 05:09:34 EST 2018]; root of context hierarchy
23 2018-01-26 05:09:37.854  INFO 9788 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String com.mytest.springboot.SpringbootApplication.home()
24 2018-01-26 05:09:37.857  INFO 9788 --- [           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)
25 2018-01-26 05:09:37.858  INFO 9788 --- [           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)
26 2018-01-26 05:09:37.896  INFO 9788 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
27 2018-01-26 05:09:37.897  INFO 9788 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
28 2018-01-26 05:09:37.947  INFO 9788 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
29 2018-01-26 05:09:38.133  INFO 9788 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
30 2018-01-26 05:09:38.354  INFO 9788 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
31 2018-01-26 05:09:38.361  INFO 9788 --- [           main] c.m.springboot.SpringbootApplication     : Started SpringbootApplication in 4.186 seconds (JVM running for 4.71)
32 2018-01-26 05:09:39.865  INFO 9788 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet dispatcherServlet
33 2018-01-26 05:09:39.866  INFO 9788 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : FrameworkServlet dispatcherServlet: initialization started
34 2018-01-26 05:09:39.893  INFO 9788 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : FrameworkServlet dispatcherServlet: initialization completed in 26 ms

打开localhost:8080/即可看到"Hello world!"页面。

Spring Boot(1):Guide

标签:ann   ref   color   conf   生产   content   rgs   []   world   

原文地址:https://www.cnblogs.com/storml/p/8360936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!