如何将 spring 中的属性设置为 WEB-INF 内的路径名? [英] How to set a property in spring to a pathname inside of WEB-INF?

查看:11
本文介绍了如何将 spring 中的属性设置为 WEB-INF 内的路径名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个必须用绝对路径名初始化的类.我想用路径名初始化它的是一个位于 WEB-INF 中的文件.

I have a class that must be initialized with an absolute pathname. The thing I want to initialize it with the pathname of is a file sitting in WEB-INF.

我正在使用 Spring 的 ContextLoaderListener 将这一切设置为动态,因此我无法运行 Java 代码来从上下文获取路径并将其粘贴到 ${whatever} 可以找到的位置.

I am using the ContextLoaderListener from Spring to set this all into motion, so I can't run Java code to obtain the path from the context and stick it where a ${whatever} could find it.

考虑一些 bean 定义,例如:

Consider some bean definition like:

<bean class="my.class">
  <property name="somePath" value="/WEB-INF/a.txt"/>
</bean>

如果可能的话,我需要一种方法来使该路径名通过 ServletContextResource 机制.似乎没有像类路径这样的前缀":

I need a way, if possible, to make that pathname pass through the ServletContextResource mechanism. There doesn't seem to be a 'prefix' for those like classpath:

在这种情况下,将项目放在类路径中将无济于事,相信我.

In this case, it won't help to have the item in the classpath, trust me.

编辑:

我去挖了bean类的源码,它已经接受了相关属性上的Resources.所以这里发生了一些荒谬的事情,因为它抱怨好像找不到东西.帮我调试调试器.

I went and dug up the source of the bean class, and it already accepts Resources on the relevant properties. So Something Ridiculous is going on here, insofar as it complains as if it can't find things. Off to the debugger for me.

再次编辑:

所以,结果证明这是一个与 Spring 无关的专家恶作剧.到处都在为您的帮助点赞,并提出另一个问题.

So, this turns out to be a maven prank, unrelated to Spring. It's upvotes all around for your help, and open another question.

推荐答案

我的偏好是修改这个类以采用 Resource,而不是路径名.然后,您可以使用以下方法注入它:

My preference would be to modify this class to take a Resource, rather than a pathname. You can then inject it using:

<property name="fileResource" value="/WEB-INF/path/to/file"/>

这样更灵活,你可以使用Resource接口上的各种方法来获取底层文件路径名之类的东西,比如getFile().getAbsolutePath().

This is more flexible, and you can use the various methods on the Resource interface to get things like the underlying file pathname, such as getFile().getAbsolutePath().

但是,如果修改目标类不可行,那么您需要某种方式将Resource 转换为路径名.您可以为此使用 FactoryBean,例如:

However, if modifying the target class is not feasible, then you need some way of converting a Resource into a pathname. You could use a FactoryBean for this, something like:

public class ResourcePathFactoryBean extends AbstractFactoryBean<String> {

    private Resource resource;

    @Required
    public void setResource(Resource resource) {
        this.resource = resource;
    }

    @Override
    protected String createInstance() throws Exception {
        return resource.getFile().getAbsolutePath();
    }

    @Override
    public Class<?> getObjectType() {
        return String.class;
    }
}

然后您可以使用它来注入您的路径:

You can then use it to inject your path:

<bean id="myBean" class="com.MyBean">
   <property name="path">
      <bean class="com.ResourcePathFactoryBean">
         <property name="resource" value="/WEB-INF/path/to/file"/>
      </bean>
   </property>
</bean>

这篇关于如何将 spring 中的属性设置为 WEB-INF 内的路径名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆