其实就是注入
FilterRegistrationBean 、
ServletRegistrationBean 、
ServletListenerRegistrationBean
这三个类
直接上代码:
@Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean registrationBean = new FilterRegistrationBean(); CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); characterEncodingFilter.setEncoding("UTF-8"); registrationBean.setFilter(characterEncodingFilter); return registrationBean; } @Bean public ServletRegistrationBean servlet(){ return new ServletRegistrationBean(); } @Bean public ServletListenerRegistrationBean<EventListener>(){ return new ServletListenerRegistrationBean<>(new EventListener()); }