在利用 Spring 集成 thrift 时,报错如下:
javax.servlet.ServletException: Servlet.init() for servlet search-nlp-service threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:)
java.lang.Thread.run(Thread.java:)
root cause org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'extractAbstractThriftServer' defined in class path resource [application-thrift-context.xml]: Cannot resolve reference to bean 'ExtractAbstractServiceHandler' while setting bean property 'handler'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'ExtractAbstractServiceHandler' is defined
org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:)
org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:)
org.springframework.beans.factory.support.AbstractBeanFactory$.getObject(AbstractBeanFactory.java:)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:)
javax.servlet.GenericServlet.init(GenericServlet.java:)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:)
java.lang.Thread.run(Thread.java:)
root cause org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'ExtractAbstractServiceHandler' is defined
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:)
org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:)
org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:)
org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:)
org.springframework.beans.factory.support.AbstractBeanFactory$.getObject(AbstractBeanFactory.java:)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:)
javax.servlet.GenericServlet.init(GenericServlet.java:)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:)
java.lang.Thread.run(Thread.java:)
解决办法:
Spring 的 contextConfigLocation 配置下的 xml文件的主要内容如下:
<util:properties id="extractAbstractThrift" location="classpath:extractAbstract-thrift.properties" /> <bean id="extractAbstractThriftServer" class="com.demo.thrift.server.ThriftServer">
<property name="handler" ref="ExtractAbstractServiceHandler"></property>
<property name="serverType" value="threadselected"></property>
<property name="protocolType" value="compact"></property>
<property name="port" value="#{extractAbstractThrift['extractAbstractThrift.port']}"></property>
<property name="workerSize" value="#{extractAbstractThrift['extractAbstractThrift.worker.size']}"></property>
<property name="selectorSize" value="#{extractAbstractThrift['extractAbstractThrift.selecter.size']}"></property>
</bean>
<bean id="extractAbstractServiceHandler" class="com.demo.search.extractAbstract.service.ExtractAbstractServiceHandler"></bean>
通过检查xml文件,为了与 handler 的 ref 相对应,应该把上面的绿色的 extractAbstractServiceHandler,改为 ExtractAbstractServiceHandler 即可。