Announcement Announcement Module
Collapse
No announcement yet.
Question on DI Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Question on DI

    I have a requirement, CAR is a interface and FORD,MARUTI,HYUNDAI,BMW are implementations of CAR interface.
    I need to get specifica car details based on the input. In that case how do we inject diff car variations using DI?

  • #2
    First declare each independent instance :

    <bean id="BMW" class="mycars.impl.BMW" />
    <bean id="MARUTI" class="mycars.impl.MARUTI" />

    .. one for each make of CAR..

    Then you'll need some for of "CAR Collection" object ..

    <bean id="carCollection" class="mycars.utils.CarCollection">
    <constructor-arg>
    <list>
    <ref bean="BMW"/>
    <ref bean="MARUTI"/>
    ...
    </list>
    </constructor-arg>
    </bean>

    You CarCollection class will need a constructor .. something like this with var args may do it :

    public class CarCollection {

    List<CAR> myCars = new ArrayList<CAR>();

    public CarCollection( CAR ... cars ) {
    // save the set of beans here
    myCars.addAll( cars );
    }
    }

    Comment


    • #3
      Could you provide more details ?
      What input is it that you have to base your decision on ?
      Is it some user input or a parameter in a config file ?

      // Pontus

      Comment


      • #4
        it will be a user input on which i need to inject a make of the car.

        Comment


        • #5
          Without having more information on your specific use case I can give the following advice.

          1. Create a repository or catalogue, much in the same way that spangleweed suggest above, and inject it into your service. You can then look up the car based on the user input.

          2. Inject the ApplicationContext into your service and use getBean() to get the variation. Note that this will make your solution harder to unit test and you will be tightly coupled with Spring framework.

          3. If you need a separate Car instance each time (instead of the sharing the same instance between all users) you can define the different the Car beans with scope="prototype" [1]
          Code:
          <bean id="BMW" class="mycars.impl.BMW" scope="prototype" />
          Every time this bean is injected or retrieved using getBean() a new instance of the mycars.impl.BMW class will be returned.

          [1] http://static.springsource.org/sprin...opes-prototype

          Hope this helps, if not please add more details on your use case.

          Best regards
          Pontus Ullgren

          Comment


          • #6
            Thanks for the suggestions. This question was asked in one of the interviews. Let me try to reframe the question
            "There is a POJO which gives Insurance details for different CARS. Depending on the insurance number, i have to inject car bean in to the POJO".

            @spangleweed
            If we put all the cars in list/hashmap, then aren't we injecting all the car beans in to the pojo unnecessarily?

            Comment


            • #7
              In the way you put it now I would not directly inject the Car bean into the POJO.
              I would create two DAOs (CarRepository and InsuranceRepository) that I inject into the POJO.

              Then I would use these
              Code:
              Insuarance insurance = insuranceRepository.getInsuranceById(userInputString);
              Car car = carRepository.getCarByMake(inusrance.getMake());
              The DAOs will probably get data from some datastore such as a SQL database and possibly using a ORM such as Hibernate in case you could possibly do:
              Code:
              Insuarance insurance = insuranceRepository.getInsuranceById(userInputString);
              Car car = insurance.getCar();
              All depending on how you annotated you entity classes.

              Cheers
              Pontus Ullgren

              Comment


              • #8
                In ur example, where are we making use of SPRING DI?

                Comment


                • #9
                  In the same way as the previous examples. By injecting the repositories and other dependencies.

                  Comment

                  Working...
                  X