Wro4j: Accessing Spring @Service from custom post processor -
i've implemented custom post processor filter of wro4j documentation.
its job generate , prepend sass vars group of sass files handed off rubysasscss filter transpiling, , it's doing job well.
the problem wanted hand job of determining sass vars off custom thememanager @service managed spring. hadn't considered filter wouldn't able see autowired @service seems case.
when @autowire @service controller, works fine, when try same thing filter npe when attempting use it.
is there way make @service visible filters or approaching wrong way?
thanks help.
update:
it's taken doing , attacking lot of angles, seem having success autowiring thememanagerservice app configuration have wro filterregistrationbean bean. pass thememanagerservice bean second argument custom configurablewromanagerfactory.
living in custom wromanagerfactory reference custom urilocator, takes thememanagerservice argument. custom urilocator invoked css resource containing arbitrary keyword within group.
the new urilocator able generate bytearrayinputstream thememanagerservice provides , pass pipeline.
simple.
i'll follow when approach pans/fizzles out.
in end, able provide spring managed thememanagerservice directly custom post processor, rather relying on custom urilocator. had tried on, forgot call super() in new constructor, processor registration system breaking.
i pass @autowired thememanagerservice customconfigurablewromanagerfactory when registering wro bean:
@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; } the constructor injection of thememanagerservice customconfigurablewromanagerfactory means can passed along custom postprocessor it's registered contributepostprocessors:
public class customconfigurablewromanagerfactory extends wro4jcustomxmlmodelmanagerfactory { private thememanagerservice thememanagerservice; public customconfigurablewromanagerfactory(properties props,thememanagerservice thememanagerservice) { //forgetting call super derailed me on super(props); this.thememanagerservice = thememanagerservice; } @override protected void contributepostprocessors(map<string, resourcepostprocessor> map) { //thememanagerservice provided custom processor registered map.put("repopostprocessor", new repopostprocessor(thememanagerservice)); } } now, post processor has access 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 = "/* custom postprocessor fetched following sass vars thememanagerservice: */\n\n"; resourcetext += thememanagerservice.getformattedproperties(); writer.append(resourcetext); //read in merged scss , add after custom content writer.append(ioutils.tostring(reader)); reader.close(); writer.close(); } } this approach working expected/intended far. hope comes in handy else.
wro4j great tool , appreciated.
Comments
Post a Comment