我需要在我的应用程序中注册一个额外的servlet。但是使用Spring Boot及其Java Config,我不能只在web.xml
文件中添加servlet映射。
如何添加其他servlet?
答案 0 :(得分:120)
同样可用ServletRegistrationBean
@Bean
public ServletRegistrationBean servletRegistrationBean(){
return new ServletRegistrationBean(new FooServlet(),"/someOtherUrl/*");
}
最终成为我走的路。
答案 1 :(得分:51)
Just add a bean for the servlet。它将映射到/{beanName}/
。
@Bean
public Servlet foo() {
return new FooServlet();
}
答案 2 :(得分:19)
你可以在Application类中使用不同的ServletRegistrationBean注册多个不同的servlet,比如@Bean,你可以注册一个servlet有多个servlet映射;
@Bean
public ServletRegistrationBean axisServletRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(new AxisServlet(), "/services/*");
registration.addUrlMappings("*.jws");
return registration;
}
@Bean
public ServletRegistrationBean adminServletRegistrationBean() {
return new ServletRegistrationBean(new AdminServlet(), "/servlet/AdminServlet");
}
答案 3 :(得分:4)
我们也可以按照以下方式注册Servlet:
@Configuration
public class ConfigureWeb implements ServletContextInitializer, EmbeddedServletContainerCustomizer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
registerServlet(servletContext);
}
private void registerServlet(ServletContext servletContext) {
log.debug("register Servlet");
ServletRegistration.Dynamic serviceServlet = servletContext.addServlet("ServiceConnect", new ServiceServlet());
serviceServlet.addMapping("/api/ServiceConnect/*");
serviceServlet.setAsyncSupported(true);
serviceServlet.setLoadOnStartup(2);
}
}
答案 4 :(得分:1)
如果您使用的是嵌入式服务器,则可以使用@WebServlet
servlet类进行注释:
@WebServlet(urlPatterns = "/example")
public class ExampleServlet extends HttpServlet
来自@WebServlet:
用于声明servlet的注释。
此注释在部署时由容器处理,并且 相应的servlet在指定的URL处可用 图案。
在基类上启用@ServletComponentScan
:
@ServletComponentScan
@EntityScan(basePackageClasses = { ExampleApp.class, Jsr310JpaConverters.class })
@SpringBootApplication
public class ExampleApp
请注意,@ServletComponentScan仅适用于嵌入式服务器:
启用对Servlet组件的扫描(过滤器,servlet和 听众)。仅在使用嵌入式Web时执行扫描 服务器
答案 5 :(得分:0)
在BeanDefinitionRegistryPostProcessor中也可用
package bj;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@SpringBootApplication
class App implements BeanDefinitionRegistryPostProcessor {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
registry.registerBeanDefinition("myServlet", new RootBeanDefinition(ServletRegistrationBean.class,
() -> new ServletRegistrationBean<>(new HttpServlet() {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.getWriter().write("hello world");
}
}, "/foo/*")));
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
}
}
答案 6 :(得分:0)
这种方式对我有用,拥有一个名为WS01455501EndpointFor89的servlet
@Bean
public ServletRegistrationBean<WS01455501EndpointFor89> servletRegistrationBeanAlt(ApplicationContext context) {
ServletRegistrationBean<WS01455501EndpointFor89> servletRegistrationBean = new ServletRegistrationBean<>(new WS01455501EndpointFor89(),
"/WS01455501Endpoint");
servletRegistrationBean.setLoadOnStartup(1);
return servletRegistrationBean;
}