2012-10-08 84 views
2

我在自動裝配和DI方面遇到了一些問題,所以我希望有人可以幫忙,因爲我現在已經堅持了幾天。春天 - 如何autowire數據源?

這是代碼:

@Service 
public class TicketsController implements Controller { 
    private TicketManager ticketManager; 

    @Autowired 
public void setTicketManager(TicketManager ticketManager) { 
    this.ticketManager = ticketManager; 
} 
... 
} 


@Service 
public class SimpleTicketManager implements TicketManager { 
    private TicketsDao ticketsDao; 

@Autowired 
public void setTicketsDao(TicketsDao ticketsDao) { 
    this.ticketsDao = ticketsDao; 
} 
... 
} 

@Repository 
public class JdbcTicketDao implements TicketsDao { 
    private DataSource dataSource; 
    @Autowired 
    public void setDataSource(DataSource dataSource) { 
    this.dataSource=dataSource; 
     this.jdbcTemplate = new JdbcTemplate(this.dataSource); 
    } 
... 
} 

public final class AppContext { 
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 
BeanFactory factory = context; 
TicketsController ticketsController = (TicketsController) factory.getBean("ticketsController"); 
} 
... 
} 

在我的beans.xml我有:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClass" value="com.mysql.jdbc.Driver"/> 
    <property name="url" value="jdbc:mysql://localhost:3306/mytckdb"/> 
    <property name="username" value="user"/> 
    <property name="password" value="pass"/> 
</bean> 
<context:component-scan base-package="bp.dao" /> 
<context:component-scan base-package="bp.mvc" /> 
<context:component-scan base-package="bp.svc" /> 
<context:component-scan base-package="bp.view" /> 

這不工作,我得到:

Error creating bean with name 'jdbcTicketDao': Injection of autowired dependencies failed 
... nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
    No matching bean of type [javax.sql.DataSource] found for dependency.` 

燦有人請幫忙嗎?我究竟做錯了什麼?看起來,自動裝配工作直到下一步注入dataSource時失敗。

編輯:我在玩代碼,並忘記setDataSource()之前的@Autowire,但它應該在那裏。

+1

錯誤消息似乎表明你想'@ Autowire'但顯示的代碼表示相反。你能澄清一下嗎? – 2012-10-08 17:48:58

+0

對不起,忘了在setDataSource之前添加@Autowired,但它在那裏,我有同樣的問題。 – newman555p

+0

我瘋狂的猜測是'beans.xml'在Spring中沒有被看作applicationContext。如果你可以提供更多關於你如何「啓動」的細節spring – 2012-10-08 17:54:02

回答

0

嘗試org.apache.commons.dbcp.BasicDataSource

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver" 
     p:url="jdbc:mysql://127.0.0.1:3306/mytckdb?autoReconnect=true" 
     p:username="user" p:password="pass" /> 

我使用JPA所以一般喜歡創建的EntityManagerFactory和使用

<bean id="entityManagerFactory" 
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
      <property name="dataSource" ref="dataSource" /> 
      <property name="persistenceUnitName" value="PU" /> 
      <property name="jpaVendorAdapter"> 
       <bean id="jpaAdapter" 
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
        <property name="database" value="${database}" /> 
        <property name="showSql" value="true" /> 
        <property name="generateDdl" value="false" /> 
       </bean> 
      </property> 
     </bean> 

<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 

    <tx:annotation-driven transaction-manager="txManager" /> 
+0

我得到同樣的錯誤。我已經試過這個dataSource bean,因爲我在搜索這個問題的解決方案時找到了它。我不能使這項工作:( – newman555p

+0

順便說一句,我只是檢查未下過@Autowired _private數據源數據源; _任何原因 – Anshu

+0

編輯:)它的存在在我的代碼... – newman555p

2

也許你錯過了佈線結構,儘量

<context:annotation-config/>

+0

補充,但我STIL得到同樣的錯誤:( – newman555p

0

看起來像你正在使用Spring 2.0,但我認爲context:component-scan是在Spring 2.5中引入的。 也許更新spring xml-config和spring依賴關係到2.5

+0

我也沒有運氣。我放棄了:) – newman555p

0

變化

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClass" value="com.mysql.jdbc.Driver"/> 
    <property name="url" value="jdbc:mysql://localhost:3306/mytckdb"/> 
    <property name="username" value="user"/> 
    <property name="password" value="pass"/> 
</bean> 

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
    <property name="url" value="jdbc:mysql://localhost:3306/mytckdb"/> 
    <property name="username" value="user"/> 
    <property name="password" value="pass"/> 
</bean> 

的屬性被稱爲driverClassName,不driverClass

而且,你不需要多context:component-scan元素可以更改

<context:component-scan base-package="bp.dao" /> 
<context:component-scan base-package="bp.mvc" /> 
<context:component-scan base-package="bp.svc" /> 
<context:component-scan base-package="bp.view" /> 

<context:component-scan base-package="bp.dao,bp.mvc,bp.svc,bp.view" /> 
0

這是由於bean實例創建的順序。在創建dataSource實例之前,您的DAO已經被實例化。

讓您的數據源bean定義之前

另一種方式是,在一個單獨的XML定義你的數據源定義,並導入之前