gpt4 book ai didi

java - SpringMvc - java.lang.NoSuchMethodException 异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:48 26 4
gpt4 key购买 nike

在我的代码下尝试设置我正在使用 Spring 3.0 的数据源时出现以下错误:

错误

520  [ContainerBackgroundProcessor[StandardEngine[Catalina]]] ERROR org.springframework.web.context.ContextLoader  - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'countryManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'countryDao' while setting bean property 'countryDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'countryDao' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.crimetrack.jdbc.JdbcCountryDAO]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.crimetrack.jdbc.JdbcCountryDAO.<init>()

JdbcCountryDAO.java

@Repository
public class JdbcCountryDAO extends JdbcDaoSupport implements CountryDAO{

private final Logger logger = Logger.getLogger(getClass());


@Autowired
JdbcCountryDAO(DataSource dataSource){
setDataSource(dataSource);
}

public List<Country> getCountryList() {
int countryId = 6;
String countryCode = "AI";
logger.debug("In getCountryList()");
String sql = "SELECT * FROM TBLCOUNTRY WHERE countryId = ? AND countryCode = ?";
logger.debug("Executing getCountryList String "+sql);

Object[] parameters = new Object[] {countryId, countryCode};

logger.info(sql);

//List<Country> countryList = getJdbcTemplate().query(sql,new CountryMapper());

List<Country> countryList = getJdbcTemplate().query(sql, parameters,new CountryMapper());
return countryList;
}

applicationContext.xml

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

<bean id="countryManager" class="com.crimetrack.service.CountryManager">
<property name="countryDao" ref="countryDao"/>
</bean>
<bean id="countryDao" class="com.crimetrack.jdbc.JdbcCountryDAO">
<property name="dataSource" ref="dataSource"/>
</bean>

最佳答案

你需要这样做:

<bean id="countryDao" class="com.crimetrack.jdbc.JdbcCountryDAO">
<constructor-arg index="0" ref="dataSource"/>
</bean>

dataSource 不是JdbcCountryDAO 类的属性,它是构造函数参数。 Spring 告诉你:No default constructor found,因为这不是默认构造函数:

@Autowired
JdbcCountryDAO(DataSource dataSource){
setDataSource(dataSource);
}

它有一个 DataSource 作为参数。

关于java - SpringMvc - java.lang.NoSuchMethodException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12674061/

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