Spring - 如何自动装配数据源?

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

请问有人可以帮忙解决这个问题吗?我做错了什么?似乎自动装配在注入dataSource时出了问题。

编辑: 我在调试代码时做了一些修改,忘记在setDataSource()之前加上@Autowire,但实际上应该要有这个注解。


1
错误信息似乎表明您正在尝试使用@Autowire,但所显示的代码表明情况并非如此。您能否澄清一下? - user180100
抱歉,我忘记在setDataSource之前添加@Autowired注释,但实际上已经添加了,而且我遇到了同样的问题。 - newman555p
我个人的猜测是beans.xml没有被Spring视为应用上下文。 如果你能提供更多的关于如何“启动”Spring的细节 - user180100
我最初尝试使用上下文监听器和appconfig.xml,但都没有成功。我认为只需要在AppContext中使用getBean()方法就足够了,难道不是这样吗? - newman555p
抱歉,但是“展示您的代码”。 - user180100
5个回答

2
也许你缺少了线路配置,请尝试使用 <context:annotation-config/>

1
由于bean实例创建的顺序,这可能是由于DAO在dataSource实例创建之前被实例化。
将数据源bean定义放在前面。
另一种方法是,在单独的XML文件中定义您的dataSource定义,并在之前导入该文件。

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

我得到了相同的错误。我已经尝试过这个dataSource bean,因为我在寻找解决这个问题的方法时找到了它。但我就是无法让它工作 :( - newman555p
顺便说一句,我刚刚检查了一下,你没有在“_private DataSource dataSource;_”上放置“@Autowired”。有什么原因吗? - Anshu
你也可以尝试在beans.xml中定义_JdbcTicketDao_的bean。 - Anshu

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
看起来你正在使用 Spring 2.0,但我认为 context:component-scan 是在 Spring 2.5 中引入的。 也许更新 Spring xml-config 和 Spring 依赖到 2.5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接