【问题标题】:Spring + Felix war = FileNotFoundExceptionSpring + Felix 战争 = FileNotFoundException
【发布时间】:2012-03-02 04:48:02
【问题描述】:

是否可以在 felix 的 war 包中使用 spring?我在 glassfish 3.1 上使用 spring 3.0.5 和 felix

我尝试在 felix 的 OSGI 战争包中输入 component-scan 标签,但出现以下异常。

我看到 Equinox 解决了 similar bug,那么 felix 呢?是否有解决此问题的解决方法或解决方案?

P.S:如果我在 web.xml contextConfigLocation 中用 * 定义路径,也会抛出同样的异常,例如:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/*.xml</param-value>
</context-param>

例外:

2012-02-08 18:30:23,194 [pool-28-thread-1] (PathMatchingResourcePatternResolver.java:532) WARN - Cannot search for matching files underneath URL [bundle://275.0:2/examples/services/] because it does not correspond to a directory in the file system 
java.io.FileNotFoundException: URL [bundle://275.0:2/examples/services/] cannot be resolved to absolute file path because it does not reside in the file system: bundle://275.0:2/examples/services/ 
at org.springframework.util.ResourceUtils.getFile(Res ourceUtils.java:204) 
at org.springframework.core.io.AbstractFileResolvingR esource.getFile(AbstractFileResolvingResource.java :52) 
at org.springframework.core.io.UrlResource.getFile(Ur lResource.java:168) 
at org.springframework.core.io.support.PathMatchingRe sourcePatternResolver.doFindPathMatchingFileResour ces(PathMatchingResourcePatternResolver.java:528) 
... 
2012-02-08 18:30:23,194 [pool-28-thread-1] (PathMatchingResourcePatternResolver.java:353) DEBUG - Resolved location pattern [classpath*:examples/services/**/*.class] to resources [] 

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx">

    <context:annotation-config/>

    <tx:annotation-driven />

    <context:component-scan base-package="examples.services" />

</beans>

谢谢你的提示

【问题讨论】:

    标签: spring osgi glassfish-3 apache-felix


    【解决方案1】:

    如果您不介意将自己与 Felix 绑定,则可以使用更优雅的解决方案。

    package org.eclipse.core.runtime;
    
    import java.net.URL;
    
    import org.apache.felix.framework.BundleRevisionImpl;
    import org.osgi.framework.Bundle;
    import org.osgi.framework.FrameworkUtil;
    
    public class FileLocator {
        private FileLocator() {}
    
        public static URL resolve(URL url) {
            if(url == null) {
                return null;
            }
            Bundle bundle = FrameworkUtil.getBundle(FileLocator.class).getBundleContext().getBundle();
            BundleRevisionImpl bundleRevision = bundle.adapt(BundleRevisionImpl.class);
            return bundleRevision.getLocalURL(url.getPort(), url.getPath());
        }
    }
    

    这要求您在 Felix 启动时使用以下参数导出框架:

    -Dorg.osgi.framework.system.packages.extra=org.apache.felix.framework
    

    【讨论】:

      【解决方案2】:

      我找到了两种解决方法。不过我很高兴听到 felix 的真正解决方案。

      解决方法是:

      1. 使用春分代替 felix :)
      2. (丑陋的)创建一个临时文件夹并将所有类解压缩到其中。用函数创建一个类 org.eclipse.core.runtime.FileLocator

      公共静态 URL 解析(URL url)抛出 IOException

      并返回该文件夹中内容的url,例如:

          String path = url.getPath(); 
          String str;
          if(path.contains("com")){
              str = path.substring(path.indexOf("com"));
          }
          return new URL("file:\\c:\\temp_classes\\"+str);
      

      【讨论】:

        猜你喜欢
        • 2012-04-29
        • 2018-09-01
        • 2012-10-29
        • 2022-11-10
        • 2019-03-23
        • 2011-10-20
        • 2020-02-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多