我通常在自動接線和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中,我有:
這不起作用,我得到:
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,但應該在那兒.