Spring Boot (一) 最简单跑起Hello World!
2017-11-24 11:13
381 查看
Spring Boot (一)
注:本例子是spring Boot 框架的Web项目开发运行环境
windows 10eclipse
jdk 1.8
maven 3.5.0
1. 首先新建一个Maven工程,这里我就省略了,可参考
http://blog.csdn.net/yioow/article/details/78614600
2. pom.xml导入jar包
官方文档实例,都让我们继承spring-boot-starter-parent原文:To configure your project to inherit from the spring-boot-starter-parent set the parent
spring-boot-starter-web是web开发用的
<!-- Inherit defaults from Spring Boot (Spring boot 父引用) --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> </parent> <!-- Add typical dependencies for a web application (Spring boot 核心web) --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
注:如果再导包过程中项目报错,可以项目右键-> 点击Maven-> 点击Update Project 即可
附:还可以加入这段配置(加不加都可以,看自己需要)
Spring Boot包含一个Maven插件,可以将项目打包为可执行的jar文件,还有一个是Spring Boot 的jdk插件,可以规定jdk版本。<build> <plugins> <!-- Package as an executable jar/war --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- jdk编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <!-- 源代码使用的开发版本 --> <target>1.8</target> <!-- 需要生成的目标class文件的编译版本 --> </configuration> </plugin> </plugins> </build>
3.新建一个包,包内有一个Application.java,负责启动程序
com +- test +- springBoot +- Application.java
Application.java 的内容如下
package com.test.springBoot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @SpringBootApplication @Controller public class Application extends SpringBootServletInitializer{ @RequestMapping("/") @ResponseBody public String index() { return "Hello World!"; } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
生成可部署的war文件的第一步是提供一个 SpringBootServletInitializer子类并重写它的configure方法。这样做可以利用Spring框架的Servlet 3.0支持,并且可以让你的应用程序在被servlet容器启动的时候进行配置。通常,程序的主类以继承SpringBootServletInitializer
4.启动程序 Application.java文件右键-> 点击Run As-> 点击Java Application 即可
5.在浏览器输入http://localhost:8080/
Good Luck!
附:
代码http://download.csdn.net/download/yioow/10131127?locationNum=1&fps=1
SpringBoot 使用文档
https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-maven
相关文章推荐
- spring boot 输出简单的hello world(没用分层结构)
- spring boot(1)-简单搭建 Hello World
- 【Spring】Spring-boot搭建一个简单的Hello World工程
- spring boot 利用分层结构输出简单的Hello world
- Spring Boot:简单Hello World
- Spring Boot—— Thymeleaf (gradle) 的简单使用
- 尝试通过springboot+angular写一个简单的工作流1
- 快速构建可运行的Spring-boot项目(Hello World)
- Springboot+jedis简单项目
- SpringBoot入门:Hello World(一)
- springboot与ebean的简单整合
- spring boot的拦截器简单使用
- SpringBoot+Shiro+SpringSession的简单集成配置简要步骤
- Spring boot 学习之 Hello World
- 一、springboot项目的简单使用
- Spring Boot整合Spring Security简单实现登入登出从零搭建教程
- SpringBoot结合Redis简单应用
- spring boot 入门-使用spring boot构建简单应用
- 玩转spring boot——简单登录认证
- SpringBoot-配置简单的错误页面