Advertisement
tpeierls

Injecting Restlet Components and Applications

Dec 14th, 2012
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.96 KB | None | 0 0
  1. // Sketch of how to use Guice to inject Restlet Components and Applications
  2. // to avoid explicitly having to pass FinderFactory around.
  3.  
  4. public class MyMain {
  5.     public static void main(String... args) {
  6.         // RestletGuice.createInjector ensures that FinderFactory is
  7.         // bound properly.
  8.         Injector inj = RestletGuice.createInjector(...my modules...);
  9.         inj.getInstance(MyComponent.class).start();
  10.     }
  11. }
  12.  
  13. public class MyComponent extends Component {
  14.     @Inject MyComponent(Application1 app1, Application2 app2) {
  15.         ...
  16.         host.attach("/app1", app1);
  17.         host.attach("/app2", app2);
  18.         ...
  19.     }
  20. }
  21.  
  22. public class Application1 extends Application {
  23.     private final FinderFactory ff;
  24.  
  25.     @Inject Application1(FinderFactory ff) {
  26.         this.ff = ff
  27.     }
  28.  
  29.     @Override public Restlet createInboundRoot() {
  30.         ...
  31.         router.attach("/res", ff.finder(MyResource.class));
  32.         ...
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement