Changes for page Creating XWiki Components

Last modified by Simon Urli on 2023/10/10

<
From version < 51.1 >
edited by slauriere
on 2012/10/08
To version < 52.1 >
edited by Thomas Mortagne
on 2012/11/16
>
Change comment: There is no comment for this version

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.slauriere
1 +XWiki.ThomasMortagne
Content
... ... @@ -200,6 +200,11 @@
200 200   @Inject
201 201   private HelloWorld helloWorld;
202 202  
203 + /** Will be injected by the component manager */
204 + @Inject
205 + @Named("database")
206 + private HelloWorld databaseWorld;
207 +
203 203   [...]
204 204  }
205 205  {{/code}}
... ... @@ -235,6 +235,9 @@
235 235  {{code}}
236 236  HelloWorld greeter = Utils.getComponent(HelloWorld.class);
237 237  greeter.sayHello();
243 +
244 +HelloWorld databaseGreeter = Utils.getComponent(HelloWorld.class, "database");
245 +greeter.sayHello();
238 238  {{/code}}
239 239  
240 240  Note that, even if, in fact, the object returned by this function is an instance of the DefaultHelloWorld, you should **never declare your object of the implementation type nor cast to implementation instead of interface**. A component is represented by its interface, the implementation for such a service can be provided by any code, any class so relying on the implementation type is neither good practice (since the interface contract should be enough for a component), nor safe. In the future, a maven enforcer plugin will be setup in the build lifecycle, so that any reference to component implementations (located in an "internal" subpackage) will cause build errors.

Get Connected