2011-11-11 25 views
1

我會讓我的代碼講,第一,這裏是我的根context.xml中:的Spring bean構造 - 精氨酸與我一般

<context:component-scan base-package="it.trew.prove" /> 

<bean id="usersDao" class="it.trew.prove.model.dao.UsersDao" /> 

<bean id="usersService" class="it.trew.prove.server.services.UsersServiceImpl" /> 

我的一些用戶的DAO:

public class UsersDao extends ObjectifyDao<User> { 

    protected UsersDao(Class<User> clazz) { 
     super(User.class); 
    } 

    static { 
     ObjectifyService.register(User.class); 
    }  
} 

和我的用戶服務(執行):

public class UsersServiceImpl implements UsersService { 

    private final UsersDao usersDao; 

    @Autowired 
    public UsersServiceImpl(UsersDao usersDao) { 
     this.usersDao = usersDao; 
    } 

    @Override 
    public List<User> listUsers() { 
     return usersDao.list(); 
    } 

    @Override 
    public void saveUser(User user) { 
     usersDao.add(user); 
    } 
} 

現在我的記錄是:

AVVERTENZA:嵌套在 org.springframework.beans.factory.UnsatisfiedDependencyException: 錯誤創建名稱爲豆 'usersController' 文件 [定義/home/fabio/stsworkspace/TestGAE/target/TestGAE-1.0-SNAPSHOT/WEB -INF/classes/it/trew/prove/web/UsersController.class]:通過構造函數參數與 索引0表示的不完整依賴項[it.trew.prove.server.services.UsersService]的類型:錯誤 創建bean with名稱'usersService'在ServletContext中定義 資源[/WEB-INF/spring/root-context.xml]:不滿意的依賴關係 通過構造函數參數表示,索引0的類型爲 [it.trew.prove.model.dao.UsersDao] ::創建bean wi時出錯名稱 在ServletContext資源中定義的'usersDao' [/WEB-INF/spring/root-context.xml]:bean的實例化失敗; 嵌套的異常是 org.springframework.beans.BeanInstantiationException:無法 實例化bean類[it.trew.prove.model.dao.UsersDao]:無默認值 構造函數找到;嵌套異常是 java.security.PrivilegedActionException: java.lang.NoSuchMethodException: it.trew.prove.model.dao.UsersDao。();嵌套異常是 org.springframework.beans.factory.BeanCreationException:錯誤 在ServletContext資源 [/WEB-INF/spring/root-context.xml]中定義的名稱爲'usersDao'的Bean創建bean:Bean實例化失敗; 嵌套的例外是 org.springframework.beans.BeanInstantiationException:無法 實例化bean類[it.trew.prove.model.dao.UsersDao]:沒有默認的構造函數 發現;嵌套異常是 java.security.PrivilegedActionException: java.lang.NoSuchMethodException: it.trew.prove.model.dao.UsersDao。();嵌套的例外是 org.springframework.beans.factory.UnsatisfiedDependencyException: 誤差名ServletContext中定義的「usersService」創建豆 資源[/WEB-INF/spring/root-context.xml]:不滿意依賴 通過構造函數的參數表示索引爲0的 [it.trew.prove.model.dao.UsersDao]::在ServletContext資源 [/WEB-INF/spring/root-context.xml]中創建bean的名稱爲 'usersDao'的錯誤: bean的實例化失敗; 嵌套的異常是 org.springframework.beans.BeanInstantiationException:無法 實例化bean類[it.trew.prove.model.dao.UsersDao]:無默認值 構造函數找到;嵌套異常是 java.security.PrivilegedActionException: java.lang.NoSuchMethodException: it.trew.prove.model.dao.UsersDao。();嵌套的異常是 org.springframework.beans.factory。BeanCreationException:錯誤 創建在ServletContext資源中定義名爲'usersDao'的Bean [/WEB-INF/spring/root-context.xml]:Bean的實例化失敗; 嵌套的異常是 org.springframework.beans.BeanInstantiationException:無法 實例化bean類[it.trew.prove.model.dao.UsersDao]:無默認值 構造函數找到;嵌套的異常是 java.security.PrivilegedActionException: java.lang.NoSuchMethodException: it.trew.prove.model.dao.UsersDao(): java.lang.NoSuchMethodException: it.trew.prove.model.dao .UsersDao。()

因此...在您誠實的意見下,我該如何更改我的代碼才能使其正常工作?

  • 對不起,如此冗長:) -

回答

2

將構造函數參數添加到配置文件

<bean id="usersDao" class="it.trew.prove.model.dao.UsersDao"> 
    <constructor value="it.trew.prove.model.dao.User" /> 
</bean> 

或更好,從UserDao構造函數中刪除無用的參數!

併爲用戶服務bean聲明添加autowire="constructor"<bean id="usersService" class="it.trew.prove.server.services.UsersServiceImpl" autowire="constructor"/>

1

未發現默認的構造函數;嵌套的例外是java.security.PrivilegedActionException:java.lang.NoSuchMethodException:

只需添加默認的構造函數

0

爲了讓您的@Autowire註釋工作,你必須添加<context:annotation-config /> .Eg,

<?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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <context:annotation-config/> 

</beans> 
+1

來自文檔:元素:component-scan 掃描類註釋組件的類路徑, 將被自動註冊爲Spring bean。 ...檢測到 。注意:此標籤意味着 'annotation-config'標籤的效果,激活了\ @ Required, \ @Autowired ... –

相關問題