1

wro4j ドキュメントの助けを借りて、カスタム ポスト プロセッサ フィルターの実装に成功しました。

その仕事は、SASS vars を生成して SASS ファイルのグループの先頭に追加することであり、それがトランスパイルのために ruby​​SassCss フィルターに渡されます。この仕事はうまくいっています。

ThemeManager @Service問題は、SASS vars を決定するジョブをSpring によって管理されるカスタムに渡したかったことです。フィルターが自動配線された @Service を認識できないとは考えていませんでしたが、そのようです。

コントローラーに接続@Autowireする@Serviceと正常に動作しますが、フィルターで同じことをしようとすると、使用しようとすると NPE が発生します。

フィルターに見えるようにする方法はあり@Serviceますか、それとも間違った方法でアプローチしていますか?

助けてくれてありがとう。

アップデート:

多くの角度からの実行と攻撃が必要でしたが、WRO filterRegistrationBean Bean があるアプリ構成に themeManagerService を自動配線することに成功しているようです。次に、themeManagerService Bean を 2 番目の引数としてカスタム ConfigurableWroManagerFactory に渡します。

カスタム WroManagerFactory に存在することは、その themeManagerService を引数として取るカスタム UriLocator への参照です。カスタム UriLocator は、グループ内の任意のキーワードを含む CSS リソースによって呼び出されます。

新しい UriLocator は、themeManagerService が提供するものから ByteArrayInputStream を生成し、パイプラインに渡すことができます。

単純。

このアプローチがパン/フィズルアウトしたら、フォローアップします。

4

1 に答える 1

1

最終的に、カスタム UriLocator に依存するのではなく、Spring 管理の ThemeManagerService をカスタム ポスト プロセッサに直接提供することができました。私は早い段階でそれを試みましたが、新しいコンストラクターで super() を呼び出すのを忘れていたため、プロセッサ登録システムが壊れていました。

WRO Bean を登録するときに 、@Autowired ThemeManagerServiceを myに渡します。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;
}

へのコンストラクター注入は、次のように登録されているカスタム ポストプロセッサーに渡すことができることを意味しThemeManagerServiceます。CustomConfigurableWroManagerFactorycontributePostProcessors

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 は優れたツールであり、高く評価されています。

于 2016-01-17T21:23:38.133 に答える