springboot-基础

后端 / 2021-09-17

springboot项目结构

image.png

  • src 源码目录
  • BootDemoApplication 启动类
  • resource 资源目录
    • static 静态资源目录
    • javascript js目录
    • templates 模板目录
    • yaml 配置文件目录
  • test 测试目录

maven依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
  • spring-boot-starter 用于支持自动配置 日志 和 yaml
  • spring-boot-starter-test 单元测试模块

了解springboot入口类

  • @SpringBootApplication springboot启动类
  • main 入口方法

定制启动动画

如果要修改springboot启动动画,可以在resource目录下新建banner.txt

例如

 ,__                       ,__
 /  `   ___  , __     ___. /  `   ___  , __     ___.
 |__   /   ` |'  `. .'   ` |__   /   ` |'  `. .'   `
 |    |    | |    | |    | |    |    | |    | |    |
 |    `.__/| /    |  `---| |    `.__/| /    |  `---|
 /                   \___/ /                   \___/

image.png

当然你也可以用别的,支持的文件格式有

  • banner.txt
  • banner.gif
  • banner.jpg
  • banner.png

如果你不喜欢你可以在启动类中关闭Banner

package com.lu.springboot.bootdemo;

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

@SpringBootApplication
public class BootDemoApplication {

    public static void main(String[] args) {

        SpringApplication app = new SpringApplication(BootDemoApplication.class);
	// 这里关闭
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
    }

}

springboot常见类注解

@RestController

用于返回JOSN/XML相当于 @ResponseBody 和 @Controller 在一起的作用

package com.lu.springboot.bootdemo.controller;

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

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "Hello,Springboot!";
    }

}

@Controller

用于标注控制器,在MVC开发模式中代表C控制器

package com.lu.springboot.bootdemo.controller;

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

@Controller
public class HiController {
    @RequestMapping("/hi")
    @ResponseBody
    public String hi(){
        return "Hi,SpringBoot!";
    }
}

@Service

用于标注一个service

package com.lu.springboot.bootdemo.service;

public interface UserService {
    String findUserById();
}
package com.lu.springboot.bootdemo.service;

import org.springframework.stereotype.Service;

@Service
public class UerServiceImpl implements UserService{
    @Override
    public String findUserById() {
        return "Hello,World!";
    }
}

@Respository

用于标注一个数据访问层

package com.lu.springboot.bootdemo.dao;

import org.springframework.stereotype.Repository;

@Repository
public class UserRepository {
    public void test(){
        System.out.println(Thread.currentThread().getName()+" test");
    }
}

@Component

用于标注一个组件 交给spring IOC容器管理

@Resource

作用方式和 @Autowired 差不多实现自动装载

package com.lu.springboot.bootdemo.service;

import com.lu.springboot.bootdemo.dao.UserRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UerServiceImpl implements UserService{
    // 自动装配 @Resource 和 @Autowired 都是实现自动装配
    @Resource
    private UserRepository userRepository;
    @Override
    public String findUserById() {
        return "Hello,World!";
    }
}

@Autowired

spring会扫描所有被 @Autowired标注的类然后根据IOC容器进行依赖注入。

package com.lu.springboot.bootdemo.controller;

import com.lu.springboot.bootdemo.service.UerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/service")
public class ServiceTestController {
    @Autowired
    UerServiceImpl userService;

    @GetMapping("")
    public String test() {
        return userService.findUserById();
    }
}

@RequestMapping

用来处理地址映射 起到路由的作用,如果作用在类上表示为父路由

  • Params 指定参数
  • Headers 指定请求中header
  • Value 指定实际请求地址
  • Method 指定请求类型 GET POST PUT DELETE 等
  • Consumes 处理请求提交内容类型 Content—Type
  • Produces 指定返回内容类型

@Transactional

用于事务

@Qualifer

通常和@Autowired 同时使用

springboot常见方法注解

@RequestBody

使用位置: 方法参数前

说明:用来处理 application/jsonapplication/xmlContent-Type类型的数据 POST请求参数获取

package com.lu.springboot.bootdemo.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/rest")
public class RestfulController {
    @GetMapping("/get/{id}")
    public String get(@PathVariable("id") Integer id) {
        System.out.println("request->get:" + id);
        return "ok";
    }
    @PostMapping("/post")
    public String post(@RequestBody String name){

        return "ok";
    }
}

image.png

@PathVariable

使用位置: 方法参数前

说明: 用于获取路径参数

package com.lu.springboot.bootdemo.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/rest")
public class RestfulController {
    @GetMapping("/get/{id}")
    public String get(@PathVariable("id") Integer id) {
        System.out.println("request->get:" + id);
        return "ok";
    }
}

@Bean

使用位置: 方法上

说明: 声明该方法返回结果是一个由spring容器管理的bean

@ResponseBody

使用位置: 方法上

说明: 通过适当的HttpMessageConverter将控制器中的方法返回对象转换为指定格式(JSON/xml)后写入 Response对象的body数据区

不经过视图渲染直接写出数据

@GetMapping("/resp")
@ResponseBody
public String hello(){
	return "Hello";
}

springboot其他常见注解

@EnableAutoConfiguration

使用位置: 入口类/类名上

用来开启自动配置

@SpringBootApplication

使用位置: 入口类/类名上

标注springboot启动类

@EnableAsync

使用位置: 入口类/类名上

开启异步注解功能

@ComponentScan

使用位置: 入口类/类名上

用来扫描组件,为自动装配实现数据支持,自动扫描后进入IOC容器

默认从该注解所在类的包开始扫描

@Aspec

使用位置: 入口类/类名上

标注一个AOP切面

@ControllerAdvice

使用位置: 类名上

包含@Component,可以被扫描到。统一处理异常

@ExceptionHandler

使用位置: 方法上

用在方法上,表示遇到这个异常就执行该方法

@Value

使用位置: 属性上

读取配置文件中的值