Java 通过spring注入属性进行访问,该属性引用目录来访问其文件
我试图访问bean中定义的属性,如下所示:Java 通过spring注入属性进行访问,该属性引用目录来访问其文件,java,windows,spring,tomcat,osgi,Java,Windows,Spring,Tomcat,Osgi,我试图访问bean中定义的属性,如下所示: <bean id="reportdepositService" class="a.b.c.ServiceImpl"> <property name="reportDeposit" value="/WebContent/WEB-INF/dirName/" /> </bean> public class ServiceImpl implements IService { private Resource sp
<bean id="reportdepositService" class="a.b.c.ServiceImpl">
<property name="reportDeposit" value="/WebContent/WEB-INF/dirName/" />
</bean>
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计算机时将其捆绑到捆绑jar中,我想您应该在路径中添加前缀(但可能需要关闭WebContent),如bundle:、classpath:,等等。请参阅参考。但是你需要提供更多的信息来确定