Spring boot入门一 环境搭建HelloWorld


说在前面:  

最近Spring boot也火了一把,不稍微熟习下Spring boot都不好意思出门了,于是我也试着看下Spring boot的相关配置。

概述:

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


配置工具:

1、JDK1.7

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

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


代码配置:

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


2、pom.xml中配置

	
		org.springframework.boot
		spring-boot-starter-parent
		1.4.0.RELEASE
	

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



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方法

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.0.RELEASE)

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)
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
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
2017-08-30 16:05:11.371  INFO 5180 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-08-30 16:05:11.378  INFO 5180 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-08-30 16:05:11.379  INFO 5180 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.4
2017-08-30 16:05:11.441  INFO 5180 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/aaa]    : Initializing Spring embedded WebApplicationContext
2017-08-30 16:05:11.441  INFO 5180 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 902 ms
2017-08-30 16:05:11.554  INFO 5180 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-08-30 16:05:11.557  INFO 5180 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-08-30 16:05:11.557  INFO 5180 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-08-30 16:05:11.558  INFO 5180 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-08-30 16:05:11.558  INFO 5180 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
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.
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
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()
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)
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> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
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]
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]
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]
2017-08-30 16:05:12.486  INFO 5180 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-08-30 16:05:12.530  INFO 5180 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
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。


至此,一个入门简单的spring boot创建完成。



你可能感兴趣的:(#,SpringBoot)