发布于 2024-12-23 05:08:08 字数 1496 浏览 2 评论 0 原文

我真的被这个问题困住了......救命! :)

我正在使用 j2ee:jndi 查找属性文件。以下工作正常:

<bean class="org.springframework.core.io.FileSystemResource">
    <constructor-arg>
        <jee:jndi-lookup id="myProps" jndi-name="myProps" resource-ref="true" />
    </constructor-arg>
</bean>

但是,我想处理 jndi 查找失败但会依靠位于 WEB-INF/classes 文件夹中的默认文件的情况。如果我使用如下默认值,web应用程序会抛出异常,抱怨它找不到文件“classpath:myprops.properties”

<bean class="org.springframework.core.io.FileSystemResource">
    <constructor-arg>
        <jee:jndi-lookup id="myProps" jndi-name="myProps" resource-ref="true"
            default-value="classpath:myprops.properties" />
    </constructor-arg>
</bean>

但是,如果我硬编码默认值的特定路径,那么它工作正常,但是作为最终解决方案,这是不可接受的。

因此,我的问题是如何使用“classpath:”以便它得到正确解决?

这是我正在使用的总体用法:

<bean id="authServerProperties"
     class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="location">
        <bean class="org.springframework.core.io.FileSystemResource">
            <constructor-arg>
                <jee:jndi-lookup id="myProps" jndi-name="myProps" resource-ref="true"
                    default-value="classpath:myprops.properties" />
            </constructor-arg>
        </bean>
     </property>
     .....
</bean> 

I am really stuck on this one... Help! :)

I am using j2ee:jndi lookup for a property file. The following works fine:

<bean class="org.springframework.core.io.FileSystemResource">
    <constructor-arg>
        <jee:jndi-lookup id="myProps" jndi-name="myProps" resource-ref="true" />
    </constructor-arg>
</bean>

However, I want to handle the case where the jndi lookup fails but will fall back on a default file located in WEB-INF/classes folder. If I use the default-value as below, the webapp throws an exception complaining that it cannot find the file "classpath:myprops.properties"

<bean class="org.springframework.core.io.FileSystemResource">
    <constructor-arg>
        <jee:jndi-lookup id="myProps" jndi-name="myProps" resource-ref="true"
            default-value="classpath:myprops.properties" />
    </constructor-arg>
</bean>

However, if I hard-code a specific path for default-value, then it works fine, but that is unacceptable as a final solution.

Thus, my issue is how to use "classpath:" so that it gets properly resolved?

This is the overall usage I'm employing:

<bean id="authServerProperties"
     class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="location">
        <bean class="org.springframework.core.io.FileSystemResource">
            <constructor-arg>
                <jee:jndi-lookup id="myProps" jndi-name="myProps" resource-ref="true"
                    default-value="classpath:myprops.properties" />
            </constructor-arg>
        </bean>
     </property>
     .....
</bean> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愿与i 2024-12-30 05:08:08

让 Spring 使用其内置的 PropertyEditor 支持来决定资源的类型,而不是提供显式的 FileSystemResource bean,因为这不适用于类路径资源(它需要要配置文件系统上的路径)。相反,您应该使用类似的内容

<bean id="authServerProperties"
      class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="location" ref="myProps" />
</bean>

<jee:jndi-lookup id="myProps" jndi-name="myProps" resource-ref="true"
            default-value="classpath:myprops.properties"/>

,这里我们将位置设置为字符串值,并允许 Spring 将其转换为适当的资源类型,因此,如果您

<env-entry>
    <env-entry-name>myProps</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>file:///Users/something/myProps.properties</env-entry-value>
</env-entry>

web.xml 中有,它将使用UrlResource 与给定的文件 URL,否则它将创建一个 ClasspathResource 来查找文件 myprops.properties

Let Spring use its built-in PropertyEditor support to decide on the type of resource, rather than supplying an explicit FileSystemResource bean as this won't work with classpath resources (it needs to be configured with a path on the file system). Instead you should use something like

<bean id="authServerProperties"
      class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="location" ref="myProps" />
</bean>

<jee:jndi-lookup id="myProps" jndi-name="myProps" resource-ref="true"
            default-value="classpath:myprops.properties"/>

Here we are setting the location to be a string value and allowing Spring to convert that to the appropriate resource type, so if you have

<env-entry>
    <env-entry-name>myProps</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>file:///Users/something/myProps.properties</env-entry-value>
</env-entry>

in your web.xml, it will use a UrlResource with the given file URL, otherwise it will create a ClasspathResource to look for the file myprops.properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文