简体   繁体   中英

Multiple Global Settings for Play 2

Can I have two Global settings objects for Play2?

I know there is only setting available at application.conf.

global=common.GlobalOne

Two Global classes:

public class GlobalOne extends GlobalSettings {

  @Override
  public void onStart(Application app) {
      Logger.info("****** Set something ******");
  }

}

public class GlobalTwo extends GlobalSettings {

  @Override
  public void onStart(Application app) {
      Logger.info("****** some other ******");
  }

}

The reason I am asking is for play 2 modules. How to have a global setting object in a module so it can be enabled when a project uses it?

That's not possible. GlobalSettings apply to an application, not modules. Therefore, modules should not define a GlobalSettings object.

I use such approach for merging multiple Global objects in root Global.java :

0) Folder structure:

/root
    /app
        Global.java
    /conf
        application.conf
    /subprojects
        /user
            /app
                /globals
                    UserGlobal.java
        /admin
            /app
                /globals
                    AdminGlobal.java

1) Put all subproject Global classes into globals package:

package globals;

public class UserGlobal extends GlobalSettings
{ ... }

2) Add configuration variable into all subproject conf files like a

application.global= globals.UserGlobal

3) Add private field and private method in root Global.java :

private List<GlobalSettings> subprojectGlobals = new ArrayList<>();

private void LoadSubprojectGlobals(Application app)
    throws ClassNotFoundException, IllegalAccessException, InstantiationException
{
    Reflections reflections = new Reflections("globals");
    Set<Class<? extends GlobalSettings>> subprojectGlobalClasses = 
        reflections.getSubTypesOf(GlobalSettings.class);

    for(Class subprojectGlobalClass : subprojectGlobalClasses)
    {
        subprojectGlobals.add(
            (GlobalSettings)subprojectGlobalClass.newInstance()
        );
    }
}

4) Place such code into onStart event handler in root Global.java :

@Override
public void onStart(Application app)
{
    try
    {
        LoadSubprojectGlobals(app);
    }
    catch(Exception e)
    {
        new RuntimeException(e);
    }

    for(GlobalSettings subprojectGlobal: subprojectGlobals)
    {
        subprojectGlobal.onStart(app);
    }
}

Now if you add some new subproject you must follow some conventions about adding Global class but you have no need to modify anything for merging functionality.

If you want to handle more global settings you need to override appropriate method in root Global.java .

Here is my question about this problem.

I have a project split with a main->frontend->common and main->backend->common dependency. In each (common, frontend and backend) I have a GlobalSetting. I need this for running the tests.

是的,这是不可能的,因为当您发出请求时,播放框架引擎将启动并为当前应用程序找到Global.java;或者,如果找不到它,您可以说出Action方法,否则它将跳过该.So,底线是扩展GlobalSettings的类名应该是Global.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM