Wro4j自定义XML模型位置

时间:2013-05-29 13:43:34

标签: wro4j

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

我现在需要扩展一些wro类,以便能够从其他地方读取模型,但无法使其正常工作。

必要代码

的web.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>

的applicationContext.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;
            }
        };
    }
}

CopyrightKeeperConfigurableWroManagerFactory:

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

1 个答案:

答案 0 :(得分:1)

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

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

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

ClassLoader.getSystemResourceAsStream(resourceLocation);

或者你可以使用ClasspathUriLocator:

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

<强> EDITED : 显然,此示例发现了以下issue中描述的问题: 在修复准备就绪之前,可以使用以下选项: