• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question regarding DI and "new" instentiation

 
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

How can I inject the repositories into the Resolver class when the Resolver constructor arg is based on user input? Maybe there is a better way to structure this code?



 
Rj Ewing
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe a better question to ask is what is the best way to structure code like the above?

The Resolver class is used in a rest service (not shown). I also realize that the Resolver won't work as currently written. I am refactoring our legacy code and trying to understand the best way to structure our code with spring. When creating classes that utilize repositories, is it better to inject the repositories in the constructor and then move the previous constructor code into a method, and then call this new method in the method you want to call (resolveIdentifier method above)?
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should a new Resolver instance be created on every request to the RESTful service?
 
Rj Ewing
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the way it is currently written, I would say yes.

In the Resolver, I need to parse a string to get an identifier and then lookup a Bcid from the db with the identifier.

the IdentifierIncludingSuffix class is a class within the Resolver that decodes the string and stores the decoded information, including the identifier.

Maybe there would be a better way to write the Resolver though?
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the logic (without any Spring stuff) is something like this, where service creates a Resolver instance and then uses it:

and there is only one repository and Resolver always uses that same repository, I feel a cleaner design is to make Resolver a stateless singleton:

The spring version of that should be straightforward. Basically, remove the constructor argument and logic, move them to a getIdentifier or similar method and don't store that bcid in the Resolver.

----

But if that is not possible and Resolver has to to be one instance per request for some reason, then some changes have to be made:
1. Resolver should be made a "prototype" bean. In spring, beans are singletons by default; in contrast, a prototype bean is one that can be instantiated multiple times.
Every instance will be wired with dependencies.


2. The service bean should ask Spring to give it an instance of the Resolver. This can be done in multiple ways:
  • Via applicationContext, the downside being your code is now spring aware:



  • Via @Lookup annotation, the downside being your project has to include cglib.jar as a dependency:



  • Via javax.inject.Provider, the downside being your project has to include javax.inject jar as a dependency:

  •  
    Rj Ewing
    Ranch Hand
    Posts: 120
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the help. I was able to make the Resolver a singleton.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic