Spring @Autowired of @Service类适用于Tomcat但不适用于Resin

时间:2015-03-30 22:17:43

标签: spring tomcat spring-data-rest resin

我使用Spring restful服务和spring核心库将Tomcat 8.0.20中的100%工作解决方案打包为WAR。

当我使用相同的WAR并将其放入Resin 4.0.36(以及4.0.43)时,我的@Autowired @Services无法识别。

这让我相信Tomcat和Resin之间存在环境差异。我是否需要明确设置一些属性或引用某些内容才能使WAR在Resin上工作?

    2015-03-30 17:26:25 WARN  XmlWebApplicationContext:487 - Exception    
encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userInfoWebService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ca.abcc.service.user.UserService ca.abcc.controller.webservice.UserInfo.UserInfoWebService.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ca.abcc.service.user.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:629)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:677)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:548)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:70)
at com.caucho.server.dispatch.ServletConfigImpl.createServletImpl(ServletConfigImpl.java:1358)
at com.caucho.server.dispatch.ServletConfigImpl.createServlet(ServletConfigImpl.java:1254)
at com.caucho.server.dispatch.ServletConfigImpl.createServlet(ServletConfigImpl.java:1205)
at com.caucho.server.dispatch.ServletManager.init(ServletManager.java:251)
at com.caucho.server.webapp.WebApp.startImpl(WebApp.java:3777)
at com.caucho.server.webapp.WebApp.access$700(WebApp.java:208)
at com.caucho.server.webapp.WebApp$StartupTask.run(WebApp.java:5297)
at com.caucho.env.thread2.ResinThread2.runTasks(ResinThread2.java:173)
at com.caucho.env.thread2.ResinThread2.run(ResinThread2.java:118)
Caused by: org.springframework.beans.factory.BeanCreationException: Could   
not autowire field: private ca.abcc.service.user.UserService 
ca.abcc.controller.webservice.UserInfo.UserInfoWebService.userService; 
nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No 
qualifying bean of type [ca.abcc.service.user.UserService] found for 
dependency: expected at least 1 bean which qualifies as autowire candidate 
for this dependency. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostPr
ocessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at 
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 26 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:    
No qualifying bean of type [ca.abcc.service.user.UserService] found 
for dependency: expected at least 1 bean which qualifies as autowire 
candidate for this dependency. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
at 
 org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 28 more
2015-03-30 17:26:25 ERROR DispatcherServlet:497 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userInfoWebService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ca.abcc.service.user.UserService ca.abcc.controller.webservice.UserInfo.UserInfoWebService.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ca.abcc.service.user.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
{resin-50} WebApp[production/webapp/default/Impact.Auto.Online.Buyer.Portal] fail
[15-03-30 17:26:25.294] {resin-15} org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ca.impactauto.service.user.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
                    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
                    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
                    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
                    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
                    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
                    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
                    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
                    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
                    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
                    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
                    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
                    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
                    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
                    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663)
                    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:629)
                    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:677)
                    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:548)
                    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489)
                    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
                    at javax.servlet.GenericServlet.init(GenericServlet.java:70)
                    at com.caucho.server.dispatch.ServletConfigImpl.createServletImpl(ServletConfigImpl.java:1358)
                    at com.caucho.server.dispatch.ServletConfigImpl.createServlet(ServletConfigImpl.java:1254)
                    at com.caucho.server.dispatch.ServletConfigImpl.createServlet(ServletConfigImpl.java:1205)
                    at com.caucho.server.dispatch.ServletManager.init(ServletManager.java:251)
                    at com.caucho.server.webapp.WebApp.startImpl(WebApp.java:3777)
                    at com.caucho.server.webapp.WebApp.access$700(WebApp.java:208)
                    at com.caucho.server.webapp.WebApp$StartupTask.run(WebApp.java:5297)
                    at com.caucho.env.thread2.ResinThread2.runTasks(ResinThread2.java:173)
                    at com.caucho.env.thread2.ResinThread2.run(ResinThread2.java:118)

Dispatcher Servlet:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
xmlns:context="http://www.springframework.org/schema/context"  
xmlns:p="http://www.springframework.org/schema/p"   
xmlns:mvc="http://www.springframework.org/schema/mvc"   
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-4.0.xsd  
http://www.springframework.org/schema/mvc  
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
">  

<mvc:annotation-driven />
<!-- <mvc:default-servlet-handler/>-->
<!-- <context:annotation-config />-->
<context:component-scan base-package="ca.xyz.controller" />
<context:annotation-config />

<bean id="viewResolver"    
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!-- <property name="viewClass" 
value="org.springframework.web.servlet.view.JstlView" />-->
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="messageSource" 
class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages"/>
</bean>

<mvc:resources mapping="/resources/**" location="/resources/" />

<mvc:view-controller path="/" view-name="welcome"/>

</beans>

1 个答案:

答案 0 :(得分:0)

还不知道为什么但这最终起作用了。我必须识别每个上下文文件:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:service-context.xml classpath:data-context.xml</param-value>
</context-param>

而不是:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:*-context.xml</param-value>
</context-param>

原始(后者)解决方案在Tomcat中使用类路径*:* ant样式通配符查找完全正常但在Resin 4.0.42,4.0.43和4.0.36中则不然。