Reputation: 9428
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?
Upvotes: 2
Views: 1650
Reputation: 123
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.
Upvotes: 1
Reputation: 1
yes that is not possible because when you give request than play framework engine starts and it finds Global.java for the current Application or you can say Action method if it doesnot find than it skips that .So , bottomline is the class name which extends GlobalSettings should be Global.java
Upvotes: 0
Reputation: 4435
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.
Upvotes: 0
Reputation: 9663
That’s not possible. GlobalSettings
apply to an application, not modules. Therefore, modules should not define a GlobalSettings
object.
Upvotes: 5