• 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

Automatic object initializing based on type in spring

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there everybody,

I have a list of Class types that I need to iterate over and for every type in that list, Spring needs to automatically find the correct sort of object to initialize.

For example : I have a ClassA and ClassB. When for example there is a type User, a ClassA needs to be initialized with certain parameters (which are defined in the context.xml). When for example there is a type of Message, it should also make an object from type ClassA but with different parameters, which are again defined in the context.xml.

How would I go about doing this? I have looked at the inversion of control feature of spring but cannot seem to find the correct implementation for my specific purpose

One idea was that I make a bean for every kind of possible type such as



Kind regards,

Merlijn
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't quite understand what you mean.

But I can point you to some ideas.

1) Use @PostConstruct and @Autowired on a List<typesHere>, in the postConstruct loop through the list and create instances?
2) Use Spring Expression language in your config, calling a method that goes through your list and creates instances based on that.
3) implement a BeanFactoryPostProcessor that looks at all the BeanDefinitions in your ApplicationContext and for every type, create a BeanDefinition for your class types
4) implement a BeanFactory or POJO factory, which creates those objects.

Just ideas without understanding your needs.

Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic