初始化EbeanServer时出现NullPointerException

时间:2015-08-27 21:18:25

标签: java spring ebean

我尝试使用EbeanORM连接到MS SQL Server 2012.此外,我使用的是Spring和jTDS驱动程序。

我在初始化Spring上下文时遇到以下异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ebeanServer' defined in class path resource [spring.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:753)
  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
  at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
  at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
  at com.example.cloud.dal.Dal.<clinit>(Dal.java:48)
  ... 43 more
Caused by: java.lang.NullPointerException
  at com.avaje.ebeaninternal.server.changelog.DefaultChangeLogListener.configure(DefaultChangeLogListener.java:62)
  at com.avaje.ebeaninternal.server.core.DefaultServer.configureServerPlugins(DefaultServer.java:257)
  at com.avaje.ebeaninternal.server.core.DefaultServer.<init>(DefaultServer.java:248)
  at com.avaje.ebeaninternal.server.core.DefaultContainer.createServer(DefaultContainer.java:130)
  at com.avaje.ebeaninternal.server.core.DefaultContainer.createServer(DefaultContainer.java:45)
  at com.avaje.ebean.EbeanServerFactory.createInternal(EbeanServerFactory.java:108)
  at com.avaje.ebean.EbeanServerFactory.create(EbeanServerFactory.java:67)
  at com.avaje.ebean.springsupport.factory.EbeanServerFactoryBean.afterPropertiesSet(EbeanServerFactoryBean.java:54)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$6.run(AbstractAutowireCapableBeanFactory.java:1627)
  at java.security.AccessController.doPrivileged(Native Method)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1624)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
  ... 55 more

查看code in question,我发现这与配置有关。似乎预期的属性对象为空,并且预期不会出现。

我确实在日志中看到以下错误:

16:39:12.070 [run-main-2] ERROR c.a.ebean.config.PropertyMapLoader - ebean.properties not found

这个错误令我感到困惑。由于我使用Spring来配置EbeanServer,因此我似乎不需要ebean.properties(编辑:添加空ebean.properties会导致错误消失,但不是有问题的例外)。事实上,此设置一度正常(我可以读/写DB)。现在不行了,我疯狂地想弄清楚发生了什么变化。

这是我的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:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
                      http://www.springframework.org/schema/beans/spring-beans.xsd
                      http://www.springframework.org/schema/context
                      http://www.springframework.org/schema/context/spring-context.xsd
                      http://www.springframework.org/schema/tx
                      http://www.springframework.org/schema/tx/spring-tx.xsd
                     ">

  <import resource="classpath:default-ebean-server.xml"/>

  <context:property-placeholder location="classpath:dal.properties" />

  <context:component-scan
    base-package="com.example.dal.service" />

  <context:component-scan
    base-package="com.example.dal.dao" />

  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="net.sourceforge.jtds.jdbc.Driver" />
    <property name="jdbcUrl" value="${db.jdbc_url}" />
    <property name="user" value="${db.user}" />
    <property name="password" value="${db.password}" />

    <!-- these are C3P0 properties -->
    <property name="initialPoolSize" value="${db.min_pool_size}" />
    <property name="minPoolSize" value="${db.min_pool_size}" />
    <property name="maxPoolSize" value="${db.max_pool_size}" />
    <property name="preferredTestQuery" value="SELECT 1" />
    <property name="idleConnectionTestPeriod" value="300" />
    <property name="connectionCustomizerClassName" value="com.example.dal.IsolationLevelConnectionCustomizer" />
  </bean>

  <tx:annotation-driven transaction-manager="transactionManager"/>

  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
  </bean>

  <bean id="serverConfig" parent="defaultEbeanServerConfig">
    <property name="name" value="serverConfig"/>
    <property name="packages">
      <list>
        <value>com.example.dal.model</value>
      </list>
    </property>
    <property name="dataSource" ref="dataSource"/>
  </bean>

  <bean id="ebeanServer" class="com.avaje.ebean.springsupport.factory.EbeanServerFactoryBean">
    <property name="serverConfig" ref="serverConfig"/>
  </bean>
</beans>

可能相关的细节:

  • Java 8
  • EbeanORM 6.4.1
  • Spring 4.1.7.RELEASE
  • avaje-ebeanorm-spring 4.5.3
  • jTDS 1.3.1

1 个答案:

答案 0 :(得分:0)

最后自己想出来。

简答

似乎EbeanORM 6.4.1中引入的新功能打破了一些东西。这是有问题的功能:https://github.com/ebean-orm/avaje-ebeanorm/issues/390

降级(见下文)至6.3.1解决了这个问题。

长答案

当我第一次开始工作时,我一直在使用EbeanORM 6.3.1。我升级到6.4.1,看到它已在几天前发布。我注意到事情发生后不久(没有自动测试)。我再次降级到6.3.1,但这并没有解决问题所以我认为这是一个红色的鲱鱼。

我不知道,SBT / Ivy正在推动6.3.1支持6.4.1,尽管我明确依赖6.3.1。我正在使用的avaje-ebeanorm-spring依赖于avaje-ebeanorm版本说明符[6,7)。所以,出于某种原因,SBT / Ivy认为6.4.1是这两个版本要求(WTF ......)的最佳解决方案。

我用这个出色的插件来解决这个问题:https://github.com/jrudolph/sbt-dependency-graph

为了强制SBT给我6.3.1,我修改了我的依赖项以添加exclude

"org.avaje.ebeanorm" % "avaje-ebeanorm" % "6.3.1",
"org.avaje.ebeanorm" % "avaje-ebeanorm-spring" % "4.5.3" exclude("org.avaje.ebeanorm", "avaje-ebeanorm")