Pivotal Knowledge Base

Follow

Spring Framework: How To Customize Auto-Wiring using User Defined Classes

Environment

 Product  Version
 Spring  All

Purpose

This article provides information on customizing auto wiring for Spring Framework using unrelated (not by type or name) user-defined classes.

Resolution

You can customize Auto wiring for your classes by specifying a qualifier. For example, you may use the class MyProvider below. Notice that @Qualifier should co-exist with the @Component Annotation so that the MyProvider class could be created.

@Component
@Qualifier("contentprovider")
public class MyProvider {
// Implementation omitted
}

If, for instance, you want a Spring Controller to have a property that will hold a list of Objects with classes you qualified as content provider, use the @Autowire annotation from your controller on one of your setters. You can use the @Qualifer to control Auto wiring:

@Controller
public class MyController {
.... @Autowired
public void setProviders(@Qualifier("contentprovider") List< Object > providers) {
...
}
...
}

Additional Information

For more information on auto wiring with qualifiers, refer to 4.9.3 Fine-tuning annotation-based auto wiring with qualifiers.

Comments

Powered by Zendesk