4

I am getting the following error when trying to set the dataSource i am using Spring 3.0 under is my code:

Error

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>

2 Answers 2

4

You need to do this:

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

The dataSource is not a property of JdbcCountryDAO class, it is a constructor argument. Spring says to you: No default constructor found, because this is not the default constructor:

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

It has a DataSource as an argument.

Sign up to request clarification or add additional context in comments.

1 Comment

Paulius Matulionis is absolutely right here. You should update your applicationContext.xml configuration by introducing constructor-arg for data source reference.
0

I think the reason for the error moght be simply that the scope of constructor of your JdbcCountryDAO is default scope, you need to set it to public.

1 Comment

Constructor annotated with @Autowired annotation does not have to be public. static.springsource.org/spring/docs/3.0.x/javadoc-api/org/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.