2016-03-29 101 views
0

我正在使用Twitter Scala類創建一個bean的角力。它的構造函數參數不是簡單類型,而是Scala Function1類型。我如何去創建它?春天和斯卡拉 - Function1構造函數參數

<bean id="permissivePolicy" class="com.twitter.finagle.http.filter.Cors.Policy"> 
</bean> 

這個類的構造函數的參數是:

case class Policy(
    allowsOrigin: String => Option[String], 
    allowsMethods: String => Option[Seq[String]], 
    allowsHeaders: Seq[String] => Option[Seq[String]], 
    exposedHeaders: Seq[String] = Nil, 
    supportsCredentials: Boolean = false, 
    maxAge: Option[Duration] = None 
) 

這意味着,在Spring bean的,我需要通過類型構造函數ARGS:

1. allowsOrigin - Function1<String, Option<String>> 
2. allowsMethods - Function1<String, Option<Seq<String>>> 
3. allowsHeaders - Function1<Seq<String>, Option<Seq<String>>> 
4. exposedHeaders - Seq<String> 
5. supportsCredentials - boolean 
6. maxAge - Option<Duration> 

我不是很好在Scala中,所以我不確定如何去創建這個bean。有任何想法嗎?

回答

0

最後整理了一下。我首先要斯卡拉-UTIL添加到application.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" 
     xmlns:scala-util="http://www.springframework.org/schema/scala/util" 
     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 
      http://www.springframework.org/schema/scala/util 
      http://www.springframework.org/schema/scala/util/scala-util.xsd"> 

我還需要創建一個Scala實現的功能1性狀(這基本上只是一個接口):

package com.test.util 

    class Function1Impl extends Function1[String, Option[String]] { 
     override def apply(v1: String): Option[String] = Some("") 
    } 

現在,回到application.xml中,我創建了必要的豆相匹配的構造函數指定參數和創建的permissivePolicy豆引用它們:

 <bean id="scalaOptionBean" class="scala.Some"> 
     <constructor-arg value="" /> 
     </bean> 
     <bean id="function1Bean" class="com.test.util.Function1Impl" /> 
     <scala-util:seq id="scalaSeqBean" /> 

     <bean id="permissivePolicy" class="com.twitter.finagle.http.filter.Cors.Policy"> 
     <constructor-arg ref="function1Bean" /> 
     <constructor-arg ref="function1Bean" /> 
     <constructor-arg ref="function1Bean" /> 
     <constructor-arg ref="scalaSeqBean" /> 
     <constructor-arg type="boolean" value="true" /> 
     <constructor-arg ref="scalaOptionBean" /> 
     </bean> 

     <bean id="httpFilter" 
      class="com.twitter.finagle.http.filter.Cors.HttpFilter"> 
     <constructor-arg ref="permissivePolicy" /> 
     </bean> 

這給了我一個策略對象與埃森特特別是正確類型的空構造函數參數。如果我想在某些時候填充它們,只需要更改Function1Impl或調整application.xml中的值。