spring - how to autowire data source?

17,034

Solution 1

Maybe you're missing wiring configuration, try

<context:annotation-config/>

Solution 2

This will be due to the order of bean instance creation. Your DAO has been instantiated before the dataSource instance created.

Keep your data Source bean definition before

other way is , define your dataSource definitions in a separate xml and import that before

Share:
17,034
newman555p
Author by

newman555p

Updated on June 04, 2022

Comments

  • newman555p
    newman555p almost 2 years

    I'm having some problem with autowire and DI in general, so I hope that someone can help cause I've been stuck for days now.

    This is the code:

    @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");
    }
    ...
    }
    

    In my beans.xml I've got:

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

    This doesn't work and I get:

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

    Can someone please help out with this? What am I doing wrong? It seems that autowiring is working all until the next step where it fails when injecting dataSource.

    EDIT: I was playing with the code, and forgot @Autowire before setDataSource() but it is supposed to be there.

  • newman555p
    newman555p over 11 years
    I get the same error. I already tried this dataSource bean cause I found it while searching for solution to this problem. I just can't make this work :(
  • Anshu
    Anshu over 11 years
    btw, I just checked you didn't place @Autowired over private DataSource dataSource;. Any reasons?
  • Anshu
    Anshu over 11 years
    You can also try defining bean for JdbcTicketDao in your beans.xml