• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

How to dynamically inject sub type dependencies in Spring mvc?

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 5508
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Autowired isn't the right solution for this use case. In fact, your example given will fail to start as Spring will complain that it doesn't know which implementation of Vehicle it should inject.

You will need to write some code that will recognise the user input and create the correct implementation of Vehicle. The Factory Pattern might be a suitable approach to take here.
 
abhinas raj
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:Autowired isn't the right solution for this use case. In fact, your example given will fail to start as Spring will complain that it doesn't know which implementation of Vehicle it should inject.

You will need to write some code that will recognise the user input and create the correct implementation of Vehicle. The Factory Pattern might be a suitable approach to take here.



yes right now it will fail. but if i have configured all the beans in spring config file spring container has to create the instances of the  beans. if i use factory pattern to create the objects based on the data passed from
ui then i think there is no use of using spring for this use case. i need to do it using features of spring  only. that's why i had to ask if there is any way in spring to achieve this use case.
 
Tim Cooke
Sheriff
Posts: 5508
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abhinas raj wrote:i need to do it using features of spring  only



Why?
 
abhinas raj
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:

abhinas raj wrote:i need to do it using features of spring  only



Why?



Actually i was asked this question in an interview. he was asking like there is some way to do that in spring because he told that you do not have to do it with factory design pattern.
 
Tim Cooke
Sheriff
Posts: 5508
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have named your bean definitions then you could use

For example (pseudo-code)
 
abhinas raj
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:If you have named your bean definitions then you could use

For example (pseudo-code)



will @Autowire with @Qualifier(vehicleName)  can help here ? we can get vehicleName from user and pass inside @Qualifier.
 
Tim Cooke
Sheriff
Posts: 5508
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm pretty sure you can't pass variables into Java Annotations, so no that won't help.
 
abhinas raj
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:I'm pretty sure you can't pass variables into Java Annotations, so no that won't help.



Thank you Tim for your reply
 
reply
    Bookmark Topic Watch Topic
  • New Topic