为什么我不能通过Spring Boot中的@Bean注释注册HttpRequestHandlerServlet

问题描述 投票:0回答:1

我目前正在使用Spring Boot和qazxsw poi Java Class来注册Servlet qazxsw poi

但它总是告诉我那是不可用的

这是我的bean注册代码类:

@Configuration

这是我的Stacktrace

HttpRequestHandlerService

还有我的Web XML

@Configuration
public class WebConfiguration extends SpringBootServletInitializer {

    private static final String SERVLET_NAME = "DataFileServlet";

    /**
     * Initializer servlet context initializer.
     *
     * @return the servlet context initializer
     */
    @Bean
    public ServletContextInitializer initializer() {
        return servletContext -> {
            servletContext.setInitParameter("javax.faces.FACELETS_SKIP_COMMENTS", "true");
            servletContext.setInitParameter("contextConfigLocation", "classpath:applicationContext.xml");
            servletContext.setInitParameter("javax.faces.FACELETS_REFRESH_PERIOD", "1");
            servletContext.setInitParameter("primefaces.THEME", "admin");
            servletContext.setInitParameter("primefaces.FONT_AWESOME", "true");
            servletContext.setInitParameter("com.sun.faces.sendPoweredByHeader", "false");
            servletContext.setInitParameter("javax.faces.CLIENT_WINDOW_MODE", "url");
        };
    }

    /**
     * Is used to register the servlet HttpRequestHandler.
     *
     * @return the servlet "registartionBean"
     */
    @Bean
    public ServletRegistrationBean<HttpRequestHandlerServlet> dataFileServletRegistration() {
        ServletRegistrationBean<HttpRequestHandlerServlet> registrationBean = new ServletRegistrationBean<>(new HttpRequestHandlerServlet());
        registrationBean.setName(SERVLET_NAME);
        registrationBean.setLoadOnStartup(1);
        return registrationBean;
    }

    /**
     * Is responcable for registering a filter.
     *
     * @return the filter "registrationBean"
     */
    @Bean
    public FilterRegistrationBean<CharacterEncodingFilter> encodingFilter() {
        FilterRegistrationBean<CharacterEncodingFilter> registrationBean = new FilterRegistrationBean<>(new CharacterEncodingFilter());
        registrationBean.setEnabled(true);
        registrationBean.setOrder(1);
        registrationBean.addInitParameter("encoding","UTF-8" );
        registrationBean.addInitParameter("forceEncoding", "true");
        return registrationBean;
    }
    /**
     * Is resposible for registering a new listener.
     *
     * @return the listener "registrationBean"
     */
    @Bean ServletListenerRegistrationBean<IntrospectorCleanupListener> IntrospectorCleanupListener(){
        ServletListenerRegistrationBean<IntrospectorCleanupListener> registrationBean = new ServletListenerRegistrationBean<>();
        registrationBean.setListener(new IntrospectorCleanupListener());
        return registrationBean;
    }

}

我该怎么做才能让它正常工作?

java spring-boot servlets
1个回答
2
投票

2019-02-14 16:16:35.151 ERROR 5468 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : StandardWrapper.Throwable org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'DataFileServlet' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:772) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1221) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:294) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089) at org.springframework.web.context.support.HttpRequestHandlerServlet.init(HttpRequestHandlerServlet.java:60) at javax.servlet.GenericServlet.init(GenericServlet.java:158) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1123) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:974) at org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedContext.load(TomcatEmbeddedContext.java:83) at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184) at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382) at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580) at java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:270) at java.util.TreeMap$ValueSpliterator.forEachRemaining(TreeMap.java:2897) at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151) at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174) at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418) at org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedContext.lambda$deferredLoadOnStartup$0(TomcatEmbeddedContext.java:65) at org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedContext.doWithThreadContextClassLoader(TomcatEmbeddedContext.java:108) at org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedContext.deferredLoadOnStartup(TomcatEmbeddedContext.java:64) at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.performDeferredLoadOnStartup(TomcatWebServer.java:282) at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:200) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.startWebServer(ServletWebServerApplicationContext.java:311) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:164) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) at de.Application.main(Application.java:33) 2019-02-14 16:16:35.233 INFO 5468 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2019-02-14 16:16:35.234 ERROR 5468 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Failed to destroy the filter named [Tomcat WebSocket (JSR356) Filter] of type [org.apache.tomcat.websocket.server.WsFilter] java.lang.AbstractMethodError: null at org.apache.catalina.core.ApplicationFilterConfig.release(ApplicationFilterConfig.java:301) at org.apache.catalina.core.StandardContext.filterStop(StandardContext.java:4543) at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5345) at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1398) at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1387) at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) at java.util.concurrent.FutureTask.run(FutureTask.java) at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134) at org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:974) at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1398) at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1387) at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) at java.util.concurrent.FutureTask.run(FutureTask.java) at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134) at org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:974) at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) at org.apache.catalina.core.StandardService.stopInternal(StandardService.java:475) at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) at org.apache.catalina.core.StandardServer.stopInternal(StandardServer.java:995) at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) at org.apache.catalina.startup.Tomcat.stop(Tomcat.java:408) at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.stopTomcat(TomcatWebServer.java:250) at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.stop(TomcatWebServer.java:306) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.stopAndReleaseWebServer(ServletWebServerApplicationContext.java:320) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) at de.Application.main(Application.java:33) 2019-02-14 16:16:35.244 INFO 5468 --- [ main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-02-14 16:16:35.309 ERROR 5468 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: A component required a bean named 'DataFileServlet' that could not be found. Action: Consider defining a bean named 'DataFileServlet' in your configuration. 代表加入<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <context-param> <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name> <param-value>1</param-value> </context-param> <context-param> <param-name>primefaces.THEME</param-name> <param-value>admin</param-value> </context-param> <context-param> <param-name>primefaces.FONT_AWESOME</param-name> <param-value>true</param-value> </context-param> <!-- security: don´t send x-powered-by-header --> <context-param> <param-name>com.sun.faces.sendPoweredByHeader</param-name> <param-value>false</param-value> </context-param> <context-param> <param-name>javax.faces.CLIENT_WINDOW_MODE</param-name> <param-value>url</param-value> </context-param> <servlet> <servlet-name>DataFileServlet</servlet-name> <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DataFileServlet</servlet-name> <url-pattern>/DataFileServlet</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> 豆。这在类的javadoc中描述:

委托给Spring的根Web应用程序上下文中定义的HttpRequestHandlerServlet bean。目标bean名称必须与HttpRequestHandler servlet-name匹配

在为servlet定义注册bean时,您已将其名称设置为HttpRequestHandler。因此,当servlet收到请求时,它正在尝试检索名为HttpRequestHandlerServlet的bean。这是失败的,因为没有使用该名称定义bean。

要在配置DataFileServlet时使用DataFileServlet,您需要定义一个名为HttpRequestHandlerServletHttpRequestHandler bean:

DataFileServlet
© www.soinside.com 2019 - 2024. All rights reserved.