bean的初始化失败;嵌套异常是org.springframework.beans

时间:2014-10-14 17:52:42

标签: hibernate spring-mvc

  

我将我的模型类保存在com.anand.model.Employee.java中   我正确地在sdnext-servlet.xml中提到了annotatedClasses。   但我仍然得到错误。请帮我解决这个问题。

sdnext-servlet.xml中

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.anand.model.Employee</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>             
            </props>
        </property>
    </bean>

模型类:Employee.java

package com.anand.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * @author Anand Gopalan
 *
 */
@Entity
@Table(name="Employee")
public class Employee implements Serializable{

    private static final long serialVersionUID = -723583058586873479L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "empid")
    private Integer empId;

    @Column(name="empname")
    private String empName;

    @Column(name="empaddress")
    private String empAddress;

    @Column(name="salary")
    private Long salary;

    @Column(name="empAge")
    private Integer empAge;

    public Integer getEmpId() {
        return empId;
    }

    public void setEmpId(Integer empId) {
        this.empId = empId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public String getEmpAddress() {
        return empAddress;
    }

    public void setEmpAddress(String empAddress) {
        this.empAddress = empAddress;
    }

    public Long getSalary() {
        return salary;
    }

    public void setSalary(Long salary) {
        this.salary = salary;
    }

    public Integer getEmpAge() {
        return empAge;
    }

    public void setEmpAge(Integer empAge) {
        this.empAge = empAge;
    }

}

日志:

  

严重:StandardWrapper.Throwable    org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; sessionFactory&#39;的bean在ServletContext中定义   resource [/WEB-INF/config/sdnext-servlet.xml]:bean的初始化   失败;嵌套异常是   org.springframework.beans.TypeMismatchException:无法转换   类型&#39; java.util.ArrayList&#39;的属性值要求的类型   &#39; java.lang.Class中[]&#39;对于属性&#39; annotatedClasses&#39 ;;嵌套异常   是java.lang.IllegalArgumentException:找不到类   [com.anand.model.Employee] at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)     在   org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:290)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)     在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)     在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:545)     在   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)     在   org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:443)     在   org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:459)     在   org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:340)     在   org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:307)     在   org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)     在javax.servlet.GenericServlet.init(GenericServlet.java:158)at   org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)     在   org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)     在   org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)     在   org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5229)     在   org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5516)     在   org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)     在   org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1575)     在   org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1565)     在java.util.concurrent.FutureTask.run(未知来源)at   java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源)at   java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)at   java.lang.Thread.run(未知来源)引起:   org.springframework.beans.TypeMismatchException:无法转换   类型&#39; java.util.ArrayList&#39;的属性值要求的类型   &#39; java.lang.Class中[]&#39;对于属性&#39; annotatedClasses&#39 ;;嵌套异常   是java.lang.IllegalArgumentException:找不到类   [com.anand.model.Employee] at   org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:457)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1354)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1313)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1067)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)     ... 26更多引起:java.lang.IllegalArgumentException:不能   找到班级[com.anand.model.Employee]   org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:294)     在   org.springframework.beans.propertyeditors.ClassEditor.setAsText(ClassEditor.java:64)     在   org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:474)     在   org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:446)     在   org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:215)     在   org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:122)     在   org.springframework.beans.TypeConverterDelegate.convertToTypedArray(TypeConverterDelegate.java:485)     在   org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:227)     在   org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:154)     在   org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:452)     ... 30更多引起:java.lang.ClassNotFoundException:   com.anand.model.Employee at   org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)     在   org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)     在org.springframework.util.ClassUtils.forName(ClassUtils.java:258)     在   org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:291)     ... 39更多

     

2014年10月14日上午11:40:35 org.apache.catalina.core.StandardContext   loadOnStartup SEVERE:Servlet / sdnext引发了load()异常   java.lang.ClassNotFoundException:com.anand.model.Employee at   org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)     在   org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)     在org.springframework.util.ClassUtils.forName(ClassUtils.java:258)     在   org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:291)     在   org.springframework.beans.propertyeditors.ClassEditor.setAsText(ClassEditor.java:64)     在   org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:474)     在   org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:446)     在   org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:215)     在   org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:122)     在   org.springframework.beans.TypeConverterDelegate.convertToTypedArray(TypeConverterDelegate.java:485)     在   org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:227)     在   org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:154)     在   org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:452)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1354)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1313)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1067)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)     在   org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:290)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)     在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)     在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:545)     在   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)     在   org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:443)     在   org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:459)     在   org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:340)     在   org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:307)     在   org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)     在javax.servlet.GenericServlet.init(GenericServlet.java:158)at   org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)     在   org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)     在   org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)     在   org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5229)     在   org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5516)     在   org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)     在   org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1575)     在   org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1565)     在java.util.concurrent.FutureTask.run(未知来源)at   java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源)at   java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)at   java.lang.Thread.run(未知来源)

3 个答案:

答案 0 :(得分:7)

这是您所做的替代方案。尝试将annotatedClasses替换为配置中以下元素的<property name="packagesToScan"> <list> <value>com.anand.model</value> </list> </property> ,并在此之后清理构建工作区+重新启动服务器。

{{1}}

答案 1 :(得分:0)

您的问题的解决方案:清理maven并再次更新maven并构建它。它会工作,maven缺少一些包,因为它显示错误,所以一旦清理并更新maven

答案 2 :(得分:0)

尝试清理Maven文件并构建它并清理项目,并清理服务器工作目录和清理服务器。并运行该项目。它会起作用。