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