使用Spring的Scala依赖注入

时间:2015-12-30 08:48:26

标签: java spring scala dependency-injection

我在项目中有两个模块:

第1单元:

我有一个scala类:

TimeSeries.scala

      trait TimeSeries {
        def series(sourceData: BoundedTimeSeries, destType: Type) : BoundedTimeSeries

        def series(sourceData: BoundedTimeSeries, destType: Type, 
        destDuration: Duration) : BoundedTimeSeries
        }

        object TimeSeries extends TimeSeries {
        def series(sourceData: BoundedTimeSeries, destType: Type) : BoundedTimeSeries = {
        new CalculatedBoundedTimeSeries(sourceData, destType);

        }
         def series(sourceData: BoundedTimeSeries, destType: Type, 
         destDuration: Duration) : BoundedTimeSeries = {
         new ComputedBoundedTimeSeries(sourceData, destType, destDuration);
         }
       }

这是XML文件中的bean条目:

<bean id="timeSeries" class="com.people.user.booking.TimeSeries" />

第2单元:

在此模块中,我在Autowiring类下面使用了TimeSeries个scala类java

class Xyz
{
   @Autowired
   private TimeSeries timeSeries;

   //Some code

   private void callTimeSeries()
   { 
       BoundedTimeSeries series = someMethodToGetSeries();
       Type type = someMethodToGetType();
       timeSeries.series(series, type);
   }

}

但我得到例外:

unsatisfied dependencies: Dependency on
[(objectClass=com.people.user.booking.TimeSeries)] (from bean [&timeSeries])

Caused by: org.springframework.beans.BeanInstantiationException: 
Could not instantiate bean class [com.people.user.booking.TimeSeries]
:  Specified class is an interface
at org.springframework.beans.factory.support.
SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:67)
at org.springframework.beans.factory.support.
AbstractAutowireCapableBeanFactory.
instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
... 15 more

所以TimeSeries scala对XYZ类的依赖注入(DI)没有正确发生。

我对一个java使用相同的DI到另一个java类,这种情况很好但是从Scala到Java的DI导致了这样的问题。

使用Spring的Scala依赖注入是否有不同的方式?

0 个答案:

没有答案