2015-04-08 47 views
0

當我嘗試使用@Autowired這讓我異常@Autowired領域引起org.springframework.beans.factory.BeanCreationException

事我已經加入到使用@Autowired註解

  • 背景:註釋-config/

  • 豆ID = 「GoAnalyserService」 類= 「com.dataanalyser.serviceimpl.GoAnalyserServiceImpl」/

  • 上下文:組分掃描基包= 「com.dataanalyser.service」/

彈簧調度員的servlet

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

    <context:component-scan base-package="com.dataanalyser.controller"/> 
    <!-- <context:component-scan base-package="com.dataanalyser"/> --> 
    <context:component-scan base-package="com.dataanalyser.dao"/> 
    <context:component-scan base-package="com.dataanalyser.service"/> 
    <mvc:annotation-driven/> 

    <mvc:default-servlet-handler/> 
    <context:annotation-config /> 

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

    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="org.postgresql.Driver" /> 
     <property name="url" value="jdbc:postgresql://localhost:5432/GoAnalyserDB" /> 
     <property name="username" value="postgres" /> 
     <property name="password" value="toor" /> 
    </bean> 

    <bean id = "goAnalyserService" class = "com.dataanalyser.serviceimpl.GoAnalyserServiceImpl"/> 
    <bean id = "userDao" class = "com.dataanalyser.daoimpl.UserDaoImpl"/> 
</beans> 

控制器類

@Controller 
public class GoAnalyserControl { 

    public GoAnalyserControl() 
    { 
     System.out.println("------- Inside the controller -------"); 
    } 

    @Autowired 
    GoAnalyserService goAnalyserService; 

    @RequestMapping(value = "/logInChecker", method = RequestMethod.POST, consumes = {"application/json"}) 
    public @ResponseBody String logInCheckerFn(@RequestBody UserLogData userLogData){ 
     System.out.println("inside loginChecker()"); 
     //GoAnalyserService goAnalyserService = new GoAnalyserServiceImpl(); 
     Integer userAuthFlag = goAnalyserService.checkUserAuth(userLogData); 
     System.out.println(userAuthFlag.toString()); 
     return userAuthFlag.toString(); 
    } 
} 

服務類

public interface GoAnalyserService { 

    public Integer checkUserAuth(UserLogData userLogData); 

} 

我的服務工具階級

public class GoAnalyserServiceImpl implements GoAnalyserService { 

    @Autowired 
    UserDao userDao; 

    @Override 
    public Integer checkUserAuth(UserLogData userLogData){ 
     //UserDao userDao = new UserDaoImpl(); 
     if((userDao.getUserCount(userLogData.getUserName())) == 1){ 
      String dbPass = userDao.getUserPass(userLogData.getUserName()); 
      if(dbPass.equals(userLogData.getPassword())) 
       return 1; 
      else 
       return 2; 
     }else 
      return 0; 
    } 

} 

當我嘗試它沒有@Autowired註解,此代碼的工作了罰款,但是當我添加@Autowired它給了我

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'goAnalyserControl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.dataanalyser.service.GoAnalyserService com.dataanalyser.controller.GoAnalyserControl.goAnalyserService; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.dataanalyser.serviceimpl.GoAnalyserService] for bean with name 'GoAnalyserService' defined in ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.dataanalyser.serviceimpl.GoAnalyserService 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.dataanalyser.service.GoAnalyserService com.dataanalyser.controller.GoAnalyserControl.goAnalyserService; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.dataanalyser.serviceimpl.GoAnalyserService] for bean with name 'GoAnalyserService' defined in ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.dataanalyser.serviceimpl.GoAnalyserService 
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.dataanalyser.serviceimpl.GoAnalyserService] for bean with name 'GoAnalyserService' defined in ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.dataanalyser.serviceimpl.GoAnalyserService 

和幾個這樣的, 我認爲在spring-dispatcher-servlet.xml中有些問題,我查了很多,仍然無法找到錯誤的東西。我有任何需要添加到項目的jar文件....?

+0

有消息稱註解的:找不到類[com.dataanalyser.serviceimpl。 GoAnalyserService。我猜這個班不在那個包裏。 –

+0

如果您使用註釋,爲什麼不使用'@ Service'作爲您的服務類? – GriffeyDog

回答

0

嘗試從xml配置除去

<bean id = "goAnalyserService" class = "com.dataanalyser.serviceimpl.GoAnalyserServiceImpl"/> 

並將GoAnalyserServiceImpl註釋爲@Component。

0

首先,確保您在XML中聲明的類存在於包內。然後,用@Component註釋來註釋你的類。 Spring使用這些註釋來查找使用@Autowired時應該實例化的類。

1

把@Repo上GoAnalyserServiceImpl類

可能解決您的錯誤怎麼一回事,因爲當你使用@Autowired你不wory有關創建object.And使用@Autowired必須類@Controller or @Repo or @Service/only @Component.註釋的頂部使用在Spring 3中將該類標記爲控制器。

0

你的組件掃描聲明:

<context:component-scan base-package="com.dataanalyser.service"/> 

,而你的異常消息指出:

Cannot find class com.dataanalyser.serviceimpl.GoAnalyserService 

所以,你必須在你的組件掃描一個錯誤的包聲明。讓它:

<context:component-scan base-package="com.dataanalyser.serviceimpl"/> 

或類的包更改爲com.dataanalyser.service

假設,當然,你的服務類正確