Spring:使用名称' edao'创建bean时出错在类路径资源[applicationContext.xml]

时间:2016-02-13 10:13:45

标签: spring

我是spring编程的新手。在执行一个从mysql数据库中获取某些记录的程序时,我遇到了这些错误:

log4j:WARN No appenders could be found for logger (org.springframework.beans.factory.xml.XmlBeanDefinitionReader).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'edao' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'jtemplate' while setting bean property 'jdbcTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jtemplate' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'ds' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ds' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.Driver]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at Test.main(Test.java:15)

我的项目中有三个文件。

1.EmpDAO.java:

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;



public class EmpDAO {

    private JdbcTemplate template;

    public JdbcTemplate getTemplate() {
        return template;
    }

    public void setTemplate(JdbcTemplate template) {
        this.template = template;
    } 

    public List<Student> getAllStudents(){  
         return (List<Student>) template.query("select * from test",new ResultSetExtractor(){  
            @Override  
             public List<Student> extractData(ResultSet rs) throws SQLException,  
                    DataAccessException {  

                List<Student> list=new ArrayList<Student>();  
                while(rs.next()){  
                Student e=new Student();    
                e.setName(rs.getString(1));  
                e.setRoll(rs.getString(2));  
                list.add(e);  
                }  
                return list;  
                }  
            });  
          }  


}

2.Student.java:

public class Student {
    private String name;
    private String roll;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getRoll() {
        return roll;
    }
    public void setRoll(String roll) {
        this.roll = roll;
    }
    public void display(){
        System.out.println("Name: "+name+" Roll="+roll);
    }

}

3.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:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  

<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
<property name="driverClassName" value="com.mysql.jdbc.Driver" />  
<property name="url" value="jdbc:mysql://localhost/test" />  
<property name="username" value="root" />  
<property name="password" value="password" />  
</bean>  

<bean id="jtemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">  
<constructor-arg ref="ds"></constructor-arg>  
</bean>  

<bean id="edao" class="EmpDAO">  
<property name="jdbcTemplate" ref="jtemplate"></property> 
</bean>  

</beans> 

4.Test.java(执行):

import java.util.List;

import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.ClassPathResource;  
import org.springframework.core.io.Resource; 
public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         Resource resource=new ClassPathResource("applicationContext.xml");  
            BeanFactory factory=new XmlBeanFactory(resource);  

            EmpDAO dao=(EmpDAO)factory.getBean("edao");  

            List<Student> list=dao.getAllStudents();  

            for(Student e:list)  
                System.out.println(e);  

            }  


    }

我参与库中包含的罐子:

These are the jars i included

请在这方面帮助我。普遍的一天,谢谢你提前。

1 个答案:

答案 0 :(得分:0)

错误消息的相关部分是:

java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.Driver]

您可以从此处下载该jar:5.1.38,或在central nexus repository上查找不同的版本。

注意:您可以更好地使用一些依赖关系管理系统,而不是手动执行此操作。我建议看看Maven。