2

我已经投入wro.xml了,src/main/resources因为还有一些其他资源,并且在单元测试中更容易访问它们。

我现在需要扩展一些 wro 类,以便能够从另一个地方读取模型,但不能让它工作。

必要的代码

网页.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
    <filter-name>WebResourceOptimizer</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
        <param-name>targetBeanName</param-name>
        <param-value>wroFilter</param-value>
    </init-param>
    <init-param>
        <param-name>targetFilterLifecycle</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>WebResourceOptimizer</filter-name>
    <url-pattern>/resources/*</url-pattern>
</filter-mapping>

应用程序上下文.xml:

<bean id="wroFilter" class="ro.isdc.wro.http.ConfigurableWroFilter">
    <property name="properties" ref="wroProperties" />
</bean>

<bean id="wroProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value="classpath:wro.properties" />
</bean>

wro.properties:

managerFactoryClassName=com.example.web.wro.manager.factory.MyWroManagerFactory;
preProcessors=cssUrlRewriting,cssImport,semicolonAppender,lessCss
postProcessors=cssMin,jsMin
debug=true

MyWroManagerFactory:

public class MyWroManagerFactory extends CopyrightKeeperConfigurableWroManagerFactory {
    private static final Logger LOG = LoggerFactory.getLogger(MyWroManagerFactory.class);

    @Override
    protected WroModelFactory newModelFactory() {
        LOG.debug("Load wro.xml directly from classpath");
        return new XmlModelFactory() {
            @Override
            protected InputStream getModelResourceAsStream() throws IOException {
                final String resourceLocation = getDefaultModelFilename();
                final InputStream stream = getClass().getResourceAsStream(resourceLocation);

                if (stream == null) {
                    throw new IOException("Invalid resource requested: " + resourceLocation);
                }

                return stream;
            }
        };
    }
}

版权所有KeeperConfigurableWroManagerFactory:

public class CopyrightKeeperConfigurableWroManagerFactory extends ConfigurableWroManagerFactory {
    private static final Logger LOG = LoggerFactory.getLogger(CopyrightKeeperConfigurableWroManagerFactory.class);

    private static final String[] PROCESSORS = {
        CssImportPreProcessor.ALIAS,
        JawrCssMinifierProcessor.ALIAS,
        CssMinProcessor.ALIAS,
        JSMinProcessor.ALIAS
    };

    @Override
    protected void contributePreProcessors(final Map<String, ResourcePreProcessor> map) {
        for (String processor : PROCESSORS) {
            if (map.containsKey(processor)) {
                LOG.debug("Apply CopyrightKeeperProcessorDecorator on " + processor);
                map.put(processor, CopyrightKeeperProcessorDecorator.decorate(map.get(processor)));
            }
        }
    }
}

为什么找不到classes/wro.xml/如何为 wro.xml 使用自定义位置?

编辑 这是完整的日志输出: http: //pastebin.com/NeNy1NH4

4

1 回答 1

1

问题是您正在加载相对于 MyWroManagerFactory 类的模型:

 final InputStream stream = getClass().getResourceAsStream(resourceLocation);

这意味着它将在类所在的文件夹中查找模型。由于您的 wro.xml 位于 classes 文件夹(这是 classpath 的根目录),您应该使用以下内容:

ClassLoader.getSystemResourceAsStream(resourceLocation);

或者,您可以使用 ClasspathUriLocator:

new ClasspathUriLocator().locate("classpath:" + resourceLocation)

已编辑:显然此示例发现了以下问题中描述的问题:在修复准备好之前,以下选项可用:

于 2013-05-29T14:08:27.730 回答