-
Notifications
You must be signed in to change notification settings - Fork 1
Component
The Component annotation is used to indicate that a class is a framework component, meaning it is a candidate for auto-detection by the framework if component scanning is enabled.
The annotation has a single, optional attribute which indicates the name of the component. If it is not specified, the component takes the class name in camelcase form.
Components include mechanisms such a post processors, e.g, BeanFactoryPostProcessors and BeanPostProcessors, and other hooks used by the framework, like BeanProviders.
If looking to inject dependencies, use the Bean annotation since beans, which are specialized components, are intended to resolve autowire dependencies. Components simply register a class with the framework context.
The below example illustrates an implementation of a BeanFactoryPostProcessor. The Component annotation will allow the post processor to be picked up and registered automatically by the framework.
@Component
public class BuildTargetedPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(BeanFactory beanFactory) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
// Configure beans for newer APIs
} else {
// Configure beans for older APIs
}
}
}