Announcement Announcement Module
No announcement yet.
Providing dependency Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Providing dependency

    I have this java code

    interface A{}
    class B implements A{}
    class C implements A{}

    class B and C both implements the same interface. Now I have one class D in which based on condition I want to return either instance of B or C as shown in code below.

    Class D{
    public getXYZ(boolean condition){
    if (condition)
    return new B();
    return new C();

    How I can do this using DI?

  • #2
    Dependency Injection is a strategy which allows your code to receive its dependencies externally as opposed to hard-wiring them through the use of the 'new' operator. I don't quite follow how the question you asked has anything to do with dependency injection. Your question pertains more to the factory pattern.


    • #3
      Problem in Autowiring in Aspect class

      I have also problem with dependency..
      I have written an aspect like
      public class A{
      B b;
      public void method(){;}

      But I am getting null value in log. where as in non aspect classes value of b is proper as required. I have properly mentioned the @Bean B in config class as well.

      Can anyone let me know what I am missed in the above code?


      • #4
        For the above problem i m getting a message is
        org.springframework.beans.factory.wiring.BeanConfi gurerSupport - BeanFactory has not been set on BeanConfigurerSupport: Make sure this configurer runs in a Spring container. Unable to configure bean of type [A]. Proceeding without injection.

        Please let me know the missing part of the code..
        Last edited by Dinesh Kumar; Mar 19th, 2009, 04:26 AM.


        • #5
          As your question is completely different from the original, you'd probably be better off starting a new thread. When you do, post your configuration xml also.