我正在学习spring,在通过spring查询Java对象的时候出了些问题,自己检查了很久,还是没有解决错误!
希望各位大牛,能够帮助一下解决这一问题,感谢!!我使用MyEclipse做的,出现以下报错:
log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'litQuery' defined in class path resource [cn/edu/scnu/spring/dao/litInformationDao/beans.xml]: Cannot resolve reference to bean 'litQueryById' while setting bean property 'litQueryById'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'litQueryById' defined in class path resource [cn/edu/scnu/spring/dao/litInformationDao/beans.xml]: Cannot resolve reference to bean 'dataSource' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [cn/edu/scnu/spring/dao/litInformationDao/beans.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 

main类package cn.edu.scnu.spring.dao.litInformationDao;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;import cn.edu.scnu.beans.LitInformation;public class QueryMain { public static void main(String[] args) {

ClassPathResource resource = new ClassPathResource("cn/edu/scnu/spring/dao/litInformationDao/beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);
        
LitInformationDao litQuery= (LitInformationDao)factory.getBean("litQuery");
LitInformation lit = (LitInformation)litQuery.getLitInformationById(1);
System.out.println("返回的Java对象Job的具体信息为:");
System.out.println("id: "+lit.getId());
System.out.println("author: "+lit.getAuthor());
}
}
LitInformationDao的代码
package cn.edu.scnu.spring.dao.litInformationDao;
import cn.edu.scnu.beans.LitInformation;public class LitInformationDao {

private LitQueryByIdMapping  litQueryById;

public LitInformation getLitInformationById(long id){
Object[] params=new Object[]{id};
LitInformation literature=(LitInformation) litQueryById.execute(params).get(0);
return literature;
}

public void setLitQueryById(LitQueryByIdMapping litQueryById) {
this.litQueryById = litQueryById;
}
public LitQueryByIdMapping getLitQueryById() {
return litQueryById;
}
}
LitQueryByIdMapping代码package cn.edu.scnu.spring.dao.litInformationDao;import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;import javax.sql.DataSource;import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.MappingSqlQuery;import cn.edu.scnu.beans.LitInformation;public class LitQueryByIdMapping extends MappingSqlQuery{  public LitQueryByIdMapping(DataSource ds)
    {
     super(ds,"select * from lit_information where id=?");
     declareParameter(new SqlParameter("id",Types.INTEGER));
     compile();
    }

@Override
protected Object mapRow(ResultSet rs, int index) throws SQLException {
LitInformation literature=new LitInformation();

literature.setId(rs.getLong("id"));
literature.setTitle(rs.getString("title"));
literature.setKeyword(rs.getString("keyword"));
literature.setAuthor(rs.getString("author"));
literature.setCatagory_id(rs.getLong("catagory_id"));
literature.setJointime(rs.getString("jointime"));
literature.setPublication(rs.getString("publication"));
literature.setPubtime(rs.getString("pubtime"));
literature.setRe(rs.getString("re"));
literature.setSearchcount(rs.getLong("searchcount"));
literature.setSummary(rs.getString("summary"));

return literature;
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- dataSource配置开始 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/literature
</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
<!-- dataSource配置结束 --> <!-- LitQueryById配置开始 -->
<bean id="litQueryById"
class="cn.edu.scnu.spring.dao.litInformationDao.LitQueryByIdMapping">
<constructor-arg>
<ref bean="dataSource" />
</constructor-arg>
</bean>
<!-- LitQueryById配置结束 --> <!-- LitInformationDao配置开始 -->
<bean id="litQuery"
class="cn.edu.scnu.spring.dao.litInformationDao.LitInformationDao">
<property name="litQueryById">
<ref bean="litQueryById" />
</property>
</bean>
<!-- LitInformationDao配置结束 --></beans>
LitInformation bean代码package cn.edu.scnu.beans;
public class LitInformation { private long id;
private long catagory_id;
private String title;
private String author;
private String keyword;
private String publication;
private String pubtime;
private String summary;
private String re;
private long searchcount;
private String jointime;
private String path;

public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public long getCatagory_id() {
return catagory_id;
}
public void setCatagory_id(long catagory_id) {
this.catagory_id = catagory_id;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getJointime() {
return jointime;
}
public void setJointime(String jointime) {
this.jointime = jointime;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getPublication() {
return publication;
}
public void setPublication(String publication) {
this.publication = publication;
}
public String getPubtime() {
return pubtime;
}
public void setPubtime(String pubtime) {
this.pubtime = pubtime;
}
public String getRe() {
return re;
}
public void setRe(String re) {
this.re = re;
}
public long getSearchcount() {
return searchcount;
}
public void setSearchcount(long searchcount) {
this.searchcount = searchcount;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}

}

解决方案 »

  1.   

    Error creating bean with name 'litQuery' defined in class path resource [cn/edu/scnu/spring/dao/litInformationDao/beans.xml]
    创建定义在[cn/edu/scnu/spring/dao/litInformationDao/beans.xml]配置文件中的bean litQuery时出错。Cannot resolve reference to bean 'litQueryById' while setting bean property 'litQueryById'找不到属性需要的bean 'litQueryById'.(英语不好,见笑,大概知道是这个意思)
    Cannot resolve reference to bean 'dataSource' while setting constructor argument一直向下分析,发现你的配置文件错误太多了,数据源的配置相当有问题,然后各个参数没有正确的注入...