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

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
@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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值