2010-05-14 38 views
2

我在基於Spring的應用程序中嵌入了Jetty。我在Spring上下文文件中配置我的Jetty服務器。我遇到的麻煩的配置的特定部分是這樣的:嵌入式Jetty資源庫類路徑URL

<bean class="org.eclipse.jetty.webapp.WebAppContext"> 
    <property name="contextPath" value="/" /> 
    <property name="resourceBase" value="????????" /> 
    <property name="parentLoaderPriority" value="true" /> 
</bean> 

如果你看到上面的,在這裏我已經把????????,我非常希望resourceBase引用一個我的類路徑中的文件夾。我將我的應用程序部署在一個可執行的JAR文件中,並在我的類路徑中有一個文件夾config/web/WEB-INF

Jetty似乎能夠處理在resourceBase中定義的URL(例如jar:file:/myapp.jar!/config/web),但它似乎不支持類路徑URL。如果我定義諸如classpath:config/web之類的東西,我會得到IllegalArgumentException。

這對我來說是一個真正的痛苦。有沒有人知道要實現這個功能?

感謝,

安德魯

回答

5

你需要讓你的資源作爲一個Spring的Resource,並在其上調用getURI().toString(),這樣的事情:

public class ResourceUriFactoryBean extends AbstractFactoryBean<String> { 

    private Resource resource; 

    public ResourceUriFactoryBean(Resource resource) { 
     this.resource = resource; 
    } 

    @Override 
    protected String createInstance() throws Exception { 
     return resource.getURI().toString(); 
    } 

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

-

<property name="resourceBase"> 
    <bean class = "com.metatemplating.sample.test.ResourceUriFactoryBean"> 
     <constructor-arg value = "classpath:config/web" /> 
    </bean> 
</property> 

-

或者更優雅的方式與Spring 3.0的表達式語言:

<property name="resourceBase" 
    value = "#{new org.springframework.core.io.ClassPathResource('config/web').getURI().toString()}" /> 
+3

尼斯之一。如果你不使用Spring,只需使用SomeClass.class.getResource(「/ config/web」)。toURI()。toString() – 2011-01-31 06:13:59