You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Alexander Broekhuis <a....@gmail.com> on 2021/06/25 05:18:49 UTC
DependencyManager composition and additional dependencies with annotations
Hi all,
I am using the DependencyManager (not SCR), and am wondering if with
composition it is possible to add additional dependencies in the other
classes of a component?
In my tests this does not work, but perhaps I am missing something?
Eg:
@Component
class MyComponent {
MyComposition myComp = new MyComposition(this);
@Composition
getComposition() {
return new Object() { this, myComp };
}
// ...
}
class MyComposition {
// new dependency, not present in MyComponent, this does not seem to work
@ServiceDependency(service = MyService.class)
void addMyService(MyService service) {
// ...
}
}
--
Met vriendelijke groet,
Alexander Broekhuis
Re: DependencyManager composition and additional dependencies with annotations
Posted by Pierre De Rop <pi...@gmail.com>.
Hello Alexander,
When you use DependencyManager service composition, only the top level
component dependencies (and lifecycle callbacks) are propagated to the
composites, which must not be annotated. They only need to define the same
method callback names than the ones specified in the top level MyComponent
(optionally).
So, in your example, if your MyComposition instance needs to get injected
with the MyService , you will need to define the annotation on the top
level MyComponent (and possibly define an empty method in case the
MyComponent does not care about the MyService). Then just define the same
bind method on the MyComposition class.
Notice that all lifecycle callbacks are also propagated
(like @Start, @Stop), and the Composition @Start methods can also return a
map in order to propagate more service properties.
Please check
https://felix.apache.org/documentation/subprojects/apache-felix-dependency-manager/reference/dm-annotations.html#component-composition
I may have to update the documentation, which is not enough specific, I'm
sorry about that.
So, let's take your original example:
@Component
public class MyComponent {
private final MyComposition myComposition = new MyComposition();
* @ServiceDependency void bind(MyService service) { // Empty
method, we don't care, but all composites may then optionally define the
bind(MyService ) method if, they need the MyService }*
@Start
void start() {
System.out.println("MyComponent.start: ");
}
@Composition
Object[] getComposition() {
return new Object[] { this, myComposition };
}
}
and now, the MyComposition:
public class MyComposition {
* void bind(MyService service) {
System.out.println("MyComposition.bindService: " + service); } *
}
if you have another Composite, and if this one does not case about the
MyService, then it can just not define the bind(MyService) method.
Notice that the MyComposition can also define the start lifecycle callback
(which is only annotated on the top level MyComponent):
public class MyComposition {
void bind(MyService service) {
System.out.println("MyComposition.bindService: " + service);
}
void start() {
System.out.println("MyComposition.start: ");
}
}
hope it helps
kind regards
/pierre
On Fri, Jun 25, 2021 at 7:19 AM Alexander Broekhuis <a....@gmail.com>
wrote:
> Hi all,
>
> I am using the DependencyManager (not SCR), and am wondering if with
> composition it is possible to add additional dependencies in the other
> classes of a component?
>
> In my tests this does not work, but perhaps I am missing something?
>
> Eg:
>
> @Component
> class MyComponent {
>
> MyComposition myComp = new MyComposition(this);
>
> @Composition
> getComposition() {
> return new Object() { this, myComp };
> }
>
> // ...
> }
>
> class MyComposition {
>
> // new dependency, not present in MyComponent, this does not seem to work
> @ServiceDependency(service = MyService.class)
> void addMyService(MyService service) {
> // ...
> }
> }
>
> --
> Met vriendelijke groet,
>
> Alexander Broekhuis
>