嵌入式 Jetty 资源基类路径 URL

发布于 2024-09-01 08:54:17 字数 703 浏览 2 评论 0原文

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

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

如果你看到上面,我把 ????????? 放在哪里,我理想地希望资源库引用我的类路径上的文件夹。我将应用程序部署在单个可执行 JAR 文件中,并在类路径上有一个文件夹 config/web/WEB-INF 。

Jetty 似乎能够处理在资源库中定义的 URL(例如 jar:file:/myapp.jar!/config/web),但它似乎不支持类路径 URL。如果我定义类似 classpath:config/web 的内容,我会收到 IllegalArgumentException。

这对我来说是一个真正的痛苦。有谁知道如何实现此功能?

谢谢,

安德鲁

I'm embedding Jetty in a Spring based application. I configure my Jetty server in a Spring context file. The specific part of the configuration I'm having trouble with is this:

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

If you see above, where I've put the ????????, I ideally want the resourceBase to reference a folder on my classpath. I'm deploying my application in a single executable JAR file and have a folder config/web/WEB-INF on my classpath.

Jetty seems to be able to handle URLs defined in the resourceBase (e.g. jar:file:/myapp.jar!/config/web) but it doesn't seem to support classpath URLs. I get an IllegalArgumentException if I define something like classpath:config/web.

This is a real pain for me. Does anyone know of anyway to achieve this functionality?

Thanks,

Andrew

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2024-09-08 08:54:17

您需要将资源作为 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()}" /> 

You need to get your resource as a Spring's Resource and call getURI().toString() on it, something like this:

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>

-

Or more elegant approach with Spring 3.0's expression language:

<property name="resourceBase" 
    value = "#{new org.springframework.core.io.ClassPathResource('config/web').getURI().toString()}" /> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文