yml对空格的要求严格
#对象存储 Dog: name: "lucky" age: "7" Person: name: "阿辉" age : 22 happy: true birth: 2021/2/23 maps: {k1: v1, k2: v2} lists: [code,basketball,girl] dog: name: Lucky age: 22
通过@Component和@ConfigurationProperties(prefix = "dog")将实体类的属性与yml中的配置进行绑定
ConfigurationProperties(prefix = "dog")必须导入spring-boot-configuration-processor依赖还需要使用prefix进行绑定!
package com.example.springboot.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Data @NoArgsConstructor @AllArgsConstructor @Component @ConfigurationProperties(prefix = "dog") public class Dog { private String name; private Integer age; @Override public String toString() { return "Dog{" + "name='" + name + '\'' + ", age=" + age + '}'; } } package com.example.springboot.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; @Data @NoArgsConstructor @AllArgsConstructor @Component @ConfigurationProperties(prefix = "person") @Validated //数据校验 public class Person { private String name; private Integer age; private Boolean happy; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog; @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", happy=" + happy + ", birth=" + birth + ", maps=" + maps + ", lists=" + lists + ", dog=" + dog + '}'; } }通过@Autowired的自动装配原理将实体类中的数据进行输出
package com.example.springboot; import com.example.springboot.pojo.Dog; import com.example.springboot.pojo.Person; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class SpringbootApplicationTests { @Autowired private Dog dog; @Autowired private Person person; @Test void contextLoads() { System.out.println(dog); System.out.println("--------------"); System.out.println(person); } } 四、多环境切换测试application.properties 环境切换(需要三个环境,application.properties、application-test.properties、application-dev.properties)
server.port=8080 # SpringBoot的多环境配置:自主选择激活配合文件(test/dev) spring.profiles.active=devapplication.yml方式实现多环境的切换
# 选择激活哪个环境(test/dev) 各个配置之间拿---分隔 spring: profiles: active: test --- spring: profiles: test server: port: 8082 --- spring: profiles: dev server: port: 8089 五、静态资源 1、在springboot中,可以使用以下方式处理静态资源:
webjars:localhost:8080/webjars/
public、static、/**、resources 可以使用:``localhost:8080/文件名.后缀名
2、优先级(如果文件相同则)
resources > static(默认)>public
六、扩展mvc package com.example.springboot.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.View; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.util.Locale; // //@EnableWebMvc就是添加了@Import({DelegatingWebMvcConfiguration.class})这个类,意思就是从容器中获取所有的webmvcconfig // 分析:WebMvcAutoConfig类有个@ConditionalOnMissingBean({WebMvcConfigurationSupport.class}) // 如果有这个类,那么MyConfig就不会自动装配, // 又因为上面DelegatingWebMvcConfiguration这个类继承了WebMvcConfigurationSupport // 所以不能加@EnableWebMvc这个注解,不然就会失去自动装配的功能! // 扩展SpringMVC @Configuration public class MyConfig implements WebMvcConfigurer { // ViewResolver 实现了视图解析器接口的类 就相当于是个视图解析器 // 如果想要自定义的功能,只需要把它注入到bean中,交由Springboot,Springboot会帮我们自动装配! @Bean // 自定义功能!!! public ViewResolver myViewResolver(){ return new MyViewResolver(); } public static class MyViewResolver implements ViewResolver{ @Override public View resolveViewName(String s, Locale locale) throws Exception { return null; } } // 视图跳转 @Override public void addViewControllers(ViewControllerRegistry registry) { // addViewController("/springboot")相当于用localhost:8080/springboot这个跳转到localhost:8080/test // 相当于更名!! registry.addViewController("/springboot").setViewName("test"); } } 七、thymeleaf(模板引擎)的应用 7.1、 作用域的导入 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> 7.2、thymeleaf的th的应用