【问题标题】:Inject property into non managed Spring xml将属性注入非托管 Spring xml
【发布时间】:2014-05-27 05:50:00
【问题描述】:

我有 xml。 (Ehcache.xml) 我想从属性文件(在类路径上)注入它的属性。

但是,由于这个 xml 不是 Spring 托管的 bean,所以我不能这样做。

有什么建议可以解决这个问题吗?

xml:

...
            properties="peerDiscovery=manual,rmiUrls=//**${other.node.hostname}**:41001/org.jasig.cas.ticket.ServiceTicket|//**${other.node.hostname}**:41001/org.jasig.cas.ticket.TicketGrantingTicket"
            propertySeparator="," />



    <cacheManagerPeerListenerFactory
            class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
            properties="port=41001,socketTimeoutMillis=5000" />

</ehcache>

我想从另一个属性文件中注入 ${other.node.hostname}。

但我在运行时得到了这个:

原因:java.net.URISyntaxException:索引 2 处的权限中存在非法字符://${other.node.hostname}:41001/org.jasig.cas.ticket.TicketGrantingTicket

谢谢, 射线。

【问题讨论】:

    标签: java xml spring properties ehcache


    【解决方案1】:

    在 ehcache.xml 中它不起作用,因为它不是 spring 配置。但是,如果您在 spring 中定义等效配置而不是 ehcache 文件,它应该可以工作:

    <context:property-placeholder location="classpath:config.properties"/>
    
    <bean id="myCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
        <property name="cacheManager" ref="cacheManager"/>
        <property name="maxElementsInMemory" value="${cache.maxMemoryElements}"/>
    </bean>
    

    最好的办法是尽可能少地使用 ehcache.xml 文件并在 spring 中配置所有内容,因为文件中的大多数选项都有 spring 等效项。

    【讨论】:

    • 但是您将如何使用这些属性修改新的 spring xml:cacheManagerPeerProviderFactory、cacheManagerPeerListenerFactory?
    猜你喜欢
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 2011-07-13
    • 2023-04-07
    相关资源
    最近更新 更多