最后,我能够将 Spring 管理的 ThemeManagerService 直接提供给自定义后处理器,而不是依赖于自定义 UriLocator。我很早就尝试过,但是忘记在新的构造函数中调用 super(),所以处理器注册系统被破坏了。
我在注册 WRO bean 时将其 传递@Autowired
ThemeManagerService
给我:CustomConfigurableWroManagerFactory
@Autowired
ThemeManagerService themeManagerService;
@Bean
FilterRegistrationBean webResourceOptimizer(Environment env) {
FilterRegistrationBean fr = new FilterRegistrationBean();
ConfigurableWroFilter filter = new ConfigurableWroFilter();
Properties props = buildWroProperties(env);
filter.setProperties(props);
//The overridden constructor passes ThemeManager along
filter.setWroManagerFactory(new CustomConfigurableWroManagerFactory(props,themeManagerService));
filter.setProperties(props);
fr.setFilter(filter);
fr.addUrlPatterns("/wro/*");
return fr;
}
into的构造函数注入ThemeManagerService
意味着CustomConfigurableWroManagerFactory
它可以传递给自定义后处理器,因为它由以下方式注册contributePostProcessors
:
public class CustomConfigurableWroManagerFactory extends Wro4jCustomXmlModelManagerFactory {
private ThemeManagerService themeManagerService;
public CustomConfigurableWroManagerFactory(Properties props,ThemeManagerService themeManagerService) {
//forgetting to call super derailed me early on
super(props);
this.themeManagerService = themeManagerService;
}
@Override
protected void contributePostProcessors(Map<String, ResourcePostProcessor> map) {
//ThemeManagerService is provided as the custom processor is registered
map.put("repoPostProcessor", new RepoPostProcessor(themeManagerService));
}
}
现在,后处理器可以访问ThemeManagerService
:
@SupportedResourceType(ResourceType.CSS)
public class RepoPostProcessor implements ResourcePostProcessor {
private ThemeManagerService themeManagerService;
public RepoPostProcessor(ThemeManagerService themeManagerService) {
super();
this.themeManagerService = themeManagerService;
}
public void process(final Reader reader, final Writer writer) throws IOException {
String resourceText = "/* The custom PostProcessor fetched the following SASS vars from the ThemeManagerService: */\n\n";
resourceText += themeManagerService.getFormattedProperties();
writer.append(resourceText);
//read in the merged SCSS and add it after the custom content
writer.append(IOUtils.toString(reader));
reader.close();
writer.close();
}
}
到目前为止,这种方法正在按预期/预期工作。希望它对其他人有用。
Wro4j 是一个很棒的工具,非常感谢。