java - beanName 不能为空

标签 java spring hibernate

我相当确信存在一些类或 jar 问题,但我不清楚它是什么。 beanName 错误不会在任何类型的搜索中提供非常有用的数据。当我收到错误时,我正在尝试在 Tomcat 8 上启动它。我在 hibernate 5 上运行。这是我的错误:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/data.xml]: 'beanName' must not be empty; nested exception is java.lang.IllegalArgumentException: 'beanName' must not be empty
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:223)
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:222)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:86)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:166)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1404)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1394)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: 'beanName' must not be empty
    at org.springframework.util.Assert.hasText(Assert.java:168)
    at org.springframework.beans.factory.config.RuntimeBeanReference.<init>(RuntimeBeanReference.java:58)
    at org.springframework.beans.factory.config.RuntimeBeanReference.<init>(RuntimeBeanReference.java:46)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:178)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:141)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:82)
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:220)
    ... 18 more

这是 data.xml 中引用的 bean:

 <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
        <property name="dataSource" ref="${database.dataSource}" />
        <property name="annotatedClasses">
            <list>
                <value>com.adilly.giftlist.model.BaseEntity</value>
                <value>com.adilly.giftlist.model.EmailOptOut</value>
                <value>com.adilly.giftlist.model.Event</value>
                <value>com.adilly.giftlist.model.EventComment</value>
                <value>com.adilly.giftlist.model.EventUser</value>
                <value>com.adilly.giftlist.model.PersistentLogins</value>
                <value>com.adilly.giftlist.model.User</value>
                <value>com.adilly.giftlist.model.WishItem</value>
                <value>com.adilly.giftlist.model.WishItemComment</value>
                <value>com.adilly.giftlist.model.WishItemReservation</value>
            </list>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${database.dialect}</prop>
                <prop key="hibernate.connection.autocommit">true</prop>
                <prop key="hibernate.connection.autoReconnect">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">${database.showSQL}</prop>
<!--
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
-->

            </props>
        </property>
    </bean>

最佳答案

问题是您将属性指向数据源!

<property name="dataSource" ref="${database.dataSource}" />

您应该创建一个 ID 为“datasource”的 bean,并在您的 sessionFactory 上引用它!看看这个例子:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}"/>         
</bean>

然后,在您的 sessionFactory 上,更改为:

<property name="dataSource" ref="dataSource" />

看看Spring JDCB Documentation here

关于java - beanName 不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41152741/

相关文章:

java - 如何忽略Spring Boot的ServerProperties中的未知字段?

java - 方法名称中具有逻辑条件的 JPA findBy

java - Hibernate更新记录并实现监听器: getting only required attribute values for event. getOldState()

java - 如何在 Ubuntu 上正确安装 Oracle Java 8?

java - 提交给执行者的 FutureTask 不运行

java - 在java中将十进制转换为二进制时遇到反转顺序问题

java - 将 Spring Security 添加为依赖项会导致应用程序表现不同

java - 为什么tomcat看不到某些文件夹?

java - Spring MVC中的密码加密

java - Hibernate ManyToMany 结果为笛卡尔积