3

一般来说,我在使用 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,但它应该在那里。

4

5 回答 5

2

也许您缺少接线配置,请尝试

<context:annotation-config/>

于 2012-10-08T18:32:36.757 回答
1

这将归因于 bean 实例创建的顺序。您的 DAO 在创建 dataSource 实例之前已被实例化。

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

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

于 2015-06-20T09:45:04.440 回答
0

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

于 2012-10-08T18:42:42.437 回答
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" />
于 2012-10-09T08:07:25.557 回答
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" />
于 2012-10-08T17:45:48.347 回答