2

Is it possible to assign @Qualifier value at runtime. Let's say I have two beans of same type and a class with dependency injection

  <bean id="typeA" class="com.debopam.test.Type">
         <property name="typevalue" value="Export" />
    </bean>
    <bean id="typeB" class="com.debopam.test.Type">
         <property name="typevalue" value="Import" />
    </bean>

    public class Product {
    private Integer price;
    private String name;

    @Autowired
    @Qualifier("typeB")
    private Type type;

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public Type getType() {
        return type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Is there any way to define type in Product class in runtime instead of hard coding/specifying the value? If yes could you please post some code, otherwise is it advisable to use ApplicationContext.getBean("bean name") to load the bean at runtime?

Debopam
  • 3,198
  • 6
  • 41
  • 72
  • Take a look at this http://stackoverflow.com/questions/25590435/how-to-set-a-bean-qualifier-name-at-run-time-in-spring – Michael Mar 27 '15 at 10:43
  • possible duplicate of [Spring Qualifier and property placeholder](http://stackoverflow.com/questions/7812745/spring-qualifier-and-property-placeholder) – David Levy Jul 10 '15 at 01:53

1 Answers1

-2

Yes, I see that you defined two beans for a class. The @Qualifier annotation will tell the class which bean has to be used from the spring configuration file

R Penumaka
  • 171
  • 7