【问题标题】:access via spring injected property which references a directory to access its files通过 spring 注入属性访问,该属性引用目录以访问其文件
【发布时间】:2014-10-13 14:30:14
【问题描述】:

我正在尝试访问在 bean 中定义的属性,如下所示:

<bean id="reportdepositService" class="a.b.c.ServiceImpl">
    <property name="reportDeposit" value="/WebContent/WEB-INF/dirName/" />
</bean>

ServiceImpl 类如下所示:

public class ServiceImpl implements IService {

private Resource springResource;
public Resource getSpringResource() {
    return springResource;
}

public void setSpringResource(Resource springResource) {
    this.springResource = springResource;
}

private File getSpringResourceFile() throws IOException{
    Resource r = getSpringResource();
    URL url = FileLocator.resolve(r.getURL());
    return FileUtils.toFile(url);
}

public void doSomething(){

.. some logic .
File f = getSpringResourceFile();

}

在 ubuntu 机器上的 eclipse 中执行该代码可以正常工作,在 jenkins 上构建的应用程序在 ubuntu 上也可以正常工作。在 win7/64 上运行该应用程序,代码抛出以下异常:

   OSGi resource[/WebContent/WEB-INF/springResource/|bnd.id=332|bnd.sym=a.b.server] cannot be resolved to URL because it does not exist
java.io.FileNotFoundException: OSGi resource[/WebContent/WEB-INF/springResource/|bnd.id=332|bnd.sym=a.b.server] cannot be resolved to URL because it does not exist
    at org.springframework.osgi.io.OsgiBundleResource.getURL(OsgiBundleResource.java:228)

访问 Windows 托管系统上的属性需要什么? 有任何想法吗? 提前致谢

【问题讨论】:

    标签: java windows spring tomcat osgi


    【解决方案1】:

    我不确定你是否走在正确的道路上:

    /WebContent/WEB-INF 路径表明您正在编写要在 Web 容器中运行的 Web 应用程序。在这种情况下,您应该永远假设您的资源是一个文件。您应该使用Resource.getInputStream() 打开资源,而不是使用 URL/文件。原因是应用程序很可能直接从 .war 运行,而没有可用的文件系统。

    这可能会立即回答这个问题:Windows 7 环境与运行环境是否相同?我的印象是它不是。如果您在将项目传输到 Windows 机器时将项目捆绑到一个 bundle jar 中,我想您应该在路径中添加一个前缀(但可能需要关闭 WebContent),例如 bundle:、classpath: 等。参见@987654321 @ 参考。但是您需要提供更多信息才能确定。

    【讨论】:

      猜你喜欢
      • 2011-03-03
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      相关资源
      最近更新 更多