springboot 笔记
发布日期:2021-05-15 00:37:03 浏览次数:19 分类:精选文章

本文共 4056 字,大约阅读时间需要 13 分钟。

Spring Boot������������

���������������������controller

������@SpringBootApplication������������������������������������������������

@SpringBootApplication(scanBasePackages = "zyc")

������������������������@SpringBootApplication���������@SpringBootConfiguration���@EnableAutoConfiguration���@ComponentScan���������������

������@ConfigurationProperties ������������

������

���������������User���������������������������������

������������

  • ���application.properties���������������������
  • ���User������������@ConfigurationProperties���������������������������"myuser"
  • @Component
    @ConfigurationProperties(prefix = "myuser")
    public class UserConfig {
    // ���������������������������
    }

    ������@SpringBootApplication ���������

    @SpringBootApplication������������������������������������������

    • @SpringBootConfiguration
    • @EnableAutoConfiguration
    • @ComponentScan

    ������������������������

    ������devTools������������������������������������������������������������������������������������������

    org.springframework.boot
    spring-boot-devtools
    true

    ������YAML ������������

    YAML ���������������

  • ��������������������� :
  • ��������� :, - ���������������������������
  • ���������������������
    key: value
  • ������������������������������
    key: {
    key1: value1
    key2: value2
    }
  • ���������������������
    key: [value1, value2, value3]
  • YAML ���������������������

  • ������������
  • API ������
  • ������������
  • ������������������������

    ������������������������������

    spring:
    mvc:
    static-path-pattern: "/static/**"

    ������������������������

    ������������������������������������������������������������

  • ������HandlerInterceptor������
  • ���������������
  • ������������������
  • ������������������������

    public class myInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle() {
    return true;
    }
    @Override
    public boolean postHandle() {
    return true;
    }
    @Override
    public boolean afterCompletion() {
    return true;
    }
    }

    ������������������

    import org.springframework.boot.web.servlet.interceptor.WebMvcConfigurer;
    import myInterceptor;
    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    @Override
    void addInterceptors LennonIntercepror interceptor) {
    super.addInterceptors(interceptor);
    }
    }

    ���������������������������������������������������������������������������������������������������������

    spring:
    mvc:
    static-path-pattern: "/resources/**"

    ������������������������

  • ���������������������������
  • ������������������������@RequestParam������
  • ������@Uploaded������������������������
  • ���������������������������������
  • ���������������

    spring:
    servlet:
    multipart:
    max-file-size: 10MB
    max-request-size: 100MB

    ���������������������������

    @PostMapping("/upload")
    public JsonResult upload(@RequestParam("image") MultipartFile multipartFile) {
    if (!multipartFile.isEmpty()) {
    String originalFilename = multipartFile.getOriginalFilename();
    String substring = originalFilename.substring(originalFilename.lastIndexOf(".")); // ���������������������
    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
    String newName = uuid + substring;
    try {
    multipartFile.transferTo(new File(path + newName));
    return new JsonResult(true, "������������������");
    } catch (IOException e) {
    return new JsonResult(false, "������������������");
    }
    }
    return new JsonResult(false, "������������������");
    }

    ������������������

    404 ������������

  • ������404.html������
  • ���������������src/main/resources/templates/���������
  • 5XX ������������

  • ������5xx.html������
  • ���5xx.html������src/main/resources/templates/���
  • ������������������

    ���application.yaml������������������������

    spring:
    profiles:
    active: dev # ������������������
    env:
    - Denis=dev
    - Denis=prod

    ������������������������DENIS������������������������������������������������application.properties������������������������������application-${spring.profile}.properties������

    上一篇:thymeleaf 笔记
    下一篇:ideal 下创建springboot项目

    发表评论

    最新留言

    做的很好,不错不错
    [***.243.131.199]2025年04月27日 04时02分57秒