Spring 在用Scala和Java编写的应用程序中使用DI——是否可以在;“斯卡拉路”没有额外的框架?

Spring 在用Scala和Java编写的应用程序中使用DI——是否可以在;“斯卡拉路”没有额外的框架?,spring,scala,dependency-injection,Spring,Scala,Dependency Injection,我有一个部分用Java和Scala编写的应用程序。对于Scala代码,我想使用Scala特性进行依赖项注入。在旧Java代码中,使用spring,即将服务bean注入wicket页面等。在scala代码中,我使用无私特质模式创建服务,如下所述: scala中的依赖项注入目前使用“配置”对象完成,该对象将所有内容连接在一起: scala> trait Friendly { | def greet() { println("hi there") } | } defin

我有一个部分用Java和Scala编写的应用程序。对于Scala代码,我想使用Scala特性进行依赖项注入。在旧Java代码中,使用spring,即将服务bean注入wicket页面等。在scala代码中,我使用无私特质模式创建服务,如下所述:

scala中的依赖项注入目前使用“配置”对象完成,该对象将所有内容连接在一起:

scala> trait Friendly {
     |   def greet() { println("hi there") }
     | }
defined trait Friendly

scala> object Friendly extends Friendly
defined module Friendly

scala> object ServiceConsumer {
     |  def myService: Friendly = myServiceInjected.getOrElse(throw new RuntimeException("dependency not injected"))
     |  var myServiceInjected: Option[Friendly] = None
     | }
defined module ServiceConsumer

scala> class ServiceConsumer {
     |  def callService() {
     |    ServiceConsumer.myService.greet()
     |  }
     | }
defined class ServiceConsumer

scala> object Configuration { 
     |  def init() {
     |   ServiceConsumer.myServiceInjected = Some(Friendly)
     |  }
     | }
defined module Configuration

scala> Configuration.init

scala> val c = new ServiceConsumer
c: ServiceConsumer = ServiceConsumer@292e2fba

scala> c.callService()
hi there

但是如何使用Java类中的“友好”服务呢?我还应该用弹簧吗?如何将Scala编写的友好服务作为Springbean发布

我认为蛋糕图案对Scala DI来说非常好。我最喜欢的文章是:


我认为蛋糕图案对Scala DI非常好。我最喜欢的文章是:


我不确定这是否正是您想要的,但我会告诉您一个适合我的实践

我的Java代码是使用Google的DI框架(称为API)连接在一起的,我在Scala代码中调用了API。举个例子,这里有一个模块——这是一个GUI配置的东西——它将接口绑定到它们的实现。该API是一个JavaAPI,但我正在通过Scala代码与它进行交互

class UserSessionModule(storage: ActorRef) extends AbstractModule {
  protected def configure: Unit = {
    bind(classOf[ActorRef]).
      annotatedWith(Names.named("storage")).toInstance(storage)
    bind(classOf[UserSession]).toProvider(classOf[UserSessionProvider])
  }
}

我发现这是一个非常轻松的方法。

我不确定这是否正是您想要的,但我会告诉您一个适合我的实践

我的Java代码是使用Google的DI框架(称为API)连接在一起的,我在Scala代码中调用了API。举个例子,这里有一个模块——这是一个GUI配置的东西——它将接口绑定到它们的实现。该API是一个JavaAPI,但我正在通过Scala代码与它进行交互

class UserSessionModule(storage: ActorRef) extends AbstractModule {
  protected def configure: Unit = {
    bind(classOf[ActorRef]).
      annotatedWith(Names.named("storage")).toInstance(storage)
    bind(classOf[UserSession]).toProvider(classOf[UserSessionProvider])
  }
}

我发现这是一种非常简单的方法。

我将在Scala中创建一个工厂方法:

class MyService { def doSomething = ... }
object MyService {
  def create: MyService = ...
}
然后从Java将其用作静态方法:

import com.mycompany.myproject.MyService
MyService svc = MyService.create
svc.doSomething

我将在Scala中创建一个工厂方法:

class MyService { def doSomething = ... }
object MyService {
  def create: MyService = ...
}
然后从Java将其用作静态方法:

import com.mycompany.myproject.MyService
MyService svc = MyService.create
svc.doSomething

我用的是SpringDI,Scala绝对没有麻烦

<?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-2.5.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:component-scan base-package="com.mypackage"/>

</beans>
然后通过以下方式访问:

class OtherClass {
    @Autowired
    var myService:MyService = _
}

我用的是SpringDI,Scala绝对没有麻烦

<?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-2.5.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:component-scan base-package="com.mypackage"/>

</beans>
然后通过以下方式访问:

class OtherClass {
    @Autowired
    var myService:MyService = _
}

但我的问题不是如何在scala中进行依赖项注入(有很多方法可以选择),而是如何将用scala编写的服务注入到用java编写的类中,而不使用Spring之类的框架?或者-如果这个解决方案不可行-如何将scala服务(scala对象)发布为Springbean?但我的问题不是如何在scala中进行依赖项注入(有许多选项可以实现),而是如何将用scala编写的服务注入到用java编写的类中,而不使用spring之类的框架?或者-如果这个解决方案不可行-如何将scala服务(scala对象)发布为Springbean?您认为这实际可行吗?我认为从Java调用Scala对象需要类似于
MyService svc=MyService$.MODULE$.create
@Landei的东西。在Scala 2.7中,静态转发器在那时就被破坏了。但是他们现在工作得很好!你认为这真的有效吗?我认为从Java调用Scala对象需要类似于
MyService svc=MyService$.MODULE$.create
@Landei的东西。在Scala 2.7中,静态转发器在那时就被破坏了。但是他们现在工作得很好!