您的位置:首页 > 编程语言 > Java开发

SpringBoot入门之HelloWorld

2016-11-29 23:08 429 查看
Alan Liu 接触SpringBoot 也是最近几个月,为了帮助有一定基础的码友们在学习SpringBoot时的入门实例。特此一系列整理下基于SpringBoot的学习知识,使我们共同学习进步;有不足之处望大家积极反馈,Thanks!

关键点在代码中注释的就不再重复叙述了哈。

1.环境搭建

本系列开发环境:

开发语言:java(jdk1.8);
发工具 IntelliJ IDE;
构建工具:Apache-Maven-3.1.0;
版本控制:Git;
代码托管平台:GitHub。


大家对以上不熟悉的请先在网上找下相关资料学习下。

2.新建工程

基于Maven构建工程venus-spring-boot。项目结构如图:



3.SpringBoot 入门之HelloWorld

3.1 pom.xml文件

SpringBoot 构建依赖包管理两种方式:

其一:继承

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
...
</dependencies>


其二:导入

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.1.9.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>


3.2 程序启动类

/**
* Spring Boot入门之Hello World
* @author Alan Liu
* @create 2016-11-25
* 1.当工程中有@Component, @Service, @Repository, @Controller等,自动注册Spring Beans 需要添加 @ComponentScan
* 2.@SpringBootApplication same as @Configuration @EnableAutoConfiguration @ComponentScan
*/

@ComponentScan
@EnableAutoConfiguration
public class AppVenus
{
public static void main( String[] args )
{
SpringApplication.run(AppVenus.class,args);
}
}


@ComponentScan:开启注解扫描

@EnableAutoConfiguration:开启自动配置

3.3 HelloWorld类

/**
* Controller
*
* @author Alan Liu
* @create 2016-11-25 13:47
**/

@Controller
public class HelloWorld {

@RequestMapping("/helloworld")
@ResponseBody
public String helloWorld(){
return "Hello World!";
}
}


4.SpringBoot项目启动

运行AppVenus.main方法,启动在控制台可以看到服务端口默认8080,默认应用服务器Tomcat.

5.运行观察HelloWorld

在浏览器中输入http://localhost:8080/helloworld



啊哈,SpringBoot 的HelloWorld程序已完成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java springboot helloworld