gpt4 book ai didi

java.lang.IllegalArgumentException : 'dataSource' or 'jdbcTemplate' is required when starting tomcat 异常

转载 作者:行者123 更新时间:2023-11-28 22:04:56 26 4
gpt4 key购买 nike

想出一个关于 spring 和 tomcat 的问题。我有以下代码

BookDAOImpl.java

@Repository
public class BookDAOImpl extends NamedParameterJdbcDaoSupport implements BookDAO {

private class BookMapper implements RowMapper<Book> {

@Override
public Book mapRow(ResultSet resultSet, int rowNum) throws SQLException {
Book book = new Book();
book.setId(resultSet.getInt("id"));
book.setTitle(resultSet.getString("title"));
book.setAuthor(resultSet.getString("author"));
book.setPublisher(resultSet.getString("publisher"));
book.setPublicationDate(resultSet.getDate("publication_date"));
book.setIsbn(resultSet.getString("isbn"));
book.setAvailable(resultSet.getBoolean("available"));
return book;
}

}
}

库数据源.java

@Component("dataSource")
public class LibraryDataSource extends DriverManagerDataSource {

@Autowired
public LibraryDataSource(@Value("${url}")String url, @Value("${user}")String username, @Value("${password}")String password) {
super(url, username, password);
}
}

应用程序上下文.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
default-autowire="byName">

<context:annotation-config/>
<context:component-scan base-package="com.LibraryManagement.*" />
<context:property-placeholder location="classpath:prop.properties"/>
</beans>

现在据我所知, Autowiring 应该获取数据源并将其 Autowiring 到存储库,但是当我启动 tomcat 时,出现以下错误

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookDAOImpl' defined in file [P:\SourceControl\EclipseWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\WebLibraryManagemenet\WEB-INF\classes\com\LibraryManagement\access\impl\BookDAOImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)

知道发生了什么吗?

谢谢

最佳答案

将以下构造函数添加到 BookDAOImpl:

@Autowired 
public BookDAOImpl(DataSource dataSource) {
setDataSource(dataSource);
}

关于java.lang.IllegalArgumentException : 'dataSource' or 'jdbcTemplate' is required when starting tomcat 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23322231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com