我真的被这个问题困住了......救命! :)
我正在使用 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web
技术交流群。
我真的被这个问题困住了......救命! :)
我正在使用 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让 Spring 使用其内置的
PropertyEditor
支持来决定资源的类型,而不是提供显式的FileSystemResource
bean,因为这不适用于类路径资源(它需要要配置文件系统上的路径)。相反,您应该使用类似的内容,这里我们将位置设置为字符串值,并允许 Spring 将其转换为适当的资源类型,因此,如果您
的
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 explicitFileSystemResource
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 likeHere 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
in your
web.xml
, it will use aUrlResource
with the given file URL, otherwise it will create aClasspathResource
to look for the filemyprops.properties
.