2015-06-20 78 views
0

的索引0我有一個錯誤與Spring 4.1.6.RELEASE。不滿意的依賴,通過構造函數的參數表示與類型

我不知道錯誤來自哪裏。 自動裝配註解想爲我創建bean。我我錯過了什麼其他的註釋?

這裏代碼

Controller類

@Controller 
@RequestMapping("/rest/accounts") 
public class AccountController { 

private AccountService accountService; 

public AccountController() {} 
@Autowired 
public AccountController(AccountService accountService) { 
    this.accountService = accountService; 
} 

庫類

@Service 
@Transactional 
public class AccountServiceImpl implements AccountService { 
@Autowired 
private AccountRepo accountRepo; 

@Autowired 
private BlogRepo blogRepo; 

@Override 
public Account findAccount(Long id) { 
    return accountRepo.findAccount(id); 
} 

這裏錯誤

HTTP狀態500 - 爲的servlet appServlet Servlet.init()拋出異常

類型異常報告

消息Servlet.init()進行的servlet appServlet拋出異常

描述服務器遇到一個內部錯誤從完成此請求阻止它 。

例外

javax.servlet.ServletException:Servlet.init()進行的servlet appServlet 拋出異常 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) org.apache。 catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java: 518) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) org.apac he.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:668) org.apache.coyote.http11.Http11NioProtocol $ Http11ConnectionHandler.process(Http11NioProtocol.java:223) org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1517) org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1474) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617) org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run (Thread.java:745)根源

org.springframework.beans.factory.UnsatisfiedDependencyException: 在文件 [/Users/Mayor/Documents/workspace/PascalWebStore/.metadata/.plugins/org.eclipse.wst.server。中定義名稱'accountController' core/tmp0/wtpwebapps/PascalWeb/WEB-INF/classes/com/sci/pascal/rest/mvc/AccountController.class]: 通過構造函數參數表示的不完全依賴 類型爲[com.sci.pascal.core .services.AccountService]:否類型[com.sci.pascal.core.services的 排位豆。AccountService] 找到依賴項:預計至少有1個bean符合 autowire這個依賴項的候選項。依賴註釋:{}; 嵌套的異常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:無類型[com.sci.pascal.core.services.AccountService] 的 排位豆找到依賴性:預期至少1豆,其有資格作爲 自動裝配候選爲此依賴。依賴註解:{} org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) 有機springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1139) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042) org.springframework.beans.factory.support。 AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476 ) org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:303) org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) org.springframework.beans .factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons (DefaultListableBeanFactory.java:755) org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) org.springframework.context.support.AbstractApplicationContext.refresh(Abst ractApplicationContext.java:480) org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:629) org.springframework.web .servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:677) org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:548) org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489 ) org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) javax.servlet.GenericServlet.init(GenericServlet.java:158) org.apache.catalina.authenticator.Authenticato rBase.invoke(AuthenticatorBase.java:502) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) 組織.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol的.java:668) org.apache.coyote.http11.Http11NioProtocol $ Http11ConnectionHandler.process(Http11NioProtocol.java:223) org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1517) org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1 474) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617) org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread。的java:61) java.lang.Thread.run(Thread.java:745)根源

org.springframework.beans.factory.NoSuchBeanDefinitionException:無類型[com.sci.pascal.core的 排位豆。 services.AccountService] 找到依賴關係:預計至少有1個bean符合 autowire這個依賴項的候選項。依賴註解:{} org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301) org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047) 有機springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813) org.springframework.beans.factory.support。 ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) org.spri ngframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1139) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042) org.springframework.beans.factory.support。 AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java :303) org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) org.springframework.beans.fact org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(AbstractBeanFactory.java:299) DefaultListableBeanFactory.java:755) org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) org.springframework.web .servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:629) org.s pringframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:677) org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:548) org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet的。的java:489) org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) javax.servlet.GenericServlet.init(GenericServlet.java:158) org.apache.catalina.authenticator.AuthenticatorBase.invoke (AuthenticatorBase.java:502) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) org.apache。 catalina.connector.CoyoteAdapter.service (CoyoteAdapter.java:518) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:668) org.apache。 coyote.http11.Http11NioProtocol $ Http11ConnectionHandler.process(Http11NioProtocol。java:223) org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1517) org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1474) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617) org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable .run(TaskThread.java:61) java.lang.Thread.run(Thread.java:745)note注意在Apache Tomcat/8.0.21日誌中提供了根本原因的完整堆棧跟蹤 。

任何幫助表示讚賞。 謝謝。

+0

「沒有[com.sci.pascal.core.services.AccountService]類型的合格bean」 - 因此您需要將AccountService標記爲Bean,因爲它目前不是Bean。此外,您還必須添加一個XML文件。 – smoggers

+0

AccountService是一個接口,我在AccountServiceImplemetation類中實現。我還在這個類上使用了服務註釋,以便Spring將它識別爲一個bean。但我仍然得到這個錯誤 - 找不到符合依賴關係的[com.sci.pascal.core.services.AccountService]類型的合格bean:期望至少1個符合自動裝配候選符合條件的bean。 – mayorsanmayor

回答

0

將此屬性設置爲您的spring-servlet.xml文件。

<context:component-scan base-package="base package name" /> 
+0

我已經做到了。 mayorsanmayor

+0

我也有大約10個控制器類,我必須在使用它之前將每個bean聲明爲xml嗎? – mayorsanmayor

+0

不,不需要在XML中編寫bean,你可以使用@controller註釋來告訴編譯器這是控制器類 –

0

感謝大家,我發現錯誤春沒有找到配置文件 我在web.xml文件中包含

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

相關問題