Wro4j自定义XML模型位置



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

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

<<p> 必要代码/strong>

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>

中:

<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/如何使用错误的xml自定义位置?

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

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

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

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

ClassLoader.getSystemResourceAsStream(resourceLocation);

或者你可以使用ClasspathUriLocator:

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

:显然,这个例子发现了一个问题,描述如下:在准备好修复之前,可以使用以下选项:

  • 选项1
  • 选项2

相关内容

  • 没有找到相关文章

最新更新