java Playframework GlobalSettings deprecation for onStart

后端 未结 1 1084
终归单人心
终归单人心 2021-01-01 03:59

I have annoying issue with Playframwork deprecated GlobalSettings issue, I want to move my conde inside onStart to suggested way, but Actually I ca

相关标签:
1条回答
  • 2021-01-01 04:32

    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.

    0 讨论(0)
提交回复
热议问题