Results 1 to 3 of 3

Thread: How to 'get' the prototype beans cleanly ?

  1. #1

    Default 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. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

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

    Code:
    @Inject
    private DomainModel myDomainModel;
    ...
    myDomainModel.doMyStuff()

  3. #3

    Default

    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 :
    Code:
    public void addToMyList(String s, int i) {
      DomainModel model = new DefaultDomainModelImpl(); // it's hardcoding the implementation here
      model.setName(s).setAge(i);
      myList.add(model);
    }
    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 ?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •