Spring 引导自定义 Servlet 未映射到 bean 名称
Spring boot custom Servlet doesn't map to bean name
我正在尝试注册自定义 servlet。
我在 @Configuration
class:
中使用了这段代码
@Bean (name="probe")
public PingServlet probe(){
return new PingServlet();
}
我以为这会映射到 /probe,但事实并非如此。我映射到“/”,原因是在 class ServletContextInitializerBeans
中,有这个方法:
private <T, B extends T> void addAsRegistrationBean(ListableBeanFactory beanFactory,
Class<T> type, Class<B> beanType, RegistrationBeanAdapter<T> adapter) {
List<Map.Entry<String, B>> beans = getOrderedBeansOfType(beanFactory, beanType,
this.seen);
for (Entry<String, B> bean : beans) {
if (this.seen.add(bean.getValue())) {
int order = getOrder(bean.getValue());
String beanName = bean.getKey();
// One that we haven't already seen
RegistrationBean registration = adapter.createRegistrationBean(beanName,
bean.getValue(), beans.size());
registration.setName(beanName);
registration.setOrder(order);
this.initializers.add(type, registration);
if (this.log.isDebugEnabled()) {
this.log.debug(
"Created " + type.getSimpleName() + " initializer for bean '"
+ beanName + "'; order=" + order + ", resource="
+ getResourceDescription(beanName, beanFactory));
}
}
}
}
行 List<Map.Entry<String, B>> beans = getOrderedBeansOfType(beanFactory, beanType, this.seen);
,return 仅列出 1 个 bean(我的 servlet),尽管 beanType 是 javax Servlet,我希望 DispatcherServlet 也在那里(我也在使用 SpringMVC).
这会导致以下方法出错(在 class ServletRegistrationBeanAdapter
中):
@Override
public RegistrationBean createRegistrationBean(String name, Servlet source,
int totalNumberOfSourceBeans) {
String url = (totalNumberOfSourceBeans == 1 ? "/" : "/" + name + "/");
if (name.equals(DISPATCHER_SERVLET_NAME)) {
url = "/"; // always map the main dispatcherServlet to "/"
}
ServletRegistrationBean bean = new ServletRegistrationBean(source, url);
bean.setMultipartConfig(this.multipartConfig);
return bean;
}
由于 beans 列表的大小为 1,因此在 createRegistrationBean 中它将映射硬编码为“/”。
这反过来会导致他们嵌入的码头无法启动,因为有 2 个映射到“/”(DispatcherServlet 和我的 PingServlet)。
知道这里出了什么问题吗?
感谢@M。 Deinum 这有效:
@Bean
public ServletRegistrationBean pingRegistration(PingServlet pingServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(
pingServlet);
registration.addUrlMappings("/probe/*");
return registration;
}
我正在尝试注册自定义 servlet。
我在 @Configuration
class:
@Bean (name="probe")
public PingServlet probe(){
return new PingServlet();
}
我以为这会映射到 /probe,但事实并非如此。我映射到“/”,原因是在 class ServletContextInitializerBeans
中,有这个方法:
private <T, B extends T> void addAsRegistrationBean(ListableBeanFactory beanFactory,
Class<T> type, Class<B> beanType, RegistrationBeanAdapter<T> adapter) {
List<Map.Entry<String, B>> beans = getOrderedBeansOfType(beanFactory, beanType,
this.seen);
for (Entry<String, B> bean : beans) {
if (this.seen.add(bean.getValue())) {
int order = getOrder(bean.getValue());
String beanName = bean.getKey();
// One that we haven't already seen
RegistrationBean registration = adapter.createRegistrationBean(beanName,
bean.getValue(), beans.size());
registration.setName(beanName);
registration.setOrder(order);
this.initializers.add(type, registration);
if (this.log.isDebugEnabled()) {
this.log.debug(
"Created " + type.getSimpleName() + " initializer for bean '"
+ beanName + "'; order=" + order + ", resource="
+ getResourceDescription(beanName, beanFactory));
}
}
}
}
行 List<Map.Entry<String, B>> beans = getOrderedBeansOfType(beanFactory, beanType, this.seen);
,return 仅列出 1 个 bean(我的 servlet),尽管 beanType 是 javax Servlet,我希望 DispatcherServlet 也在那里(我也在使用 SpringMVC).
这会导致以下方法出错(在 class ServletRegistrationBeanAdapter
中):
@Override
public RegistrationBean createRegistrationBean(String name, Servlet source,
int totalNumberOfSourceBeans) {
String url = (totalNumberOfSourceBeans == 1 ? "/" : "/" + name + "/");
if (name.equals(DISPATCHER_SERVLET_NAME)) {
url = "/"; // always map the main dispatcherServlet to "/"
}
ServletRegistrationBean bean = new ServletRegistrationBean(source, url);
bean.setMultipartConfig(this.multipartConfig);
return bean;
}
由于 beans 列表的大小为 1,因此在 createRegistrationBean 中它将映射硬编码为“/”。
这反过来会导致他们嵌入的码头无法启动,因为有 2 个映射到“/”(DispatcherServlet 和我的 PingServlet)。
知道这里出了什么问题吗?
感谢@M。 Deinum 这有效:
@Bean
public ServletRegistrationBean pingRegistration(PingServlet pingServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(
pingServlet);
registration.addUrlMappings("/probe/*");
return registration;
}