Spring配置自定义命名空间

Spring配置自定义命名空间,spring,Spring,我有一个带有嵌入式ApacheFTP服务器的独立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/ftpserve

我有一个带有嵌入式ApacheFTP服务器的独立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>
<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>

不工作,因为
端口
在xsd中定义为
xs:int
。我想知道是否有任何解决方法(使用SpEL?)允许我使用AFS命名空间并从文件或系统属性加载端口属性。

您可以尝试使用
PropertyOverrideConfiguler

问题是您需要知道
标记定义的bean名称(可能是“服务器”)和
定义的属性类型(可能是bean定义的托管列表)

查看STSBeanExplorer以找到正确答案,并尝试以下内容

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

server.listeners[0].port=2222

在探索了一些选项之后,我决定最简单的方法是跳出afs名称空间,只进行侦听器配置。最终配置如下所示-

<?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>
<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>


我确实知道哪些类对应于自定义标记,但是
属性覆盖
不是解决方案,因为
端口
是构造函数arg,没有setter。我不确定我是否理解您关于在应用程序上下文中禁用验证的其他建议。你能详细说明一下吗?这里的问题是,
port
属性不仅在xsd中声明为int,而且在java类中也声明为int。我尝试了context.setValidating(false),这很有效,但在BeanDefinitonParser中失败,甚至在它到达PropertyPlaceHolderConfigure之前。原因是BeanDefinitonParser将端口属性解析为int,而不是将其作为字符串传递给PropertyPlaceHolderConfigure以供以后解析。这将在
java.lang.NumberFormatException中出现:对于输入字符串:“${ftp.port}”
。我开始觉得没有简单的方法可以做到这一点。。。