springboot学习日记(一)

什么是springboot

SpringBoot是Spring社区发布的一个开源项目,在帮助开发者快速并且更简单的构建项目。它使用习惯优于配置的理念让你的项目快速运行起来,使用Spring Boot很容易创建一个独立运行(运行jar,内置Servlet容器,Tomcat、jetty)、准生产级别的基于Spring框架的项目,使用SpringBoot框架,你可以不用或者只需要很少的配置文件。

springboot核心功能

独立运行的Spring项目:可以以jar包形式独立运行,通过java -jar xx.jar即可运行。

内嵌Servlet容器:可以选择内嵌Tomcat、Jetty等。

提供starter简化maven配置:一个maven项目,使用了spring-boot-starter-web时,会自动加载Spring Boot的依赖包。

自动配置Spring:Spring Boot会根据在类路径中的jar包、类,为jar包中的类自动配置Bean。

准生产的应用监控:提供基于http、ssh、telnet对运行时的项目进行监控。

项目目录结构

/src/main/java/ //为存放项目所有源代码目录
/src/main/resources //为存放项目所有资源文件以及配置文件的目录
/src/test //为存放测试代码目录

源码解读

package com.example.springbootdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootdemoApplication.class, args);
    }
}

@SpringBootApplication开启了Spring组件扫描和springboot的自动配置功能,相当于把一下三个注解组合在一起

  • @Configuration:表名该类使用基于Java的配置,将此类作为配置类
  • @ComponentScan:启用注解扫描
  • @EnableAutoConfiguration:开启springboot的自动配置功能

控制器新建测试

新建一个package名为com.example.springbootdemo.controller
在包内创建名为HelloController.java的文件用于测试控制器
输入以下代码:

package com.example.springbootdemo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping
    public String hello(){
        return "做个小测试";
    }
}

启动项目,在浏览器中输入http://localhost:8080/hello来测试是否搭建成功

很幸运,咱成功了


假如在HelloController类中的@RequestMapping加入参数"hello"
输入http://localhost:8080/hello效果如下

输入http://localhost:8080/hello/hello才能访问正确

posted @ 2021-01-19 10:07  YukioLee  阅读(176)  评论(0编辑  收藏  举报