2012-10-01 150 views
1

我收到以下錯誤:春天-MVC - JdbcDaoSupport錯誤

[C:\Users\Darin\apache-tomcat-6.0\webapps\crimeTrack\WEB-INF\classes\com\crimetrack\jdbc\JdbcCountryDAO.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:1455) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) 
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631) 
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588) 
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645) 
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508) 
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449) 
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133) 
    at javax.servlet.GenericServlet.init(GenericServlet.java:212) 
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1206) 
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1026) 
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4421) 
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4734) 
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799) 
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779) 
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601) 
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:943) 
    at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:778) 
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:504) 
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1385) 
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:306) 
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142) 
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1389) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1653) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1662) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1642) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required 

jdbcDAOCountry.java

@Repository 
public class JdbcCountryDAO extends JdbcDaoSupport implements CountryDAO{ 

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

    @Autowired 
    private JdbcTemplate jdbcTemplate; 

    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; 
    } 



    public void saveCountry(Country country) { 
     logger.debug("In saveCountry"); 

     String sql= "INSERT INTO crimetrack.tblcountry (countryName, countryCode) " + 
        "VALUES (:countryName, :countryCode)"; 

     logger.debug("Executing saveCountry String " + sql); 
     int count = getJdbcTemplate().update(sql,new MapSqlParameterSource() 

       .addValue("countryName", country.getCountryName()) 
       .addValue("countryCode", country.getCountryCode())); 
     logger.debug(count +" Rows affected in tblCountry"); 


    } 

    public void updateCountry(Country country) { 
     logger.debug("In updateCountry"); 

     String sql= "UPDATE crimetrack.tblcountry SET countryName = :countryName, "+ 
        "countryCode = :countryCode WHERE countryId = :countryId"; 

     logger.debug("Executing updateCountry String " + sql); 
     int count = getJdbcTemplate().update(sql,new MapSqlParameterSource() 
       .addValue("countryId",country.getCountryId()) 
       .addValue("countryName", country.getCountryName()) 
       .addValue("countryCode", country.getCountryCode())); 
     logger.debug(count +" Rows affected in tblCountry"); 

    } 

    public static class CountryMapper implements ParameterizedRowMapper<Country>{ 

     public Country mapRow(ResultSet rs, int rowNum) throws SQLException { 
       Country country = new Country(); 
       country.setCountryCode(rs.getString("countryCode")); 
       country.setCountryId(rs.getInt("countryId")); 
       country.setCountryName(rs.getString("countryName")); 
       return country; 
      }  
    } 

} 

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 

     xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 



<!-- __________________________________________________________________________________________________ -->    

    <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="authenticationManager" class="com.crimetrack.service.AuthenticationManager"> 
     <property name="loginDao" ref="loginDao" /> 
    </bean>  
    <bean id="loginDao" class="com.crimetrack.jdbc.JdbcLoginDAO"> 
     <property name="dataSource" ref="dataSource" /> 
    </bean> 
<!-- __________________________________________________________________________________________________ --> 

    <bean id="divisionManager" class="com.crimetrack.service.DivisionManager"> 
     <property name="divisionDao" ref="divisionDao"/> 
    </bean>  
    <bean id="divisionDao" class="com.crimetrack.jdbc.JdbcDivisionDAO"> 
     <property name="dataSource" ref="dataSource"/> 
    </bean> 

<!-- __________________________________________________________________________________________________ -->  

    <bean id="positionManager" class="com.crimetrack.service.PositionManager"> 
     <property name="positionDao" ref="positionDao"/> 
    </bean>  
    <bean id="positionDao" class="com.crimetrack.jdbc.JdbcPositionDAO"> 
     <property name="dataSource" ref="dataSource" /> 
    </bean>  

<!-- __________________________________________________________________________________________________ -->  

    <bean id="genderManager" class="com.crimetrack.service.GenderManager"> 
     <property name="genderDao" ref="genderDao"/> 
    </bean> 

    <bean id="genderDao" class="com.crimetrack.jdbc.JdbcGenderDAO" > 
     <property name="dataSource" ref="dataSource" /> 
    </bean> 

<!-- __________________________________________________________________________________________________ --> 

    <bean id="officerRegistrationValidation" class="com.crimetrack.service.OfficerRegistrationValidation"> 

      <property name="validateUserNameManager" ref="validateUserNameManager"/> 


    </bean> 

    <bean id="validateUserNameManager" class="com.crimetrack.service.ValidateUserNameManager"> 
      <property name="officerDao" ref="officerDao"/> 

    </bean> 


    <bean id="officerDao" class="com.crimetrack.jdbc.JdbcOfficersDAO" > 
      <property name="dataSource" ref="dataSource" /> 
    </bean> 


<!-- __________________________________________________________________________________________________ --> 

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
     <property name="dataSource"><ref bean="dataSource" /></property> 
    </bean> 

    <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="propertyConfigurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 
       <value>classpath:jdbc.properties</value> 
      </list> 
     </property> 
    </bean> 
<!-- __________________________________________________________________________________________________ -->  

    <bean id="transactionManager" 
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
     <property name="dataSource" ref="dataSource"/> 
    </bean> 





    </beans> 

的servlet。 X毫升

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 

     xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd 
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
          http://www.springframework.org/schema/beans/spring-context-3.0.xsd"> 


<!-- __________________________________________________________________________________________________ -->  

    <!-- Supports annotations and allows the use of @Controller, @Required, @RequestMapping --> 
    <context:annotation-config/>  

    <context:component-scan base-package="com.crimetrack.jdbc"/> 
    <context:component-scan base-package="com.crimetrack.service"/> 
    <context:component-scan base-package="com.crimetrack.web" /> 

    <mvc:annotation-driven /> 

回答

5

確保applicationContext.xml具有定義的bean'dataSource'。由於dataSource是一個構造函數arg,DAO應該傳遞一個構造函數arg而不是一個屬性。註釋一個構造函數方法並確保它接受一個dataSource。數據源通是同一個在applicationContext.xml定義:

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

@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() {....................... 
+0

謝謝,經過幾個小時的谷歌搜索後,它解決了這個問題。 – Davor

+0

很高興知道這對你有幫助:) – devdar

1

的錯誤是,沒有數據源或JdbcTemplate的是有線到JdbcCountryDao豆:

java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required

但是你的XML配置說明您的數據源發送的,所以你確定這個錯誤在使用文件的發佈版本時發生?

此外,沒有必要聲明@Autowired jdbcTemplate在繼承JdbcDaoSupport的類中,因爲超類已經爲您提供了這個功能。

+0

這是一個「讓」 – devdar

+0

我是否需要創建一個DataSource類,然後瓶坯@Autowired – devdar

+0

刪除'@Autowired私人的JdbcTemplate JdbcTemplate的發生;'從JdbcCountryDao。 –