Tomás Fernández Löbbe, il 29/08/2011 20:32, ha scritto:
You can use reflection to instantiate the correct object (specify the class
name on the parameter on the solrconfig and then invoke the constructor via
reflection). You'll have to manage the life-cycle of your object yourself.
If I understand your requirement, you probably have created a
SearchComponent that uses that "retriever", right?



sorry for the delay: I was experimenting.

Raw reflections do not suffice: you can't specify a dependency required by your reflection-constructed object

I've ended up using spring this way (will cut some code for brevity)

I've enabled spring the usual way, adding a ContextLoaderListener in web.xml and configuring the spring xml or java configuration files (I do java configuration)

I've declared a spring bean named myComponentDeclaredInTheSpringConf, that is an extension of SearchComponent, with it's collaborators

I've created SpringAwareSearchComponent, that is a delegate of SearchComponent

public SpringAwareSearchComponent() {
 this.ctx = ContextLoader.getCurrentWebApplicationContext();
}
...
public void init(NamedList args) {
 super.init(args);
inner = ctx.getBean(args.get("__beanname__").toString(), SearchComponent.class);
 inner.init(args);
}

public void prepare(ResponseBuilder rb) throws IOException {
 inner.prepare(rb);
}

public void process(ResponseBuilder rb) throws IOException {
 inner.process(rb);
}

In solrconfig.xml I've declared the search component as
<searchComponent name="myComponent" class="SpringAwareSearchComponent">
 <str name="__beanname__">myComponentDeclaredInTheSpringConf</str>
 ...other bean specific parameters
</searchComponent>

and added myComponent to the list of search components

And it works like a charm. Maybe I can implement some other solr class delegate and add hooks between spring and solr as needed

any comment will be appreciated

best regards

federico

Reply via email to