0

I have a class that requires parameters provided by the user at runtime. I do this because I like the idea of ensuring the object is always in a valid, ready to use state. However I do not know the parameter values until the user has started using the application which is causing problems with Spring IoC as everything is instantiated at start up.

I think I can work around it this with @Configuration, .getBean calls with arguments, and @Lazy annotations however it feels like I'm kluging (eg. .getBean calls are discouraged)

Is there a nicer way to handle this? I'm thinking I should just lump it and move to having a constructor with no parameters and force a setter method call with the parameters but then I'll have an object sitting around that's in an invalid state.

TownCube
  • 1,280
  • 1
  • 13
  • 32
  • Well, instantiating your beans before you have a database connection is already discouraged by your design, so spring doesn't actually add something new to the mix. I would suggest simply creating a factory class that instantiates your actual objects with the whole db stuff. Or you could make the class itself lazy, but that again would require throwing errors around until it had a change to initialize itself properly. – Florian Schaetz Feb 25 '17 at 14:13
  • @florian-schaetz If I use a factory class to create the actual objects how would I go about injecting those objects into other objects so at least those objects could still take advantage of IoC? – TownCube Feb 25 '17 at 14:17

0 Answers0