Using Spring as a dependency injection framework with play 2.4.x?

后端 未结 3 1109
执念已碎
执念已碎 2021-02-08 23:22

I am exploring play-scala 2.4.2 and trying to get the spring DI working with it. I see there are a lot of changes in play 2.4.x and old way of overriding the GlobalSettings.getC

3条回答
  •  遇见更好的自我
    2021-02-08 23:38

    The latest version of Play:

    Create the class Global (Old Global than extended GlobaSettings):

    @Singleton
    public class Global {
    
        private static final String APPLICATION_CONTEXT = "applicationContext.xml";
    
        private ConfigurableApplicationContext applicationContext;
    
        @Inject
        public Global( ApplicationLifecycle lifecycle ) {
            applicationContext = new ClassPathXmlApplicationContext(APPLICATION_CONTEXT_XML);
            lifecycle.addStopHook( () -> {
                applicationContext.close();
                return F.Promise.pure( null );
            });
        }
    
    }
    

    Create the class ConfigurableApplicationContextModule:

    public class ApplicationContextModule extends AbstractModule {
    
        @Override
        protected void configure() {
            bind( Global.class ).asEagerSingleton();
        }
    
    }
    

    In application.conf add this:

    play.modules.enabled += "config.ApplicationContextModule"
    

    Create file applicationContext.xml:

    
    
    
           
    
    
    

    After creating what stated above by Ranga

提交回复
热议问题