2013-06-23 37 views
0

我的dao實現;spring dataSource config。與註釋

\\here are imports... 
@Repository 
public class CompanyDaoImp extends JdbcDaoSupport implements CompanyDao { 


private static final String INSERTCOMPANY = "INSERT INTO b_company" 
     + "(NAME)VALUES(?)"; 

這是我的豆;

<?xml version="......... 

<!-- TODO add the component-scan and annotation-config elements --> 

<context:annotation-config/> 
<context:component-scan base-package="com.some.company"/> 



<bean id="companyAppDataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
<property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
<property name="url" value="jdbc:mysql://localhost:3306/companyapp"/> 
<property name="username" value="root"/> 
<property name="password" value="root"/> 
</bean> 
</beans> 

我不明白爲什麼我一直採取例外; .......調用init方法失敗;嵌套異常是java.lang.IllegalArgumentException:'dataSource'或'jdbcTemplate'是必需的.........

回答

0

JdbcDaoSupport沒有任何註釋來自動注入DataSource。你必須重寫setDataSource方法是這樣的:因爲你可能有多個數據源

<bean name="companyDaoImp" class="...CompanyDaoImp"> 
    <property name="dataSource" ref="companyAppDataSource" /> 
</bean> 

春天不能自動注入DataSource,並在這種情況下,有必要告訴你春天需要哪一個。

+0

我認爲setDataSource是最終的,所以我不能重寫它。第二個sol。工作,但有沒有什麼辦法可以做到這一點只有註釋(數據源bean除外) – cek

+0

@cek你說得對。我沒有注意到。我在答案中刪除了這個。抱歉。 – LaurentG

+0

沒問題。你認爲如果我在定義數據源bean時犯了錯誤嗎? – cek