嵌入式Jetty resourceBase类路径URL

Dre*_*ter 2 spring jetty embedded-jetty

我在一个基于Spring的应用程序中嵌入了Jetty.我在Spring上下文文件中配置我的Jetty服务器.我遇到问题的配置的具体部分是这样的:

<bean class="org.eclipse.jetty.webapp.WebAppContext">
   <property name="contextPath" value="/" />
   <property name="resourceBase" value="????????" />
   <property name="parentLoaderPriority" value="true" />
</bean>
Run Code Online (Sandbox Code Playgroud)

如果你看到上面我已经放了????????,我理想地希望resourceBase引用我的类路径上的文件夹.我正在一个可执行的JAR文件中部署我的应用程序,并config/web/WEB-INF在我的类路径上有一个文件夹.

Jetty似乎能够处理resourceBase中定义的URL(例如jar:file:/myapp.jar!/config/web),但它似乎不支持类路径URL.如果我定义类似的东西,我会得到一个IllegalArgumentException classpath:config/web.

这对我来说真的很痛苦.有没有人知道要实现这个功能?

谢谢,

安德鲁

axt*_*avt 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;
    }    
}
Run Code Online (Sandbox Code Playgroud)

-

<property name="resourceBase">
    <bean class = "com.metatemplating.sample.test.ResourceUriFactoryBean">
        <constructor-arg value = "classpath:config/web" />
    </bean>
</property>
Run Code Online (Sandbox Code Playgroud)

-

或者使用Spring 3.0的表达式语言更优雅的方法:

<property name="resourceBase" 
    value = "#{new org.springframework.core.io.ClassPathResource('config/web').getURI().toString()}" /> 
Run Code Online (Sandbox Code Playgroud)

  • 好一个.如果你没有使用Spring,只需使用SomeClass.class.getResource("/ config/web").toURI().toString() (3认同)