If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without@EnableWebMvc.
If you want to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, and still keep the Spring Boot MVC customizations, you can declare a bean of type WebMvcRegistrations and use it to provide custom instances of those components.
声明**WebMvcRegistrations**改变默认底层组件
If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc, or alternatively add your own @Configuration-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.
Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text.
//@ConditionalOnBean(name = "tom")若存在name为tom的组件就执行装配下面的组件
@ConditionalOnMissingBean(name = "tom")//若不存在name为tom的组件就执行装配下面的组件
//这个方法放在类上面就是执行的装配是类中的全部方法
public class MyConfig {
//若注解是放在方法上面就只对该方法生效
@ConditionalOnBean(name="tom")
@Bean("user01")
public User getUser(){}
}
@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
String brand;
Integer price;
}
===========在application.properties文件里面写======
mycar.brand=byd
mycar.price=10000
//在controller中写上
@Autowired
Car car;
@RequestMapping("/car")
public Car car(){
return car;
}
//在配置类上面使用注解绑定类,在配置类上面注解是因为myconfig是已经注册的组件,在主方法类上也是可以的
@EnableConfigurationProperties(Car.class)
public class MyConfig {
//在类上面仍需要一个注解
@ConfigurationProperties(prefix = "mycar")
public class Car {
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
@Configuration
public @interface SpringBootConfiguration {
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
//
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
//利用Registrar给容器导入一系列组件
//将指定的一个包下的所有组件导入进来,Main Application所在的包下
@Bean
@ConditionalOnBean(MultipartResolver.class) //容器中有这个类型组件
@ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) //容器中没有这个名字 multipartResolver 的组件
public MultipartResolver multipartResolver(MultipartResolver resolver) {
//给@Bean标注的方法传入了对象参数,这个参数的值就会从容器中找。
//SpringMVC multipartResolver。防止有些用户配置的文件上传解析器不符合规范
// Detect if the user has created a MultipartResolver but named it incorrectly
return resolver;
}
给容器中加入了文件上传解析器;
@Bean
@ConditionalOnMissingBean
public CharacterEncodingFilter characterEncodingFilter() {
}
//springboot也配置了字符编码过滤器,但是你自己要定义了就会使用你的
<div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="#{roles.manager}">User is a manager</p>
<p th:case="*">User is some other thing</p>
</div>