2013-04-04 113 views
-1

我有一個獨立的彈簧應用程序與嵌入式Apache FTP服務器。這個配置看起來是這樣的 -Spring配置自定義命名空間

<?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:afs="http://mina.apache.org/ftpserver/spring/v1" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://mina.apache.org/ftpserver/spring/v1 http://mina.apache.org/ftpserver/ftpserver-1.0.xsd">  

    <context:property-placeholder location="classpath:config.properties" system-properties-mode="OVERRIDE"/> 

    <afs:server id="server" anon-enabled="false"> 

     <afs:listeners> 
      <afs:nio-listener name="default" port="2222" 
       idle-timeout="60" /> 
     </afs:listeners> 

     <!-- other AFS config --> 

    </afs:server> 
</beans> 

我想從屬性文件加載的nio-listenerport財產,但

<afs:nio-listener name="default" port="${ftp.port}" 
        idle-timeout="60" /> 

不起作用,因爲port在XSD定義爲xs:int 。我想知道是否有任何解決方法(使用SpEL?),這將允許我使用AFS名稱空間從文件或系統屬性中加載port屬性。

回答

0

在探索了幾個選項之後,我已經決定最簡單的方法是跳過afs命名空間來完成監聽器配置。最後設置是這樣的 -

<bean id="listenerFactory" class="org.apache.ftpserver.listener.ListenerFactory"> 
    <property name="port" value="${ftp.port}" /> 
    <property name="dataConnectionConfiguration"> 
     <bean factory-bean="dataConnectionConfigurationFactory" 
      factory-method="createDataConnectionConfiguration" /> 
    </property> 
</bean> 

<bean id="dataConnectionConfigurationFactory" class="org.apache.ftpserver.DataConnectionConfigurationFactory" /> 

<bean id="nioListener" factory-bean="listenerFactory" factory-method="createListener" /> 

<afs:server id="server" anon-enabled="false"> 

    <afs:listeners> 
     <afs:listener name="default"> 
      <ref bean="nioListener"/> 
     </afs:listener> 
    </afs:listeners> 

    <!-- other AFS config --> 

</<afs:server> 
0

您可以試用PropertyOverrideConfigurer

問題是您需要知道<afs:server>標籤定義的bean名稱(可能是'server')以及<afs:listeners>定義的屬性類型(可能是bean定義的託管列表)。

看STS豆資源管理器找到正確答案,並嘗試蒙山像

<context:property-override location="classpath:config.properties" /> 

server.listeners[0].port=2222 

其他選項禁用模式驗證設置驗證爲假的XML應用程序上下文刷新前。

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
         new String[] {"applicationContext.xml"}, false); 
     context.setValidating(false); 
     context.refresh(); 
+0

我知道哪些類對應的自定義標籤,但因爲'port'是一個構造ARG並沒有一個二傳手'財產override'不是解決辦法。我不確定我瞭解您的其他建議,即禁用應用程序上下文中的驗證。你能否詳細說明一下?這裏的問題是'port'屬性不僅在xsd中被聲明爲int,而且在java類中也被聲明爲int。 – Vivek 2013-04-05 05:07:13

+0

我試過context.setValidating(false),它工作正常,但在BeanDefinitonParser中失敗,甚至在它到達PropertyPlaceholderConfigurer之前。原因是BeanDefinitonParser將port屬性解析爲int,而不是將其作爲String傳遞給PropertyPlaceholderConfigurer以稍後解析。 – Vivek 2013-04-05 12:54:59

+0

這會導致java.lang.NumberFormatException異常:對於輸入字符串:「$ {ftp.port}」'。我開始認爲沒有簡單的方法來做到這一點...... – Vivek 2013-04-05 13:00:28