首先,这是我第一次尝试春天。使用spring的JDBC模板连接到数据库时遇到问题
这是我的AddEmployee Servlet代码:
package com.lftechnology.spring.jdbctemplate.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lftechnology.spring.jdbctemplate.model.dao.EmployeeDAO;
import com.lftechnology.spring.jdbctemplate.model.dao.EmployeeDAOImpl;
import com.lftechnology.spring.jdbctemplate.model.dao.mysql.EmployeeMySql;
import com.lftechnology.spring.jdbctemplate.model.dto.Employee;
@WebServlet("/AddEmployee")
public class AddEmployee extends HttpServlet
{
private static final long serialVersionUID = 1L;
Employee employee;
public AddEmployee() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
RequestDispatcher dispatcher=request.getRequestDispatcher("addemployee.jsp");
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
EmployeeDAO employeeDAO= (EmployeeDAO) context.getBean("employeeDAO");
employee=new Employee();
String name=(String) request.getAttribute("name");
String designation=(String) request.getAttribute("designation");
employee.setName(name);
employee.setDesignation(designation);
employeeDAO.insert(employee);
RequestDispatcher dispatcher =request.getRequestDispatcher("success.jsp");
dispatcher.forward(request,response);
}
}
用于JDBC连接的名为EmployeeMySql的对应类是
package com.lftechnology.spring.jdbctemplate.model.dao.mysql;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import com.lftechnology.spring.jdbctemplate.model.dao.EmployeeDAO;
import com.lftechnology.spring.jdbctemplate.model.dto.Employee;
public class EmployeeMySql extends JdbcTemplate implements EmployeeDAO
{
private DataSource dataSource;
public void setDataSource(DataSource dataSource)
{
System.out.println("Datasource here"+dataSource.toString());
try {
System.out.println("Datasource here"+dataSource.getLoginTimeout());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.dataSource = dataSource;
}
public void insert(Employee employee)
{
String query_sql;
query_sql="INSERT INTO EMPLOYEE (name,designation) VALUES (?,?,?)";
update(query_sql,new Object[]{employee.getName(),employee.getDesignation()});
}
@Override
public Employee retrieveAllData() {
// TODO Auto-generated method stub
return null;
}
}
类似地,我的spring bean配置是spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="dataSourcee"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/spring" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="employeeDAO" class="com.lftechnology.spring.jdbctemplate.model.dao.mysql.EmployeeMySql">
<property name="dataSource" ref="dataSourcee"/>
</bean>
<!-- import resource="Spring-Datasource.xml" />
<import resource="Spring-Customer.xml" /-->
</beans>
然而,我总是最终得到一个我根本无法弄清楚的错误。我得到的错误是
HTTP Status 500 - Error creating bean with name 'employeeDAO' defined in class path resource [spring.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required
type Exception report
message Error creating bean with name 'employeeDAO' defined in class path resource [spring.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeDAO' defined in class path resource [spring.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:605)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
com.lftechnology.spring.jdbctemplate.controller.AddEmployee.doPost(AddEmployee.java:46)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause
java.lang.IllegalArgumentException: Property 'dataSource' is required
org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSet(JdbcAccessor.java:134)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:605)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
com.lftechnology.spring.jdbctemplate.controller.AddEmployee.doPost(AddEmployee.java:46)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
有人能告诉我错误是什么吗? 非常感谢帮助。
答案 0 :(得分:2)
在setDataSource方法中,调用super.setDataSource。您有自己的dataSource变量,但是,基类也使用给定的dataSource实例进行一些初始化,这就是错过的。