springmvc改造成springboot 笔记之针对改造

时间:2025-04-21 17:47:04

这里写自定义目录标题

  • 启动类设置
  • 注册 serlvet
  • 注册Filter
  • 注册Listener
  • 拦截器配置

(注,该o2o项目为网上的springmvc源码项目)

启动类设置

启动spring boot项目的方式
方式一:
默认的application启动,在创建项目时自动生成application启动类,直接run执行即可。
方式二:使用外置的tomcat启动
默认的启动类要继承SpringBootServletInitiailzer类,并复写configure()方法。
代码:

@EnableAutoConfiguration
public class O2oApplication extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
		return ();
	}


	public static void main(String[] args) {
		(, args);
	}
}

注册 serlvet

springboot注册servlet有两种方式:
1、是通过spring的ServletRegistrationBean注册,这个bean是由spring boot提供专门来注册servlet的,可以象注册bean一样去配置servlet。

@Configuration
public class ServletRegist {

    /**
     *生成图片的Servlet
     * 1、通过@Bean注解注册实例
     * 2、返回类型为ServletRegistrationBean
     * 3、new KaptchaServlet() 第三方依赖包的对象
     * 4、设置相关拦截器参数initParams,对应 中的<init-param>
     * 5、设置映射的url;setUrlMappings,多个url的话可以放到list
     *  6、setLoadOnStartup 设置启动级别
     * @return
     */
    @Bean
    public ServletRegistrationBean kaptchaServletBean(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new KaptchaServlet());
        Map<String,String> initParams = new HashMap<>();
        //是否有边框
        ("","no");
        //字体颜色
        ("","red");
        //图片宽度
        ("","135");
        //使用哪些字符生成验证码
        ("","ACDEFHKPRSTWX345679");
        //图片高度
        ("","50");
        //字体大小
        ("","43");
        //干扰线的颜色
        ("","black");
        //字符个数
        ("","4");
        //使用哪些字体
        ("","Arial");
        (initParams);
        ("Kaptcha");
        ArrayList urlList = new ArrayList();
        ("/Kaptcha");
        (urlList);
        (1);
        return bean;
    }
}

其中需要注意的是(“rest”),这个语句很重要,因为name相同的ServletRegistrationBean只有一个会生效,也就是说,后注册的会覆盖掉name相同的ServletRegistrationBean。
如果不指定,默认为“dispatcherServlet”而spring boot提供的DispatcherServlet的name就是“dispatcherServlet”。
(SpringBoot帮我们自动的注册SpringMVC的前端控制器:DIspatcherServlet。)
所以为了不覆盖默认的dispatcherServlet,必须指定一个别的名称。

2、另外一种是通过注解注册:
@WebServlet注解
这个是javaee的注解,是servlet3.0以后提供的。spring boot会扫描这个注解,并将这个注解注解的类注册到web容器中作为一个servlet。

@WebServlet(name="helloServlet",urlPatterns={"/hello"},initParams={@WebInitParam(name="age",value="11")})
public class HelloServlet extends HttpServlet {
		//dosomething
}

但是如果是第三方依赖包中的类则无法通过注解方式注册,只能通过第1种方式注册。
在自定义的DispathcerServlet绑定的配置类上,要配置报扫描的话,必须要加上@EnableWebMvc注解,不然不会扫描@Contrller注解。

注册Filter

1、通过FilterRegistrationBean 注册

@Bean
public FilterRegistrationBean myFilter(){
     FilterRegistrationBean registrationBean = new FilterRegistrationBean();
     (new MyFilter());
     (("/hello","/myServlet"));
     return registrationBean;
 }

2、通过注解@WebFilter(urlPatterns={"/*"})

@WebFilter(urlPatterns={"/*"})
public class UserFilter implements Filter {
	//dosomething
}

注:由于该项目改造时未有filter,所以这里的code直接copy网上,顺带也提到注册filter方式

注册Listener

1、通过ServletListenerRegistrationBean 注册

 @Bean
    public ServletListenerRegistrationBean myListener(){
        ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
        return registrationBean;
    }

2、通过注解 @WebListener 注册

@WebListener
public class UserListener implements ServletContextListener {
			//dosomething
}

注:由于该项目改造时未有Listener,所以这里的code直接copy网上,顺带也提到注册Listener方式

以上通过注解方式注册的时候,需要在主配置类上添加@ServletComponentScan

@SpringBootApplication
@MapperScan("com.")
@EnableAutoConfiguration
@ServletComponentScan
public class O2oApplication extends SpringBootServletInitializer {
}

拦截器配置

1、实现WebMvcConfigurationSupport
2、重写addInterceptors
3、多个拦截器组成一个拦截器链

@Configuration
public class MyInterceptorConfigurer extends WebMvcConfigurationSupport {

    /**
     * 多个拦截器组成一个拦截器链
     * addPathPatterns 用于添加拦截规则
     * excludePathPatterns 用户排除拦截
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry){
        (new ShopLoginInterceptor()).addPathPatterns("/*");
        (new ShopPermissionInterceptor()).addPathPatterns("/*");
        (new SuperAdminLoginInterceptor()).addPathPatterns("/*");
        (registry);
    }
}

其他改造未完······

参考
/p/be2dafc8c644
/J080624/article/details/80758614