Spring Boot (一) 最简单跑起Hello World!

来源:互联网 发布:js切换隐藏 编辑:程序博客网 时间:2024/05/22 09:38

Spring Boot (一)

注:本例子是spring Boot 框架的Web项目开发

运行环境

  • windows 10
  • eclipse
  • 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@Controllerpublic 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

原创粉丝点击