- ".target". , , impl=1. impl=2.
@Component(property="impl=1")
public class MyServiceImpl1 implements MyService {
}
@Component(property="impl=2")
public class MyServiceImpl2 implements MyService {
}
:
@Component
public class MyComponent {
@Reference(target="(impl=1)")
volatile MyService myService;
}
1 2 , MyComponent myService.target . ( OSGi.)
1 2 ( select) MyComponent, . . MyComponent , select impl 1, 2? ,
@Designate( ocd=Config.class )
@Component( property = "select=1" )
public class MyComponent {
static Class<?> types [] = {
MyServiceImpl1.class,
MyServiceImpl2.class,
};
@interface Config {
int select() default 1;
}
@Reference(target="(|(impl=1)(impl=2))")
volatile List<MyService> candidates;
volatile MyService selected;
@Activate
@Modify
void changed( Config config ) {
Class<?> type = types[config.select()];
Optional<MyService> service = candidates.
stream().
filter( type::isInstance ).
findFirst();
this.service = service.isPresent() ? service.get() : null;
}
}
, , , . .
, , , . , , MyServiceImpl2 MyServiceImpl1 - , .
, , , 1 2 ? ?
( : )