Flask接口签名sign原理与实例代码浅析
227
2023-05-16
springboot注入servlet的方法
问:有了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(HttpSePnbCuRzrvletRequest 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){
ServletRegistrationPnbCuRzBean 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的输出。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~