Java – what about Dagger’s custom range?

How to create a custom range with dagger?

Are there any guidelines? I didn't find them

I am developing vaadin application and need a custom scope Something like uiscoped

Best wishes

Solution

The dagger does not use the same mechanism as Guice Specifically, dagger does not transparently handle the scope made by Guice, using various scope annotations. An injector and different instances are cached behind the scenes Instead, it uses two principles First, @ singleton means "one per chart" (the strictest interpretation of jsr-330). Second, graphs can be linked in a hierarchy

Dagger uses this hierarchical associated graph tree. You can create a graph by adding more modules and extending it through the plus () method to create a "scope" graph with shorter life cycle This is similar to the children's syringe in Gu Zi An important principle here is that the instances in the extended graph can see the instances in the originating graph, but not vice versa Therefore, the concentricity of shortening the life cycle is mirrored in visibility – a shorter lived object can see (rely on) a longer lived object, but not vice versa So an object living in the life of a request can see an object living in the life of an application, but not the opposite

It is through this mechanism that people expect to adjust the scope of cache instances more narrowly

If a module is configured with a graph and has a singleton, it will cache an instance in the graph and provide it to all dependent objects If the extension of the graph is created through the plus () method, it will be configured with other modules containing @ singleton annotation binding. Then these other modules will be a graph and a living objectgraph instance

For example, let's simulate a server responding to a request. We want to live some objects in the life cycle of the application and some objects that live only within the service life of a request:

@Module()
public class MyAppModule {
  @Provides ConnectionDictonary connectionDictionary() {
    return new ConnectionDictonary(System.getProperty("some.property"));
  }

  /** Stateless mockable utilities for this app */
  @Provides Util util() { new Util(); }

  @Provides @Singleton DataStore store() { 
    return new DataStore();
  }

  @Provides @Singleton ConnectionPool pool(DataStore store,ConnectionDictionary dict) { 
    try {
      return DataStore.connectionPool(dict,System.getProperty("pool.size"));
    } catch (Exception e) {
      // bad bad bad
      throw new RuntimeException("Could not connect to datastore.",e);
    }
  }

}

// This module "adds to" MyAppModule by adding additional graph elements in
// an extended graph.
@Module(injects=MyRequestEndpoint.class,addsTo = MyAppModule.class)
public class MyRequestModule {
  private Request req;
  public MyRequestModule(Request req) { this.req = req; }

  @Provides @Singleton RequestObject request() { return req; }

  @Provides @Singleton User user(ConnectionPool pool,Request req,Util util) {
    try {
      Connection conn = pool.obtain();
      // getUser cannot throw null;
      return util.getUser(conn,req.get("user.id"),Crypto.hash(req.get("pass")));
    } catch (UserNotFoundException e) {
      return User.UNKNowN;
    } catch (Exception e) {
      throw new RuntimeException("Could not obtain a user.",e);
    } finally { 
      // TODO: try-with-resources in Java7
      pool.release();
    }
  }

}

public class MyRequestEndpoint {
  @Inject ConnectionPool pool;
  @Inject Request req;

  public Output performService() {
    try {
      Connection conn = pool.obtain();
      // ... does stuff with request
    } finally {
      conn.release();
    }
  }
}

public class MyApp {    
  public void main(String ... args) {
    graph = ObjectGraph.create(MyAppModule.class);
    new ServiceListener(graph).start(); 
  }
}

public ServiceListener {
  private final ObjectGraph appGraph;
  public ServiceListener(ObjectGraph appGraph) {
    this.appGraph = appGraph;
  }

  //... infrastructure for listening and building request/response objects,etc.

  public void serveRequest(Request req,Response res) {
    // Take the application-scoped graph and create another graph we will 
    // use in this request and throw away.
    ObjectGraph requestGraph = MyApp.graph().plus(new MyRequestModule(req));
    Output output = requestGraph.get(MyRequestEndpoint.class).performService();
    Util.populateResult(output,result);
    result.flush();
  }
}

In this example, each myrequestendpoint will get a shared instance of ConnectionPool, but one endpoint in any two requests will get two different requestobjects

This is a somewhat stupid example, built on top of the J2EE pattern This is not a simple thing. You won't build it like this. You need a more powerful scaffold to build a suitable server model In fact, the dagger project might do this (although I respectfully recommend using an injected service object and a scheduling servlet or filter)

But it wants to illustrate a narrow scope in a familiar model

The key is not in the annotation, but in the life of the drawing You can create a shorter life chart as a "child" or "extension" of the longer life chart The objects recorded in these drawings have a lifetime (or range) of drawing management objects

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>