使用idea创建spring Boot项目

springboot项目使用配置文件判断环境的方法 springboot指定环境_spring

添加web依赖支持

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
>跑第一个SpringBoot 项目
@RestController
public class FirstDay {
    @RequestMapping("/firstDay")
    public String first(){
        return "helloSpring";
    }
}

springboot项目使用配置文件判断环境的方法 springboot指定环境_spring_02

springBoot的Maven操作

springboot项目使用配置文件判断环境的方法 springboot指定环境_后端_03


springboot项目使用配置文件判断环境的方法 springboot指定环境_spring_04

使用shell命令跑项目

springboot项目使用配置文件判断环境的方法 springboot指定环境_后端_05

更改端口号
  • 方法一: properties配置
server.port =8081
  • 方法二:yml语法
server:
  port: 8085

SpringBoot启动器

  • 比如web的启动器就相当于Spring中的配置文件+核心依赖只不过SpringBoot给你集成了不用自己手写了

yml语法

springboot项目使用配置文件判断环境的方法 springboot指定环境_spring boot_06

给属性赋值的几种方式
  • 方法一:注解方式
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dog {
    @Value("大黄")
    private String name;
    @Value("3")
    private  int age;
}
@Autowired
     private Dog dog;
    @Test
    void contextLoads() {
        System.out.println(dog);
    }

springboot项目使用配置文件判断环境的方法 springboot指定环境_后端_07

方法二:使用配置文件

@Component
@Data
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birthday;
    private Map<String,Object> maps;
    private List<Object> list;
    private Dog dog;
}

yml文件中

person:
  name: jiasen
  age: 20
  happy: true
  birthday: 2001/10/07
  maps: {q: 非q,w: 非w}
  list:
    - code
    - music
  dog:
    name: 大黄
    age: 3

springboot项目使用配置文件判断环境的方法 springboot指定环境_intellij-idea_08

  • 使用注解@ConfigurationProperties(prefix = “person”)需要的依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
自定义文件

springboot项目使用配置文件判断环境的方法 springboot指定环境_spring boot_09


springboot项目使用配置文件判断环境的方法 springboot指定环境_spring boot_10

JSR303校验
  • 在springboot中,@Validated可对后台接收model进行数据校验,不符合则抛出异常 , Validate 英文意思为:使生效,使合法化
  • 支持@Email的依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

改成正确格式

springboot项目使用配置文件判断环境的方法 springboot指定环境_java_11

自定义banner

springboot项目使用配置文件判断环境的方法 springboot指定环境_spring_12


从百度上找banner然后cv到banner.txt文件中

多环境配置以及文件位置

springboot项目使用配置文件判断环境的方法 springboot指定环境_后端_13

优先级

springboot项目使用配置文件判断环境的方法 springboot指定环境_spring_14

激活多文件配置properties形式

server.port=8083
server.port=8083
#springboot的多文件配置:可以选择激活哪一个文件
spring.profiles.active=dev

springboot项目使用配置文件判断环境的方法 springboot指定环境_后端_15

yaml语法配置

server:
  port: 8081
spring:
  profiles: 
  active: dev
---
server:
  port: 8081
spring: 
  profiles: dev
---
server:
  port: 8082
spring:
  profiles: test
---
自动装配原理
  • SpringBoot会加载大量的自动配置类
  • 我们的需求有没有在SpringBoot默认写好的自动配置中
  • 我们再来看自动配置类中到底配置了那些组件
  • 给容器中自动配置类添加组件的时候,会从properties类中获取某些属性,我们只要在配置文件中指定这些属性的值即可
  • XXXXAutoConfigurartion:自动配置类;给容器中添加组件
  • XXXXProperties:封装配置文件中相关属性
SpringWeb开发
  • 静态资源导入探究

资源探究

springboot项目使用配置文件判断环境的方法 springboot指定环境_后端_16

  • 优先级:resources>static>public
首页和图标定制、

springboot项目使用配置文件判断环境的方法 springboot指定环境_spring boot_17

Thymeleaf模板引擎
  • 添加依赖
<dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>

springboot项目使用配置文件判断环境的方法 springboot指定环境_java_18

第一个Thymeleaf程序

springboot项目使用配置文件判断环境的方法 springboot指定环境_java_19

@RequestMapping("/second")
    public String second(Model model){
        model.addAttribute("msg","hello thymeleaf");
        return "test";
    }

springboot项目使用配置文件判断环境的方法 springboot指定环境_java_20

Thymeleaf语法简介

  • 访问静态资源

th:src="@{bootstrap/js/boostrap.min.js}"

  • 访问model模型中的数据,例如访问一个user对象的name属性

th:text="${user.name}"

  • 迭代一个userlist集合
<tr th:each="user : ${userlist}">
    <td th:text="${user.name}">tyrone</td>
    <td th:text="${user.age}">18</td>
</tr>
  • 判断是否为空
<tr th:if="${messages.empty}">
    <td colspan="3">No messages</td>
</tr>
  • 在js中使用
<script th:inline="javascript">
    var user = [[${user}]]
    console.log(user.name + "\t" + user.age);
</script>
MVC 配置原理
//要想自己定制功能只要重写这个组件,然后将其交给springBoot便可
@Configuration
public class configOne implements WebMvcConfigurer {
    //自定义一个视图解析器MyViewResolver
    public static class MyViewResolver implements ViewResolver{
        @Override
            public View resolveViewName(String viewName, Locale locale) throws Exception {
                return null;
            }
        }
        //ViewResolver 实现了一个自己的视图解析器myViewResolver
        @Bean
        public ViewResolver myViewResolver(){
            return new MyViewResolver();
    }
  • 重写视图跳转
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/jiaSen").setViewName("test");
    }

springboot项目使用配置文件判断环境的方法 springboot指定环境_java_21