I have annoying issue with Playframwork deprecated GlobalSettings
issue, I want to move my conde inside onStart
to suggested way, but Actually I ca
You need two classes - one to handle the initialization, and a module to register the binding.
The initialization code:
@Singleton
public class OnStartup {
@Inject
public OnStartup(final UserService userService) {
if (userService.findUserByEmail("email@company.com") == null) {
String email = "email@company.com";
String password = "1234";
String fullName = "My Name";
User user = new User();
user.password = BCrypt.hashpw(password, BCrypt.gensalt());
user.full_name = fullName;
user.email = email;
user.save();
}
}
}
The module:
public class OnStartupModule extends AbstractModule {
@Override
public void configure() {
bind(OnStartup.class).asEagerSingleton();
}
}
Finally, add your module to application.conf
.
play.modules.enabled += "com.example.modules.OnStartupModule"
By making the singleton eager, it will run when the application is starting up.