Spring boot 快速入门 - 环境搭建

一、Spring Boot 特点

  • Spring boot 可以创建一个独立的应用程序;
  • 嵌入了tomcat,Jetty容器,同时支持不使用第三方容器,直接启动应用程序;
  •  为maven 工程提供简单依赖配置项 (spring-boot-starter-web);
  • 尽可能的支持自动配置;
  • 提供更完善的功能特性:如: metrics, health checks 和 externalized configuration。
  • 绝对没有代码生成和XML配置的要求,可以完全脱离XML配置

二、环境搭建

  • 创建spring-boot maven 工程。
  • 在pom.xml文件中添加依赖:

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


  • 创建hello/SampleController.java

package com.demo.spring.boot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/spring-boot")
public class HelloController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    @ResponseBody
    public String hello() {
        System.out.println("Hello, this is spring boot");
        return "Spring Boot";
    }
}


@Controller, 声明该类为controller 类。

@RequestMapping, 声明请求RUL路径,当前例子的URL路径为 “http://ip:8080/spring-boot/hello”, method = RequestMethod.GET, 声明请求方法为GET方式。

@ResponseBody, 声明方法返回对象为请求的返回对象。如果不写该声明,那么方法返回值默认为跳转到return的URL路径下。


  • 创建启动类:

package com.demo.spring.boot.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration    
@ComponentScan("com.demo.spring.boot")
@EnableAutoConfiguration    
public class Startup {  
    public static void main(String[] args) {  
        SpringApplication app = new SpringApplication(Startup.class);  
        app.run(args);  
    }    
}

@Configuration, 声明启动配置项。

@ComponentScan, 声明加载bean 包,默认当前类所在的包,可以指定包路径。

@EnableAutoConfiguration, 声明自动配置。

SpringApplication app = new SpringApplication(Startup.class);
创建容器,同时可以在该容器下添加各种启动参数。

app.run(args); 
启动容器。

通过run application方式运行主函数,即可启动容器。

启动容器后, 打开浏览器,访问:http://localhost:8080/spring-boot/hello  就可以在页面中看到输出“Spring Boot”, 同时可以在控制台看到输出信息:Hello, this is spring boot 。

这样,一个简单的spring-boot环境就搭建好了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值