【问题标题】:spring - how to autowire data source?spring - 如何自动装配数据源?
【发布时间】:2012-10-08 17:40:10
【问题描述】:

我在使用 autowire 和 DI 时遇到了一些问题,所以我希望有人能提供帮助,因为我已经卡了好几天了。

这是代码:

@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.` 

有人可以帮忙吗?我究竟做错了什么?似乎自动装配一直在工作,直到注入数据源时失败的下一步。

编辑:我在玩代码,在 setDataSource() 之前忘记了@Autowire,但它应该在那里。

【问题讨论】:

  • 错误消息似乎表明您正在尝试@Autowire,但显示的代码另有说明。你能澄清一下吗?
  • 抱歉,忘记在 setDataSource 之前添加 @Autowired,但它在那里,我遇到了同样的问题。
  • 我的猜测是 beans.xml 不会被 spring 视为 applicationContext。如果您可以提供有关如何“启动”弹簧的更多详细信息
  • 好吧,我首先使用上下文侦听器和 appconfig.xml 尝试了所有这些,但没有成功。我认为使用 getBean() 和 AppContext 中的代码就足够了。不是吗??
  • 抱歉,请“向我们展示代码”。

标签: java spring datasource dao autowired


【解决方案1】:

也许你缺少接线配置,试试

&lt;context:annotation-config/&gt;

【讨论】:

    【解决方案2】:

    这将取决于 bean 实例创建的顺序。在创建数据源实例之前,您的 DAO 已被实例化。

    保留您的数据源 bean 定义之前

    另一种方法是,在单独的 xml 中定义您的数据源定义并在之前导入它

    【讨论】:

      【解决方案3】:

      试试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" />
      

      【讨论】:

      • 我得到了同样的错误。我已经尝试过这个 dataSource bean,因为我在寻找这个问题的解决方案时找到了它。我就是做不到这个:(
      • 顺便说一句,我刚刚检查过您没有将@Autowired 放置在 private DataSource dataSource; 上。有什么原因吗?
      • 您也可以尝试在 beans.xml 中为 JdbcTicketDao 定义 bean
      【解决方案4】:

      看起来您正在使用Spring 2.0,但我认为context:component-scan 是在Spring 2.5 中引入的。 也许将 spring xml-config 和 spring 依赖项更新为2.5

      【讨论】:

        【解决方案5】:

        改变

        <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" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-04-14
          • 1970-01-01
          • 2020-06-17
          • 2013-07-23
          • 2018-11-01
          • 2015-02-26
          • 2017-06-04
          • 2020-09-25
          相关资源
          最近更新 更多