spring - how to autowire data source?

2019-08-01 06:24发布

我有一些问题,一般的自动装配和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但它应该在那里。

Answer 1:

也许你错过了布线的配置,可

<context:annotation-config/>



Answer 2:

这是由于bean实例创建的顺序。 您的DAO已经创建的数据源实例之前被实例化。

让您的数据源bean定义之前

另一种方法是,在一个单独的XML定义你的数据源定义,并导入之前



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


Answer 4:

看起来你正在使用Spring 2.0 ,但我认为context:component-scan年推出的Spring 2.5 。 也许春季更新XML的配置和Spring的依赖到2.5



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


文章来源: spring - how to autowire data source?