Change comment:
There is no comment for this version
Summary
-
Page properties (2 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. slauriere1 +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.