Spring内部的一种配置方式
采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制。
常用的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public void addCorsMappings(CorsRegistry registry) ;
void addInterceptors(InterceptorRegistry registry);
void configureViewResolvers(ViewResolverRegistry registry);
void configureContentNegotiation(ContentNegotiationConfigurer configurer);
void addViewControllers(ViewControllerRegistry registry);
void addResourceHandlers(ResourceHandlerRegistry registry);
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);
|
addInterceptors: 拦截器
- addInterceptor: 需要一个实现HandlerInterceptor接口的拦截器实例
- addPathPatterns: 用于设置拦截器的过滤路径规则
- excludePathPatterns: 用于设置不需要拦截的过滤规则
1 2 3 4 5
| @Override public void addInterceptors(InterceptorRegistry registry) { super.addInterceptors(registry); registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**"); }
|
addCorsMappings: 跨域
1 2 3 4 5 6 7 8
| @Override public void addCorsMappings(CorsRegistry registry) { super.addCorsMappings(registry); registry.addMapping("/cors/**") .allowedHeaders("*") .allowedMethods("POST","GET") .allowedOrigins("*"); }
|
addViewControlllers: 跳转指定页面
1 2 3 4 5 6 7
| @Override public void addViewControllers(ViewControllerRegistry registry) { super.addViewControllers(registry); registry.addViewController("/").setViewName("/index"); registry.addViewController("/login").setViewName("forward:/index.html"); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
@Bean public InternalResourceViewResolver resourceViewResolver() { InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver(); internalResourceViewResolver.setPrefix("/WEB-INF/jsp/"); internalResourceViewResolver.setSuffix(".jsp"); return internalResourceViewResolver; }
@Override public void configureViewResolvers(ViewResolverRegistry registry) { super.configureViewResolvers(registry); registry.viewResolver(resourceViewResolver()); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
@Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures( SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty ); fastConverter.setFastJsonConfig(fastJsonConfig); converters.add(fastConverter);
}
|
addResourceHandlers: 静态资源
1 2 3 4 5 6 7
| @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resource/**").addResourceLocations("/WEB-INF/static/"); }
|