By default, <context:component-scan> looks for classes that are annotated with one of a handful of special stereotype annotations:
@Component - A general-purpose stereotype annotation indicating that the class is a Spring component
@Controller - Indicated that the class defines a Spring MVC controller
@Repository - Indicated that the class defines a data repository
@Service - Indicates that the class defines a service
Any custom annotation that is itself annotated with @Component
First, let’s annotate the Guitar class with @Component:
package com.springinaction.springidol;
import org.springframework.stereotype.Component;
@Component
public class Guitar implements Instrument{
public void play(){
System.out.println("Strumstrumstrum");
}
}
When Spring scans the com.springinaction.springidol package, it’ll find that
Guitar is annotated with @Component and will automatically register it in Spring. By
default, the bean’s ID will be generated by camel-casing the class name. In the case of
Guitar that means that the bean ID will be guitar.
@Component("eddie")
public classInstrumentalistimplementsPerformer{
// ...
}
we explicitly named it eddie.
@Component - A general-purpose stereotype annotation indicating that the class is a Spring component
@Controller - Indicated that the class defines a Spring MVC controller
@Repository - Indicated that the class defines a data repository
@Service - Indicates that the class defines a service
Any custom annotation that is itself annotated with @Component
First, let’s annotate the Guitar class with @Component:
package com.springinaction.springidol;
import org.springframework.stereotype.Component;
@Component
public class Guitar implements Instrument{
public void play(){
System.out.println("Strumstrumstrum");
}
}
When Spring scans the com.springinaction.springidol package, it’ll find that
Guitar is annotated with @Component and will automatically register it in Spring. By
default, the bean’s ID will be generated by camel-casing the class name. In the case of
Guitar that means that the bean ID will be guitar.
@Component("eddie")
public classInstrumentalistimplementsPerformer{
// ...
}
we explicitly named it eddie.
Comments
Post a Comment