简体   繁体   中英

Embedded Jetty resourceBase classpath URL

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 (eg 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

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()}" /> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM