spring-boot之快速入门

代码见

https://gitee.com/gaoxinfu/demo-spring-boot/tree/master/%20demo-spring-boot-example-01

1.设置spring-boot的parent

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
</parent>

说明:

1.Spring boot的项目必须要将parent设置为spring boot的parent,
 该parent包含了大量默认的配置,大大简化了我们的开发。
2.各位同学可以点击pom文件中上面的链接,看一下都默认配置了哪些东西; 

在这里插入图片描述

2.导入spring boot的web支持

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
	   <!-- <version></version> 由于我们在上面指定了 parent(spring boot) -->
	</dependency>

会发现 spring-boot-starter-web: MVC,AOP的依赖包都在这里有了
主要web开发

3.添加Spring boot的插件

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

3.1.Spring Boot Maven plugin 作用

1.Spring Boot的Maven插件(Spring Boot Maven plugin)能够以Maven的方式为应用
  提供Spring Boot的支持,即为Spring Boot应用提供了执行Maven操作的可能。
2.Spring Boot Maven plugin能够将Spring Boot应用打包为可执行的jar或war文件, 
  然后以通常的方式运行Spring Boot应用。
3.Spring Boot Maven plugin的最新版本为2017.6.8发布的1.5.4.RELEASE,要求Java 8, Maven 3.2及以后。

3.2.Spring Boot Maven plugin的5个Goals

1.spring-boot:repackage,默认goal。在mvn package之后,再次打包可执行的jar/war, 
  同时保留mvn package生成的2.jar/war为.origin
3.spring-boot:run,运行Spring Boot应用
4.spring-boot:start,在mvn integration-test阶段,进行Spring Boot应用生命周期的管理
5.spring-boot:stop,在mvn integration-test阶段,进行Spring Boot应用生命周期的管理
6.spring-boot:build-info,生成Actuator使用的构建信息文件build-info.properties

4.编写第一个spring-boot程序


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

/**
 * 
 * @Description:
 * 			@RestController 的意思就是controller里面的方法都以json格式输出,不用再写什么jackjson配置的了!
 * @Author:gaoxinfu
 * @Time:2018年4月18日 上午10:56:04
 */
@RestController
public class HelloWorldController {

	/**
	 * 
	 * @Title: index   
	 * @Description:
	 * 	 启动主程序,打开浏览器访问
	 * 		http://localhost:8080/hello
	 * ,就可以看到效果了,有木有很简单!
	 * @return      
	 * @return: String      
	 * @throws   
	 * @Exception:
	 * @Author: gaoxinfu
	 * @Time:2018年4月18日 上午11:15:43
	 */
	@RequestMapping("/hello")
    public String index() {
        return "Hello World";
    }
}

备注

1@SpringBootApplication:Spring Boot项目的核心注解,主要目的是开启自动配置。;
2@Configuration:这是一个配置Spring的配置类;
3@Controller:标明这是一个SpringMVC的Controller控制器;
4、main方法:在main方法中启动一个应用,即:这个应用的入口;

5.启动方式

直接运行main方法即可


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

2019-01-06 16:18:21.163  INFO 5144 --- [           main] c.g.demo.springboot.HelloApplication     : Starting HelloApplication on gaoxinfu with PID 5144 (E:\workspace\eclipse\luna\demo-spring-boot-example-01\target\classes started by www.0001.GA in E:\workspace\eclipse\luna\demo-spring-boot-example-01)
2019-01-06 16:18:21.169  INFO 5144 --- [           main] c.g.demo.springboot.HelloApplication     : No active profile set, falling back to default profiles: default
2019-01-06 16:18:21.277  INFO 5144 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@223877f8: startup date [Sun Jan 06 16:18:21 CST 2019]; root of context hierarchy
2019-01-06 16:18:23.577  INFO 5144 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2019-01-06 16:18:23.591  INFO 5144 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2019-01-06 16:18:23.592  INFO 5144 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.11
2019-01-06 16:18:23.710  INFO 5144 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-01-06 16:18:23.710  INFO 5144 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2437 ms
2019-01-06 16:18:23.936  INFO 5144 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2019-01-06 16:18:23.942  INFO 5144 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-01-06 16:18:23.942  INFO 5144 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-01-06 16:18:23.943  INFO 5144 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-01-06 16:18:23.943  INFO 5144 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2019-01-06 16:18:24.355  INFO 5144 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@223877f8: startup date [Sun Jan 06 16:18:21 CST 2019]; root of context hierarchy
2019-01-06 16:18:24.445  INFO 5144 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.gaoxinfu.demo.springboot.HelloApplication.hello()
2019-01-06 16:18:24.453  INFO 5144 --- [           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)
2019-01-06 16:18:24.454  INFO 5144 --- [           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)
2019-01-06 16:18:24.504  INFO 5144 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-01-06 16:18:24.505  INFO 5144 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-01-06 16:18:24.574  INFO 5144 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-01-06 16:18:24.901  INFO 5144 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2019-01-06 16:18:24.983  INFO 5144 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2019-01-06 16:18:25.003  INFO 5144 --- [           main] c.g.demo.springboot.HelloApplication     : Started HelloApplication in 4.303 seconds (JVM running for 4.661)

5.验证

http://localhost:8080/hello
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东山富哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值