Making @Service annotation even cleverer


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 😉

Advertisements
This entry was posted in e(fx)clipse. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s