As some of you might know e(fx)clipse provides a Eclipse DI extension supporting more powerful feature when we deal with OSGi-Services:
- Support for dynamics (eg if a higher-ranked service comes along you get it injected, …)
- Support for service list
- ServiceFactory support because the request is made from the correct Bundle
Since tonights build the @Service
annotation has support to define:
- A static compile time defined LDAP-Filter expression
public class MySQLDIComponent { @Inject public void setDataSource( @Service(filterExpression="(dbtype=mysql)") DataSource ds) { // ... } } public class H2DIComponent { @Inject public void setDataSource( @Service(filterExpression="(dbtype=h2)") DataSource ds) { // ... } }
- A dynamic LDAP-Filter expression who is calculated at runtime and can change at any time
public class CurrentDatasource extends BaseValueObservable<String> implements OString { @Inject public CurrentDatasource( @Preference(key="database",defaultValue="h2") String database) { super("(dbtype="+database+")"); } @Inject public void setDatabase( @Preference(key="database",defaultValue="h2") String database) { setValue("(dbtype="+database+")"); } } public class DIComponent { @Inject public void setDataSource( @Service(dynamicFilterExpression=CurrentDatasource.class) DataSource ds) { // ... } }
You notice the dynamic provider itself if integration fully into the DI-Story 😉
Pingback: OSGi Declarative Services news in Eclipse Oxygen | vogella blog