springboot定制化装配的常见方式

springboot定制化装配的常见方式

  1. 修改配置文件;

  2. 编写自定义的配置类 xxxConfiguration;+ @Bean替换、增加容器中默认组件;视图解析器 ,pojo等组件

    ​ 可以用于装配 filter过滤器,servlet、listener等。

    @Configuration
    public class MyRegisterConfig {
        @Bean
        public ServletRegistrationBean myServlet(){
            // MyServlet是自己创建的类,
            // 使用这种方式:装配MyServlet到ioc容器中。
            MyServlet myServlet = new MyServlet();
            return new ServletRegistrationBean(myServlet, "/my", "/my01");
        }
    
        @Bean
        public FilterRegistrationBean myFilter(){
            MyFilter myFilter = new MyFilter();
            return new FilterRegistrationBean(myFilter, myServlet());
        }
    
        @Bean
        public ServletListenerRegistrationBean servletListenerRegistrationBean(){
            MyServletContextListener listener = new MyServletContextListener();
            return new ServletListenerRegistrationBean(listener);
        }
    
    }
    
  3. Web应用 编写一个配置类实现WebMvcConfigurer 即可定制化web功能+ @Bean给容器中再扩展一些组件

    如:

public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/", "/login", "/main", "/css/**", "/fonts/**", 					"/images/**", "/js/**");
    }
}
  • @EnableWebMvc + WebMvcConfigurer —— @Bean 可以全面接管SpringMVC,所有规则全部自己重新配置; 实现定制和扩展功能

    • 原理
    • 1、WebMvcAutoConfiguration 默认的SpringMVC的自动配置功能类。静态资源、欢迎页…
    • 2、一旦使用 @EnableWebMvc 、。会 @Import(DelegatingWebMvcConfiguration.class)
    • 3、DelegatingWebMvcConfiguration 的 作用,只保证SpringMVC最基本的使用
      • 把所有系统中的 WebMvcConfigurer 拿过来。所有功能的定制都是这些 WebMvcConfigurer 合起来一起生效
      • 自动配置了一些非常底层的组件。RequestMappingHandlerMapping、这些组件依赖的组件都是从容器中获取
      • public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport
    • 4、WebMvcAutoConfiguration 里面的配置要能生效 必须 @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
    • 5、@EnableWebMvc 导入装配了DelegatingWebMvcConfiguration组件,即WebMvcConfigurationSupport组件,从而导致了 WebMvcAutoConfiguration 没有生效。

配置类实现WebMvcConfigurer接口,定制化的原理

引入web的starter的场景启动器,导入了web的众多依赖;springboot启动时会自动装配许多的配置类,其中就包括WebMvcAutoConfiguration

@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
		ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
    public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters){
            
        }
        
        @Bean
		@ConditionalOnMissingBean
		public InternalResourceViewResolver defaultViewResolver() {
			return resolver;
		}
    }
}

​ 自动装配了WebMvcAutoConfiguration后,这个类里面还实现了WebMvcConfigurer接口,该接口中有消息转换器等组件,WebMvcAutoConfiguration被装配到ioc容器后,实现接口的方法所返回的组件也都会被装配到ioc容器中;WebMvcAutoConfiguration自动配置类中实现了WebMvcConfigurer接口的所有方法,也就是说明该配置类已经创建并已经将所有接口方法的组件注册好了。

​ 所以,只需要创建配置类,实现WebMvcConfigurer接口,实现该接口的方法,在接口方法中注册组件到ioc容器中即可。

​ 如果,只实现该接口的某一些接口方法,其他没有实现了接口方法,任然会由WebMvcAutoConfiguration来实现并注册该方法的组件。

如:在addInterceptors接口方法中,将拦截器主键注册到ioc容器中。

public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/", "/login", "/main", "/css/**", "/fonts/**", 					"/images/**", "/js/**");
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值