Spring Boot

1、配置文件

1.SpringBoot使用一个全局的配置文件 , 配置文件名称是固定的

  • application.properties

    • 语法结构 :key=value
  • application.yml(application.yaml)

    • 语法结构 :key:空格 value

2.properties配置文件在写中文有乱码的时候, 我们需要去IDEA中设置编码格式为UTF-8;

具体操作:settings-->Editor-->FileEncodings 中配置;3个选项都选选择UTF-8,勾选Transparent native-to-ascii conversion-->OK

3.配置文件优先级

项目目录下config下的application.yml > 项目目录下的application.yml > resources目录下config下的application.yml >resources目录下的application.yml

4.properties 多环境切换

例如:

application-test.properties 代表测试环境配置

application-dev.properties 代表开发环境配置

但是Springboot并不会直接启动这些配置文件,它默认使用application.properties主配置文件

xml 复制代码
#比如在配置文件中指定使用dev环境,我们可以通过设置不同的端口号进行测试;#我们启动SpringBoot,就可以看到已经切换到dev下的配置了;spring.profiles.active=dev

5.yml的多文档块

复制代码
debug:true  #日志打印输出,来查看哪些自动配置类生效、哪些没生效
server:
  port: 8081
#选择要激活的那个环境块,默认是8081,当前激活的是8084
spring:
  profiles:
    active: test

---
server:
  port: 8083
spring:
  profiles: dev #配置环境的名称


---

server:
  port: 8084
spring:
  profiles: test  #配置环境的名称

6、自动配置理解

  1. yml中的所有配置,都会有一个对应的xxxProperties,例如server对应的是ServerProperties
  2. 在spring-boot-autoconfigure的META-INF的spring.factories会存在与xxxProperties对应的Auto Configure

2、springboot中JUnit的应用

  1. pom.xml添加依赖

    xml 复制代码
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
     
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13</version>
            </dependency>
  2. 在需要测试的类或接口上,右键-go to-Test,选择junit4,生成对应的测试文件

  3. 测试类的示例,参考链接

    java 复制代码
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = SpringbootDemoApplication.class)//Application是springboot启动类名称
    public class userServiceImplTest {
        @Autowired
        userServiceImpl userService1;
        @Test
        public void test(){
            userService1.login();
        }
    }

3、 springboot的静态资源

1.静态资源的存放路径为classpath,也就是resources目录下的这几个目录,访问优先级既是下面的顺序:

  1. /META-INF/resources
  2. /resources
  3. /static
  4. /public

2.配置springboot项目首页

静态资源文件夹下的所有 index.html 被称为静态首页或者欢迎页,它们会被 /** 映射,换句话说就是,当我们访问"localhost:8080"时,都会跳转到静态首页(欢迎页)。同时当index.html 存在多个时,也遵循静态资源优先级原则。

3.springboot 配置

xml 复制代码
# 默认值为    /**
spring.mvc.static-path-pattern=
# 默认值为   classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ 
spring.resources.static-locations=这里设置要指向的路径,存在多个时使用英文逗号隔开

# spring.mvc.static-path-pattern指定了访问项目静态资源的url地址,默认是/**。
# spring.resources.static-locations指定了静态资源的存放位置。

4、thymeleaf模板引擎(前后端分离后也不常用了)

1.作用

Thymeleaf 可以完全替代 JSP, 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

2.使用方法

1、导入依赖

xml 复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2、在IDEA中,Ctrl+N搜索ThymeleafProperties ,可以得出结论:*thymeleaf在Spring Boot中默认的模板配置路径为:src/main/resources/templates*,所以当前需在resources下新建templates文件夹,并在文件夹中编写测试MyTest.html

3、编写测试类,并访问http://localhost:8080/hello2测试,看是否跳转到MyTest.html

java 复制代码
@Controller
public class MyThymeleafTest {
    //http://localhost:8080/hello2
    @RequestMapping("/hello2")
    public String hello2() {
        return "MyTest";
    }
    
    
    @RequestMapping("/user/list")
    public String userList(Model model) throws Exception {
        model.addAttribute("title", "用户列表");
        model.addAttribute("hello", "<h1>Hello, Spring Boot!</h1>");
        List<User> userList = new ArrayList<>();
        userList.add(new User("小明", 25, true));
        userList.add(new User("小红", 23, false));
        model.addAttribute("userList", userList);
 
        return "/user/list";
    }
}

4、html中的约束

html 复制代码
<!DOCTYPE html>
<!--thymeleaf头文件-->
<html xmlns:th="http://www.thymeleaf.org">
 
<head>
    <meta charset="UTF-8" />
    <title th:text="${title}">标题</title>
    <title th:utext="${title}">标题</title><!--转义-->
</head>
<body>
<h1 th:text="${hello}">hello</h1>
<table>
    <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>学生</th>
    </tr>
    <tr th:each="user : ${userList}"><!--解析打印list的值-->
        <td th:text="${user.name}"></td>
        <td th:text="${user.age}"></td>
        <td th:text="${user.student}?'是':'否'"></td>
    </tr>
</table>
 <!--修改前端模板时,属性前面需加 th: ,url需用 @{} 包括住-->
<select>
    <option th:each="user : ${userList}" th:value="${user.name}" th:text="${user.name}">我是默认值</option>
</select>
 
</body>
</html>

5、SpringMVC配置

java 复制代码
package com.example.config;//新增配置文件

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
//@EnableWebMvc         //导入一个类,从容器中获取,自定义视图解析器时,不能配置该注解
public class MyMvcConfig implements WebMvcConfigurer {
    //视图跳转,访问test2时,跳转到test
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/test2").setViewName("test");
    }
    //视图解析器、拦截器等,也能在此进行配置
}

6、其他

  1. SpringBoot大面积出现【程序包org.springframework.web.servlet.config.annotation不存在】时,参考链接
    File ---> Settings ---> Runner ---> 勾选上 Delegate IDE build/run actions to Maven
  2. SpringBoot中内置tomcat版本问题(ERROR:An incompatible version [1.2.33] of the Apache Tomcat Native library )
  3. 修改spring-boot版本后,出现Project 'org.springframework.boot:spring-boot-starter-parent:2.6.13' not found
    1. 清理IDEA缓存:File、Invalidate Caches、Invalidate and Restart
  4. SpringBoot使用thymeleaf教程
相关推荐
riNt PTIP3 分钟前
SpringBoot创建动态定时任务的几种方式
java·spring boot·spring
星晨羽2 小时前
西门子机床opc ua协议实现变量读写及NC文件上传下载
java·spring boot
yuweiade2 小时前
Spring Boot 整合 Redis 步骤详解
spring boot·redis·bootstrap
三水不滴3 小时前
SpringAI + SpringDoc + Knife4j 构建企业级智能问卷系统
经验分享·spring boot·笔记·后端·spring
2601_949814693 小时前
Docker部署Spring Boot + Vue项目
vue.js·spring boot·docker
RDCJM6 小时前
Springboot的jak安装与配置教程
java·spring boot·后端
志飞8 小时前
springboot配置可持久化本地缓存ehcache
java·spring boot·缓存·ehcache·ehcache持久化
weixin_704266058 小时前
Spring Cloud Gateway
spring boot
lclcooky9 小时前
Spring Boot 整合 Keycloak
java·spring boot·后端
William Dawson11 小时前
【一文吃透 Spring Boot 面向切面编程(AOP):实例\+实现\+注意事项】
java·spring boot