Announcement Announcement Module
No announcement yet.
How to 'get' the prototype beans cleanly ? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to 'get' the prototype beans cleanly ?

    Hello !

    I would like my other classes to interact with my domain's interfaces rather than implementation, so, i would like to avoid this, since i would like the benefit of being able to change the implementation later :
    DomainModel myDomainModel = new DefaultDomainModelImpl();

    If i use spring container, with the prototype scope, i can use something like :
    // <bean id="myDomainBean" scope="prototype" class="kam.albert.content.domain.MyDomainImpl" />
    DomainModel myDomainModel = springContext.getBean("myDomainBean");

    But i would like to avoid accessing springContext explicitly in my code.

    What's the clean way to do this ?

    Im currently thinking of creating a factory implementation for each domain implementation, and autowire the factory to create the beans, but that means different implementations of my domain will have different implementations of the factory also.

    Please share your opinions, thank you !

  • #2
    What's the clean way to do this ?
    Use dependency injection...that's what Spring was designed for in the first place!

    private DomainModel myDomainModel;


    • #3
      Thanks, that'd do for a field of a class.

      How about if i want to do this inside a local method, where it's not declared as a field ?
      For example :
      public void addToMyList(String s, int i) {
        DomainModel model = new DefaultDomainModelImpl(); // it's hardcoding the implementation here
      Frankly, im beginning to think that this is a wrong question for spring, since my case is not about wiring object dependencies, but rather about instantiation of specific implementations that can be solved with autowiring factories.

      Any extra thoughts ?