【问题标题】:Using Spring as a dependency injection framework with play 2.4.x?使用 Spring 作为带有 play 2.4.x 的依赖注入框架?
【发布时间】:2015-09-30 08:56:01
【问题描述】:

我正在探索 play-scala 2.4.2 并试图让 spring DI 使用它。我看到 play 2.4.x 中发生了很多变化,并且覆盖 GlobalSettings.getControllerInstance 的旧方式似乎不再是一种选择。

我遇到了这个项目https://github.com/jroper/play-spring,但它似乎更像是证明 Spring DI 是可能的 POC,但似乎不像早期版本那样容易。这会是当前和未来 play 版本的 spring 集成机制,还是可以期待 play 社区的某个更简单的机制或框架?

【问题讨论】:

标签: java spring scala playframework


【解决方案1】:

请按照以下步骤操作:

第一步:build.sbt文件中添加spring依赖。

libraryDependencies += "org.springframework" % "spring-context" % "4.1.6.RELEASE"
libraryDependencies += "org.springframework" % "spring-core" % "4.1.6.RELEASE"
libraryDependencies += "org.springframework" % "spring-beans" % "4.1.6.RELEASE"
libraryDependencies += "org.springframework" % "spring-aop" % "4.1.6.RELEASE"

第 2 步: 创建一个新类 (ApplicationGlobalSettings.java) 并使用 GlobalSettings 类实现。

package com.ranga.global.settings;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import play.Application;
import play.GlobalSettings;
public class ApplicationGlobalSettings extends GlobalSettings { 


private static final String APPLICATION_CONTEXT_XML = "applicationContext.xml";
private ConfigurableApplicationContext applicationContext;

@Override
public void beforeStart(Application application) {      
    super.beforeStart(application);
}

@Override
public void onStart(Application application) {      
    super.onStart(application);     
    applicationContext = new ClassPathXmlApplicationContext(APPLICATION_CONTEXT_XML);           
}

@Override
public void onStop(Application application) {       
    super.onStop(application);
    if(applicationContext != null) {
        applicationContext.close();
    }
}

}

Step3:conf文件夹下新建spring配置文件(applicationContext.xmlconf\applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

   <context:component-scan base-package="com.ranga.services, com.ranga.daos"/>

</beans>

Step4:将新创建的 GlobalSettings 文件位置添加到应用程序配置文件(conf/application.conf)中。

.....some more configuration here.....
# Global Objects class
application.global=com.ranga.global.settings.ApplicationGlobalSettings

Step5:在com.ranga.service包(HelloWorldService.java)下新建一个服务类。

package com.ranga.services;
import javax.inject.Inject;
import org.springframework.stereotype.Service;

import com.ranga.daos.HelloWorldDAO;
@Service
public class HelloWorldService {

    @Inject
    private HelloWorldDAO helloWorldDAO;

    public String sayHello() {
        return helloWorldDAO.sayHello();
    }
}

第六步:com.ranga.daos包(HelloWorldDAO.java)下新建一个dao类。

package com.ranga.daos;

import org.springframework.stereotype.Repository;
@Repository
public class HelloWorldDAO {
    public String sayHello() {
        return "Hello Ranga!";
    }
}

Step7:最后在Application.java文件中注入HelloWorldService

package com.ranga.controllers;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Autowired;

import com.ranga.services.HelloWorldService;

import play.*;
import play.mvc.*;

import views.html.*;

public class Application extends Controller {

    @Inject
    private HelloWorldService helloWorldService;

    public Result index() {         
        return ok(index.render(helloWorldService.sayHello()));
    }
}

Step8:最后修改index.scala.html文件代码。

@(message: String)

<h1>@message</h1>

现在完成.. 运行应用程序。

【讨论】:

    【解决方案2】:

    Play最新版:

    创建类 Global(旧的 Global 比扩展的 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 );
            });
        }
    
    }
    

    创建类 ConfigurableApplicationContextModule:

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

    在 application.conf 中添加:

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

    创建文件applicationContext.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
           <context:component-scan base-package="services, dao"/>
    
    </beans>
    

    Ranga创建上述内容后

    【讨论】:

      【解决方案3】:

      以防万一它可以帮助某人,我还研究了一个基于 jroper 项目的解决方案:https://github.com/jroper/play-spring。 关键是使用 spring 的扫描功能来“加载” Play 类:

      ctx.scan(packages:_*)

      使用默认播放的包:

      def defaultPackages(): Seq[String] = Seq("router", "play", "controllers")

      该解决方案适用于 1 hack:您需要在 Play 类中的 @Singleton 注释旁边添加 @javax.inject.Named 以便 Spring 可以扫描并加载它们(即您需要“分叉” Play您正在使用的版本,但这是一个相当小且容易的更改)。 所以这是我使用 SpringApplicationLoader 的示例应用程序: https://github.com/remithieblin/play24_spring

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-08
        • 2015-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-14
        • 1970-01-01
        相关资源
        最近更新 更多