初始化 EbeanServer 时出现 NullPointerException

NullPointerException when initializing EbeanServer

我正在尝试使用 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.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.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,我发现这与配置有关。预期的属性对象似乎为 null,但实际情况并非如此。

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

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

这个错误让我很困惑。因为我使用 Spring 配置 EbeanServer,所以我似乎不需要 ebean.properties(编辑:添加一个空的 ebean.properties 会使错误消失,但不是有问题的例外)。事实上,在某一时刻这个设置是有效的(我可以 read/write 到数据库)。现在不是了,我快要发疯了,想弄清楚发生了什么变化。

这是我的 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>

可能相关的详细信息:

终于自己弄明白了。

简答

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,我也在使用它,它依赖于带有版本说明符 [6,7)avaje-ebeanorm。因此,出于某种原因,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")