Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: Attaching @Qualifer to Java-configured beans

In spring, you can XML-configure a bean to have a qualifier. I can't seem to find how I can attach a qualifier if configuring beans through Java annotations. What's up with that? Do I have to use just plain old names?

like image 576
Aleksandr Dubinsky Avatar asked Feb 07 '12 19:02

Aleksandr Dubinsky


2 Answers

If you're using annotations (not Java based configuration), you can use the following to add a qualifier (see the Spring documentation):

@Component
@Qualifier("myQualifier")
public class MyBean {
    //code
}

And to wire in the bean, use the following (again, see the Spring documentation):

public class MyClass {

    @Autowired
    @Qualifier("myQualifier")
    private MyBean myBean;

    //more code

}
like image 88
Snowy Coder Girl Avatar answered Nov 16 '22 19:11

Snowy Coder Girl


What, like @Qualifier, you mean?

3.10.4 Defining bean metadata within components

Example:

  @Bean @Qualifier("public")
  public TestBean publicInstance() {
      return new TestBean("publicInstance");
  }
like image 19
skaffman Avatar answered Nov 16 '22 20:11

skaffman