views:

603

answers:

1

Well, I am having a problem with configuring Spring's JDBCTemplate to work properly. I am trying to inject the datasource but it seems that it's always null. Here is a sample code:

  1. applicationContext.xml:

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" />
    <property name="url"
        value="jdbc:derby:c:\\Derby\\MyDB\\jsfkickstart;create=true" />
    <property name="username" value="admin" />
    <property name="password" value="admin" />
    </bean>
    
    
    <bean id="employeeDoa" class="com.kickstart.employeeapp.doa.EmployeeDoa">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
  2. web.xml

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
    

    <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
            <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    
  3. EmployeeDoa.java

    public class EmployeeDoa implements IEmployeeDoa, IDepartmentDoa {
    private DataSource dataSource;
            public List<Employee> getAllEmployees() {
            JdbcTemplate select = new JdbcTemplate(dataSource);
            return select.query(
                    "SELECT "+
                        "e.ID as empId, "+
                        "e.FIRSTNAME as empFirstName, "+ 
                        "e.LASTNAME as empLastName,  "+
                        "d.ID as depId,  "+
                        "d.NAME as depName, "+
                        "d.LOCATION as depLocation "+
                    "FROM jsfkickstart.Employee e, jsfkickstart.Department d",
                    new EmployeeMapper());
            }
    }
    
    
    class EmployeeMapper implements RowMapper {
        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
            Employee emp = new Employee();
            emp.setId(rs.getInt("empId"));
            emp.setFirstName(rs.getString("empFirstName"));
            emp.setLastName(rs.getString("empLastName"));
            Department dep = new Department();
            dep.setId(rs.getShort("depId"));
            dep.setLocation(rs.getString("depLocation"));
            dep.setName(rs.getString("depName"));
            emp.setDepartment(dep);
            return emp;
        }
    }
    
  4. Exception Thrown:

    Exception in thread "main" java.lang.IllegalArgumentException: Property 'dataSource' is required
    at org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSet(JdbcAccessor.java:134)
    at org.springframework.jdbc.core.JdbcTemplate.<init>(JdbcTemplate.java:142)
    at com.kickstart.employeeapp.doa.EmployeeDoa.addEmployee(EmployeeDoa.java:29)
    

Does anybody know how can I resolve this problem? If I'm missing any important information in the question, please let me know.

Thank you.

A: 

I was actually a very stupid exception. The body of the setDataSource() was empty, i.e. I didn't assigned dataSource to anything so it was always null.

Stupid me!!!

fouad
I was going to say I didn't see a setDataSource() method at all.
Matt