ale64bit
ale64bit

Reputation: 6242

Global onStart on Play Framework 2.3.7 not working?

Sorry if this question turns to be silly, but I simply cannot find my mistake and I checked already plenty of posts here in SO and other sites. I have setup a Play 2.3.7 project using Java. I have created a Global.java file in the common package under the app directory. In that file I override onStart (and other hooks) but I don't get them to work. They simply do not execute at all. Here's the Global.java file:

package common;

import play.Application;
import play.GlobalSettings;
import play.Logger;

public class Global extends GlobalSettings {

    @Override
    public void beforeStart(Application application) {
        Logger.error("good bye cruel world");
        super.beforeStart(application);
        throw new RuntimeException("WTF");
    }

    @Override
    public void onStart(Application application) {
        Logger.error("good bye cruel world");
        super.onStart(application);
        throw new RuntimeException("WTF");
    }

    @Override
    public void onStop(Application application) {
        Logger.error("good bye cruel world");
        super.onStop(application);
        throw new RuntimeException("WTF");
    }
}

And inside the application.conf, here's the relevant part, which is commented by default:

# Define the common.Global object class for this application.
# Default to common.Global in the root package.
# application.global=common.Global

What can be the problem? Thanks.

Upvotes: 8

Views: 3831

Answers (2)

MikeSchneeberger
MikeSchneeberger

Reputation: 525

It looks like you forgot to uncomment the application.global setting.

The following code worked just fine for me.

Global.java file:

package common;

import play.Application;
import play.GlobalSettings;
import play.Logger;

public class Global extends GlobalSettings {

    @Override
    public void beforeStart(Application application) {
        Logger.error("good bye cruel world");
        super.beforeStart(application);
    }

    @Override
    public void onStart(Application application) {
        Logger.error("good bye cruel world");
        super.onStart(application);
    }

    @Override
    public void onStop(Application application) {
        Logger.error("good bye cruel world");
        super.onStop(application);
    }
}

application.conf file:

# Define the Global object class for this application.
# Default to Global in the root package.
application.global=common.Global

Upvotes: 8

Michael Zajac
Michael Zajac

Reputation: 55569

The Global object must reside in the default package, so you need to remove package common.

As stated in the first paragraph of the documentation.

Upvotes: 6

Related Questions