java - 通过 spring 注入(inject)属性访问,该属性引用目录以访问其文件

标签 java windows spring tomcat osgi

我正在尝试像这样访问在 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 托管系统上的属性需要什么? 有任何想法吗? 提前致谢

最佳答案

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

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

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

关于java - 通过 spring 注入(inject)属性访问,该属性引用目录以访问其文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26342438/

相关文章:

java - Android 按钮崩溃

java - 为什么 hibernate 在更新事务期间调用删除?

java - 当我在此 Spring 应用程序中使用 JdbcTemplate BeanPropertyRowMapper 类时,为什么会出现此 InstantiationException?

c# - 更改 EnableDesktopModeAutoInvoke 注册表值并使 TabTip.exe 接受更改

java - 如何模拟 jdbcTemplate.execute(callableStatementCreator, callableStatementCallback);

java - 你如何在 Spring 中处理两个同名的 bean?

java - 如何让 Spring 忽略 @autowired jmsTemplate 字段以进行测试?

java - 如果本地字段名称与传入数据的字段名称相同,是否需要 SerializedName 注释?

windows - 如何让 Oracle vm VirtualBox 6.0 和 wsl 同时工作

.net - 我应该将敏感数据传递给 .NET 中的 Process.Start 调用吗?