springboot框架笔记——springboot提供的自动配置

Springboot基本配置

  spring MVC的定制配置需要我们的配置实现一个WebMvcConfigurer接口,如果实在spring环境下需要使用@EnableWebMVC注解,来开启对spring MVC的配置支持,这是我们就可以重写WebMvcConfigurer中的方法,完成我们的常用配置。

 1 /**
 2      * 设置允许跨域请求
 3      * @return
 4      */
 5     @Override
 6     public void addCorsMappings(CorsRegistry registry) {
 7         ConstantConfig config = context.getBean(ConstantConfig.class);
 8         Cors cors = config.getCors();
 9         registry.addMapping(cors.getMapping())
10         .allowedOrigins(cors.getOrigins())
11         .allowedMethods(cors.getMethods())
12         .allowCredentials(cors.getCredentials()).maxAge(cors.getMaxAge());
13         log.info(String.format("允许原域%s使用方法%S访问路径%s",
14                 Arrays.toString(cors.getOrigins()),
15                 Arrays.toString(cors.getMethods()),
16                 cors.getMapping()));
17     }
18     
19     
20 
21     @Override
22     public void configurePathMatch(PathMatchConfigurer configurer) {
23         configurer.setUseSuffixPatternMatch(false);// 在匹配路径的时候忽略后缀
24     }
25 
26 
27     // 添加静态资源访问路径
28     @Override
29     public void addResourceHandlers(ResourceHandlerRegistry registry) {
30         // addResourceHandler指对外暴露的访问路径,addResourcesLocations指的是配置文件存放的目录
31         registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets");
32     }
33     // 添加视图映射路径
34     @Override
35     public void addViewControllers(ViewControllerRegistry registry) {
36         // 
37         registry.addViewController("/").setViewName("index.html");
38     }

 

posted @ 2018-10-22 08:53  悦尔  阅读(482)  评论(0编辑  收藏  举报