2016-12-15 26 views
0

我想注入一個自定義彈簧模塊驗證函數的依賴。我曾嘗試在setter上使用@Autowired,但它沒有效果。有誰知道是否或如何做到這一點?我怎樣才能注入到Spring模塊驗證(Valang)自定義函數的依賴項?

這裏是我的valang驗證:

<bean id="resellerValidator" class="org.springmodules.validation.valang.ValangValidator"> 
    <property name="customFunctions"> 
     <map> 
     <entry key="isValidResellerId" value="com.myproject.valang.IsValidResellerIdFunction" /> 
     </map> 
    </property> 
    <property name="valang"> 
     <value> 
     <![CDATA[ 
      {resellerId: ? IS BLANK OR isValidResellerId(?) IS TRUE : '' : 'field.badFormat'} 
     ]]> 
     </value> 
    </property> 
</bean> 

這裏是自定義函數:

public class IsValidResellerIdFunction extends AbstractFunction { 

    private ResellerService resellerService; 

    @Autowired 
    public void setResellerService(ResellerService resellerService) { 
     this.resellerService = resellerService; 
    } 

    public IsValidResellerIdFunction(Function[] arguments, int line, int column) { 
     super(arguments, line, column); 
     definedExactNumberOfArguments(1); 
    } 

    @Override 
    protected Object doGetResult(Object o) throws Exception { 
     String resellerId = (String) getArguments()[0].getResult(o); 
     return resellerService.isValidResellerId(resellerId); 
    } 

} 

回答

0

其實我清盤發現答案是here。總之,需要2件事:

  1. 創建一個FunctionWrapper bean並將我的自定義函數注入到scope =「prototype」的包裝中。這使我可以將我想要的任何依賴注入到我的自定義函數中。
  2. 實現ConfigurableFunction接口,以便它可以與FunctionWrapper一起使用。

的FunctionWrapper是所有valang驗證自動發現,因此沒有必要與customFunction屬性顯式聲明的自定義函數,函數的名字只是你給FunctionWrapper bean的名字:

<bean id="resellerValidator" class="org.springmodules.validation.valang.ValangValidator"> 
    <property name="valang"> 
     <value> 
     <![CDATA[ 
      {resellerId: ? IS BLANK OR isValidResellerId(?) IS TRUE : '' : 'field.badFormat'} 
     ]]> 
     </value> 
    </property> 
</bean> 

<bean id="isValidResellerId" 
     class="org.springmodules.validation.valang.functions.FunctionWrapper" 
     scope="prototype"> 
    <aop:scoped-proxy/> 
    <property name="function"> 
     <bean id="isValidResellerIdFunction" 
       class="com.myproject.valang.IsValidResellerIdFunction" 
       scope="prototype"> 
      <property name="resellerService" ref="resellerService"/> 
     </bean> 
    </property> 
</bean> 

功能:

public class IsValidResellerIdFunction extends AbstractFunction implements ConfigurableFunction { 

    private ResellerService resellerService; 

    public void setResellerService(ResellerService resellerService) { 
     this.resellerService = resellerService; 
    } 

    public IsValidResellerIdFunction() {} 

    @Override 
    public int getExpectedNumberOfArguments() { 
     return 1; 
    } 

    @Override 
    public void setArguments(int expectedNumberOfArguments, Function[] arguments, 
      int line, int column) { 
     super.setTemplate(line, column); 
     super.setArguments(arguments); 
     super.definedExactNumberOfArguments(expectedNumberOfArguments); 
    } 

    @Override 
    protected Object doGetResult(Object o) throws Exception { 
     String resellerId = (String) getArguments()[0].getResult(o); 
     return resellerService.isValidResellerId(resellerId); 
    } 

} 
0

添加到您的應用程序上下文:

<bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/> 
+0

感謝您的回答,但是這並沒有影響。 –

相關問題