On Thu, Oct 15, 2015 at 1:19 AM, Debajit Adhikary <[email protected]> wrote: > Thanks for the suggestion, that worked. > > Changing the @Inject to use the @Role (interface) instead of the @Component > (the concrete class) fixed the issue :) Thanks for your help :) > > Out of curiosity, how does the Component manager know which concrete > implementation to pick? Since I have only one component, does it > automatically use that for the @Inject?
Basically yes. When you have several components implementing the same role you are supposed to indicate a hint using @Named annotation (both in the with the @Component and the @Inject). > > > > On Wed, Oct 14, 2015 at 2:02 AM, Thomas Mortagne <[email protected]> > wrote: > >> On Wed, Oct 14, 2015 at 9:11 AM, [email protected] <[email protected]> >> wrote: >> > >> > >> > On 14 Oct 2015 at 09:00:15, [email protected] ([email protected] >> (mailto:[email protected])) wrote: >> > >> >> >> >> >> >> On 14 Oct 2015 at 06:43:36, Marius Dumitru Florea ( >> [email protected](mailto:[email protected])) >> wrote: >> >> >> >> > Hi Debajit, >> >> > >> >> > On Oct 14, 2015 01:17, "Debajit Adhikary" wrote: >> >> > > >> >> > > I created an XWiki component which is available as a jar file. >> >> > > >> >> > > I have a ScriptService class for the component too: >> >> > > >> >> > > ---- >> >> > > >> >> > > @Role >> >> > > public interface SearchClient >> >> > > { >> >> > > String search(); >> >> > > } >> >> > > >> >> > > ---- >> >> > > >> >> > > @Component >> >> > > @Singleton >> >> > > public class InternalSearchClient implements SearchClient >> >> > > { >> >> > > public String search() >> >> > > { >> >> > > return "test search results"; >> >> > > } >> >> > > } >> >> > > >> >> > > ---- >> >> > > >> >> > > @Component >> >> > > @Named("internalSearch") >> >> > > @Singleton >> >> > > public class InternalSearchScriptService implements ScriptService >> >> > > { >> >> > >> >> > > @Inject >> >> > > private InternalSearchClient searchClient; >> >> > >> >> > You need to "inject" the role (interface), not directly the >> implementation. >> >> > That's why you use the Component Manager. Othewise you could have >> simply >> >> > used the "new" operator to instantiate yourself the component. >> >> >> >> Good catch, didn’t see it when I replied yesterday! :) >> >> >> >> I guess we could catch this in the AnnotationComponentLoader and report >> an error. >> > >> > Actually, the Component Manager should raise an error if it fails to >> inject a field. I was sure it was doing that, strange. I’ll debug it. >> >> It does. >> >> > >> > Thanks >> > -Vincent >> > >> >> Thanks >> >> -Vincent >> >> >> >> > Hope this helps, >> >> > Marius >> >> > >> >> > > >> >> > > public String search() >> >> > > { >> >> > > return this.searchClient.search(); >> >> > > } >> >> > > } >> >> > > >> >> > > ---- >> >> > > >> >> > > I've added all the above files into their own jar with a >> components.txt >> >> > > file: >> >> > > >> >> > > com.mycompany.wiki.search.InternalSearchClient >> >> > > com.mycompany.wiki.search.InternalSearchScriptService >> >> > > >> >> > > and in my Wiki's pom.xml, I have the following: >> >> > > >> >> > > >> >> > > org.xwiki.commons >> >> > > xwiki-commons-script >> >> > > ${commons.version} >> >> > > >> >> > > >> >> > > >> >> > > Now from my Wiki page, when I do the following: >> >> > > >> >> > > {{groovy}} >> >> > > println(services.internalSearch.search()) >> >> > > {{/groovy}} >> >> > > >> >> > > I get an error message saying "java.lang.NullPointerException: >> Cannot >> >> > > invoke method search() on null object" >> >> > > >> >> > > What am I doing wrong here? >> >> > > >> >> > > (I followed the directions in >> >> > > http://platform.xwiki.org/xwiki/bin/view/DevGuide/WritingComponents >> — have >> >> > > these directions changed, and/or is there anything else I need to >> do? ) >> >> >> > >> > _______________________________________________ >> > users mailing list >> > [email protected] >> > http://lists.xwiki.org/mailman/listinfo/users >> >> >> >> -- >> Thomas Mortagne >> _______________________________________________ >> users mailing list >> [email protected] >> http://lists.xwiki.org/mailman/listinfo/users >> > _______________________________________________ > users mailing list > [email protected] > http://lists.xwiki.org/mailman/listinfo/users -- Thomas Mortagne _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
