1. Several import methods of external resource properties
1.1 Assign values directly to entity classes using annotations
@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 + '}'; }}Copy the code
1.2 Reference in entity class through YAML assignment
@configurationProperties (prefix=”student”) SpringBoot uses this to find the yamL for the configuration file
@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;
Copy the code
student:
name: sss
age: 3
happy: false
maps: {k1: v1,k2: v2}
list:
- 1
- 2
- 3
Copy the code
1.3 Assign by 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;
Copy the code
name=axj
age=1100000
Copy the code
2. JSR303 calibration
3. Resource selection in multiple environments
By the spring. Profiles. The active = XXX
application.xxx.properties
3.1 through yaml
Use – to split to get another block
4. SpringBoot Web development
4.1 Static Resource Import
- webjars localhost:8080/webjars
- Public, static, /**, resources localhost:8080/
Priority: Resources >static>public
5. thymeleaf
Rely on:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
Copy the code
Namespace: XMLNS :th= “www.thymeleaf.org”
Spring official documentation: Find our version
Docs. Spring. IO/spring – the boot…
6 MVC auto-assembly
Website to read
Before writing the project, we also need to know what SpringBoot does to our SpringMVC configuration, including how to extend, how to customize.
Only when these are clear, we can use them more easily in the future. Way one: source code analysis, way two: official documents!
Address: docs. Spring. IO/spring – the boot…
Spring MVC auto-configuration // Spring Boot provides automatic configuration for Spring MVC, which works well with most applications. Spring Boot provides auto-configuration for Spring MVC that works well with most applications. // Auto configuration adds The following functionality to Spring's default Settings: The auto-configuration adds The following features on top of Spring's defaults: / / the Inclusion of ContentNegotiatingViewResolver containing view the parser and BeanNameViewResolver beans. / / support static resource folder path, And Webjars Support for Serving static resources, including Support for Webjars // Automatically registers Converter: This is what we do when we submit data to the background and automatically encapsulate it as an object, such as converting the string "1" to int: [Formatters, such as pages that give us a 2019-8-10, It will automatically format us as a Date object Automatic registration of Converter, GenericConverter, And Formatter beans. // HttpMessageConverters // SpringMVC is used to convert Http requests and responses. For example, we want to convert a User object to a JSON string. Support for HttpMessageConverters (covered later in this document). // Define Automatic registration of error code generation rules MessageCodesResolver (covered later in this document). // homepage customize Static index.html support (Covered later in this document). // Initialize the data binder: Bind the request data to the JavaBean for us! The Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document). / * if you want to keep the Spring Boot function of MVC, And if you want to add other MVC configurations (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMVCConfiguer, but not @EnableWebMVC. If you want to provide the RequestMappingHandlerMapping, RequestMappingHandlerAdapter or ExceptionHandlerExceptionResolver instances of custom, You can WebMVCregistrationAdapter instance declaration to provide such components. */ 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 dojo.provide to components. / / if you want to completely control the Spring MVC, You can add your own @Configuration and annotate it with @enableWebMVC. If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.Copy the code