PlayFramework, how to register a BeanPersistListen

2019-07-16 11:49发布

I discovered the BeanPersistListener that I'd like to register to some of my models, but I didn't find any documentation, from Ebean nor PlayFramework on how to integrate it.

From the documentation :

A BeanPersistListener is either found automatically via class path search or can be added programmatically via ServerConfiguration.addEntity().

Apparently, it isn't found automatically (I added some Logger.info in the implemented methods, and nothing was shown), so I'd like to add it via ServerConfiguration, but how? where?

I'm also suspecting it's a version problem. From what I've seen, BeanPersistListener is from Ebean 2.6.0, but I can't find which version of Ebean PlayFramework is running (I'm using 2.0.4).

2条回答
Summer. ? 凉城
2楼-- · 2019-07-16 12:17

First Play 2.0.4 uses Ebean version 2.7.3 [1]

And for registering your listener, you could try to use a ServerConfigStartup as shown in this documentation:

package models;

import com.avaje.ebean.config.ServerConfig;
import com.avaje.ebean.event.ServerConfigStartup;
import com.avaje.ebean.event.BeanPersistListener;

public class MyServerConfigStartup implements ServerConfigStartup {
    @Override
    public void onStart(ServerConfig serverConfig) {
        serverConfig.add(new BeanPersistListener() {
            ....
        });
    }
}

I never tested it, I think it is worth a try :-)

Update from comments:

You'll need to put these classes under the models package or a child of the models package so that Ebean can find them.

查看更多
倾城 Initia
3楼-- · 2019-07-16 12:21

I see a two problems with BeanPersistListener in PLay Framework:

  1. I must create for every entity own BeanPersistListener. Not nice when I have 100 enities.

  2. BeanPersistListener is fired in background thread. I have no information who was user.

查看更多
登录 后发表回答