java - Can Guice inject something that requires a lookup function? -


I am using Guice in a restoration web server, and there is a pattern that I can not understand: injecting objects For those who are specific to a specific user or a specific request.

We request to list all the widgets related to a project. The widget viewing service requires a project instance. There are many projects in the system.

My code currently looks like this:

  increases the public class widgetResource ServerResource {// path: project / {project} / widgets Private Last Widget Service Service ; Private Final Project Lookup Project Lookup; @ Public Widget Resource (Widget Service Service, Project Lookup Project Lookup) {this.service = service; This.projectLookup = projectLookup; } @ Get public widget collection getWidgets () {string projectname = getAttribute ("project"); // Restart the launch of Path var project project = projectLookup.get (projectName); WidgetCollection Widgets = service.getWidgetsFor (project); Visits Returns; }}   

It works well, but it is clumsy, and I hope there is a better way. It would be good to inject right project objects directly. Is there any way of doing it?

So far, I have discovered Assisted injection, which gives a factory object similar to my lookup. I came close to a reply with custom annotation / injection, but the dead ended because Restlet features have not been popped up after injection. Have you read the GitHub document and the user's guide?

I want to end up with something like this:

  public class WidgetResource ServerResource Extended {Private Last Widget Service Service; @ Public WidgetResource (widget service service) {this.service = service; } Get @inject @ public widget GetWidgets (@ path name ("project") project project) {WidgetCollection widgets = service.getWidgetsFor (project); Visits Returns; }   

with (of course) provides a method in a configuration that will look at the path variable and use lookup. However, I can not understand the method of assigning the path as the name of the path or the resource example as a variable. Is this possible? Appreciate any help!

From your point of view: You can not inject "getWidgets" ... Injection is your widget Your widget receipts have been created immediately after the start of the resource.

In addition, it looks perfectly fine. You have a fresh resource that takes the project parameter and uses a service to view the widget.

If you know all possible project names in advance, then you can use invoices instead of service.

  Public class widgetModule expands Abstraum module (Protected Zero Configuration) {mapbinder & lt; String, widget selection & gt; Mapbinder = MapBinder.newMapBinder (fastener (), string, category, widgetcollection.class); mapbinder.addBinding ("Project1") toInstance (...). mapbinder.addBinding ("Project2") toProvider (...). To mapbinder.addBinding ("project3") (...). }}    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -