2014-12-03 130 views
0

我正在運行Tomcat,並且一切似乎都正常,直到我實際嘗試在Postman中調出頁面,然後出現此錯誤(完整堆棧跟蹤) :org.springframework.beans.factory.UnsatisfiedDependencyException:創建名爲bean的bean時出錯

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'stopController' defined in file [C:\Users\Scott Meyer\metrolink-project\target\classes\metrolink\rest\mvc\StopController.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [metrolink.core.services.StopService]: : No qualifying bean of type [metrolink.core.services.StopService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [metrolink.core.services.StopService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:751) 
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1133) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1036) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725) 
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:658) 
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624) 
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672) 
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543) 
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484) 
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) 
at javax.servlet.GenericServlet.init(GenericServlet.java:160) 
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280) 
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193) 
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:865) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) 
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
at java.lang.Thread.run(Thread.java:744) 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [metrolink.core.services.StopService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1261) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1009) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904) 
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:815) 
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:743) 
... 36 more 

Dec 03, 2014 1:42:13 PM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Allocate exception for servlet mvc-dispatcher 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [metrolink.core.services.StopService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1261) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1009) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904) 
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:815) 
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:743) 
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1133) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1036) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725) 
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:658) 
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624) 
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672) 
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543) 
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484) 
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) 
at javax.servlet.GenericServlet.init(GenericServlet.java:160) 
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280) 
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193) 
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:865) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) 
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
at java.lang.Thread.run(Thread.java:744) 

這是我的業務配置文件:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 


<context:component-scan base-package="metrolink.core.repositories.jpa"/> 

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> 
    <property name="driverClassName" value="org.sqlite.JDBC" /> 
    <property name="url" value="jdbc:sqlite:metrolink.db" /> 
</bean> 

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> 
    </property> 
    <property name="jpaProperties"> 
     <map> 
      <entry key="hibernate.hbm2ddl.auto" value="create-drop"/> 
      <entry key="hibernate.show_sql" value="true"/> 
      <entry key="hibernate.dialect" value="org.hibernate.dialect.SQLiteDialect" /> 
      <entry key="hibernate.current_session_context_class" value="thread" /> 
     </map> 
    </property> 
    <property name="packagesToScan" value="metrolink.core.entities"/> 
</bean> 

<tx:annotation-driven/> 

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/> 

<context:component-scan base-package="metrolink.core.services" /> 

    </beans> 

及相關代碼:

package metrolink.core.services.impl; 

import etc... 

@Service 
@Transactional 
public class StopServiceImpl implements StopService { 

@Autowired 
private StopRepo stopRepo; 

@Override 
public Stop find(int stopId) { 
    Stop stop = stopRepo.find(stopId); 
    if(stop == null) { 
     throw new StopDoesNotExistException(); 
    } 
    return stop; 
} 

@Override 
public Stop findByStopName(String name) { 
    return stopRepo.findStopByName(name); 
} 

@Override 
public StopList findAllStops() { 
    return new StopList (stopRepo.findAllStops()); 
} 
} 

package metrolink.rest.mvc; 

import etc... 

@Controller 
@RequestMapping(value="/rest/stops") 
public class StopController { 

private StopService stopService; 

@Autowired 
public StopController(StopService stopService) { 
    this.stopService = stopService; 
} 

@RequestMapping(method= RequestMethod.GET) 
public ResponseEntity<StopListResource> findAllStops(@RequestParam(value="name", required=false) String name) { 
    StopList list = null; 
    if(name == null) { 
     list = stopService.findAllStops(); 
    } else { 
     Stop stop = stopService.findByStopName(name); 
     if(stop == null) { 
      list = new StopList(new ArrayList<Stop>()); 
     } else { 
      list = new StopList(Arrays.asList(stop)); 
     } 
    } 
    StopListResource res = new StopListResourceAsm().toResource(list); 
    return new ResponseEntity<StopListResource>(res, HttpStatus.OK); 
} 

@RequestMapping(value="/{stopId}", method= RequestMethod.GET) 
public ResponseEntity<StopResource> getStop(@PathVariable int stopId) { 
    Stop stop = stopService.find(stopId); 
    if(stop != null) { 
     StopResource res = new StopResourceAsm().toResource(stop); 
     return new ResponseEntity<StopResource>(res, HttpStatus.OK); 
    } else { 
     return new ResponseEntity<StopResource>(HttpStatus.NOT_FOUND); 
    } 
} 

@RequestMapping(method= RequestMethod.GET) 
public ResponseEntity<StopListResource> findAllStops() { 
    StopList stopList = stopService.findAllStops(); 
    StopListResource stopListRes = new StopListResourceAsm().toResource(stopList); 
    return new ResponseEntity<StopListResource>(stopListRes, HttpStatus.OK); 
} 

} 

這裏是我的web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
     http://java.sun.com/xml/ns/javaee 
     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    version="3.0"> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:spring/business-config.xml</param-value> 
</context-param> 

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<servlet> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
</servlet> 

<servlet-mapping> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

</web-app> 

回答

4

只包括以下行。刪除所有其他上下文:組件掃描條目。

<context:component-scan base-package="metrolink.core"/> 
+0

工作,只有現在我得到「所請求的資源不可用」。我發佈了我的web.xml以防萬一與它有關。 – 2014-12-04 18:45:05

+0

哪些資源? – 2014-12-05 16:22:58

+0

這是Web應用程序的後端。我嘗試在瀏覽器中顯示它,這是我得到的錯誤消息。我似乎看起來很好的服務器端,沒有錯誤,沒有在日誌中。 – 2014-12-05 21:31:09

相關問題