欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot注入servlet的方法

程序员文章站 2024-02-22 19:27:35
问:有了springmvc,为什么还要用servlet?有了servlet3的注解,为什么还要使用servletregistrationbean注入的方式? 使用场景:在...

问:有了springmvc,为什么还要用servlet?有了servlet3的注解,为什么还要使用servletregistrationbean注入的方式?

使用场景:在有些场景下,比如我们要使用hystrix-dashboard,这时候就需要注入hystrixmetricsstreamservlet(第三方的servlet),该servlet是hystrix的组件。

一、代码

1、testservlet(第一个servlet)

package com.xxx.secondboot.servlet;

import java.io.ioexception;

import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

public class testservlet extends httpservlet {
  
  private static final long serialversionuid = -4619665430596950563l;

  @override
  protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
    system.out.println("zhaojigang servlet");
  }

  @override
  protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
    this.doget(req, resp);
  }
}

2、testservlet2(第二个servlet)

package com.xxx.secondboot.servlet;

import java.io.ioexception;

import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

public class testservlet2 extends httpservlet {

  private static final long serialversionuid = 3788279972938793265l;

  @override
  protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
    system.out.println("zhaojigang servlet2");
  }

  @override
  protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
    this.doget(req, resp);
  }
}

3、servletconfig(servlet注入配置类)

package com.xxx.secondboot.servlet;

import org.springframework.boot.context.embedded.servletregistrationbean;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

@configuration
public class servletconfig {
  
  @bean
  public testservlet testservlet(){
    return new testservlet();
  }
  
  @bean
  public servletregistrationbean testservletregistrationbean(testservlet testservlet){
    servletregistrationbean registration = new servletregistrationbean(testservlet);
    registration.setenabled(true);
    registration.addurlmappings("/servlet/test");
    return registration;
  }
  /********************************************/
  @bean
  public testservlet2 testservlet2(){
    return new testservlet2();
  }
  
  @bean
  public servletregistrationbean test2servletregistrationbean(testservlet2 testservlet2){
    servletregistrationbean registration = new servletregistrationbean(testservlet2);
    registration.setenabled(true);
    registration.addurlmappings("/servlet/test2");
    return registration;
  }
  
}

说明:使用servletregistrationbean来注入servlet,对于每一个servlet都有一个servletregistrationbean来注入。

注意:如果只是自己要使用servlet,可以直接只用servlet3的注解来声明servlet就好,但是像hystrixmetricsstreamservlet这样的第三方servlet,就只能通过上边这样的方式来搞了。

 二、测试

启动服务,浏览器输入"http://localhost:8083/servlet/test","http://localhost:8083/servlet/test2",查看console的输出。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。