1. 外部资源properties的几种导入方式
1.1 使用注解直接在实体类上赋值
@Component
public class student {
@Value("sss")
private String name;
@Value("13")
private int age;
private boolean happy;
private Map<String,Object> maps;
private List<Object> list;
public student() {
}
public student(String name, int age, boolean happy, Map<String, Object> maps, List<Object> list) {
this.name = name;
this.age = age;
this.happy = happy;
this.maps = maps;
this.list = list;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isHappy() {
return happy;
}
public void setHappy(boolean happy) {
this.happy = happy;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getList() {
return list;
}
public void setList(List<Object> list) {
this.list = list;
}
@Override
public String toString() {
return "student{" +
"name='" + name + '\'' +
", age=" + age +
", happy=" + happy +
", maps=" + maps +
", list=" + list +
'}';
}
}
1.2 通过yaml赋值,在实体类中引用
@ConfigurationProperties(prefix="student") springboot通过这个寻找到配置文件的yaml
@Component
@ConfigurationProperties(prefix="student")
public class Student {
private String name;
private int age;
private boolean happy;
private Map<String,Object> maps;
private List<Object> list;
student:
name: sss
age: 3
happy: false
maps: {k1: v1,k2: v2}
list:
- 1
- 2
- 3
1.3 通过properties赋值
@PropertySource(value = "classpath:application.properties")
@Component
//@ConfigurationProperties(prefix="student")
@PropertySource(value = "classpath:application.properties")
public class Student {
@Value("${name}")
private String name;
@Value("${age}")
private int age;
private boolean happy;
private Map<String,Object> maps;
private List<Object> list;
name=axj
age=1100000
2. JSR303校验
3. 多环境下的资源选择
通过spring.profiles.active=xxx
application.xxx.properties
3.1 通过yaml
使用 - - -去分开 得到另一个块
4. SpringBoot Web开发
4.1 静态资源导入
- webjars localhost:8080/webjars
- public,static,/**,resources localhost:8080/
优先级: resources>static>public
5. thymeleaf
依赖:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
命名空间:xmlns:th=“www.thymeleaf.org”
Spring官方文档:找到我们对应的版本
6 MVC自动装配
官网阅读
在进行项目编写前,我们还需要知道一个东西,就是SpringBoot对我们的SpringMVC还做了哪些配置,包括如何扩展,如何定制。
只有把这些都搞清楚了,我们在之后使用才会更加得心应手。途径一:源码分析,途径二:官方文档!
地址 :docs.spring.io/spring-boot…
Spring MVC Auto-configuration
// Spring Boot为Spring MVC提供了自动配置,它可以很好地与大多数应用程序一起工作。
Spring Boot provides auto-configuration for Spring MVC that works well with most applications.
// 自动配置在Spring默认设置的基础上添加了以下功能:
The auto-configuration adds the following features on top of Spring’s defaults:
// 包含视图解析器
Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
// 支持静态资源文件夹的路径,以及webjars
Support for serving static resources, including support for WebJars
// 自动注册了Converter:
// 转换器,这就是我们网页提交数据到后台自动封装成为对象的东西,比如把"1"字符串自动转换为int类型
// Formatter:【格式化器,比如页面给我们了一个2019-8-10,它会给我们自动格式化为Date对象】
Automatic registration of Converter, GenericConverter, and Formatter beans.
// HttpMessageConverters
// SpringMVC用来转换Http请求和响应的的,比如我们要把一个User对象转换为JSON字符串,可以去看官网文档解释;
Support for HttpMessageConverters (covered later in this document).
// 定义错误代码生成规则的
Automatic registration of MessageCodesResolver (covered later in this document).
// 首页定制
Static index.html support.
// 图标定制
Custom Favicon support (covered later in this document).
// 初始化数据绑定器:帮我们把请求数据绑定到JavaBean中!
Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).
/*
如果您希望保留Spring Boot MVC功能,并且希望添加其他MVC配置(拦截器、格式化程序、视图控制器和其他功能),则可以添加自己
的@configuration类,类型为webmvcconfiguer,但不添加@EnableWebMvc。如果希望提供
RequestMappingHandlerMapping、RequestMappingHandlerAdapter或ExceptionHandlerExceptionResolver的自定义
实例,则可以声明WebMVCregistrationAdapter实例来提供此类组件。
*/
If you want to keep Spring Boot MVC features and you want to add additional MVC configuration
(interceptors, formatters, view controllers, and other features), you can add your own
@Configuration class of type WebMvcConfigurer but without @EnableWebMvc. If you wish to provide
custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or
ExceptionHandlerExceptionResolver, you can declare a WebMvcRegistrationsAdapter instance to provide such components.
// 如果您想完全控制Spring MVC,可以添加自己的@Configuration,并用@EnableWebMvc进行注释。
If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.