Skip to content

CT Component

Aditya Ketkar edited this page Feb 18, 2018 · 5 revisions

Component annotation (@Component)

The @Component annotation marks a java class as a bean so the component-scanning mechanism of Spring can pick it up and pull it into the application context. To use this annotation, apply it over class as below:

@Component("interfaceAImpl_1")
public class InterfaceAImpl implements InterfaceA {
    ...
}

The above annotation only works if the class using another class as bean is in the same package. For extending the scan mechanism to another package we use ComponentScan. The scanning mechanism can manually be invoked by using the @ComponentScan annotation. An example can be found below :

@ComponentScan(basePackages = {
        "in.ac.bits.protocolanalyzer.persistence.repository",
        "in.ac.bits.protocolanalyzer.analyzer.network"
}

References

  1. Basic Turorial
  2. https://www.boraji.com/index.php/spring-4-auto-component-scanning-example
  3. https://blog.codecentric.de/en/2012/07/spring-dependency-injection-styles-why-i-love-java-based-configuration/
Clone this wiki locally