If you want Spring to find your beans so you can stop wiring them by hand like it is 2003, use @Component and @ComponentScan and let Spring Boot do the heavy lifting. This guide explains what those annotations do how beans get registered and why constructor injection is the one you should actually trust when writing tests.
Annotate a plain Java class with @Component or a specialized stereotype such as @Service or @Repository and Spring treats the class as a candidate for bean creation. That means Spring will create an instance and manage its lifecycle inside the ApplicationContext. It saves you from XML rituals and from writing a factory for every tiny helper class.
@Component
public class GreetingService {
public String greet(String name) {
return "Hello " + name
}
}
@ComponentScan makes Spring walk packages looking for annotated types and register them as beans. In Spring Boot you usually do not need to add @ComponentScan manually because auto configuration and the default package scanning from your main class handle the common cases. If your classes sit outside the main package then use explicit basePackages or basePackageClasses on @ComponentScan.
@Configuration
@ComponentScan(basePackages = "com.example.app")
public class AppConfig {
}
// Or in Spring Boot the main application can be enough
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args)
}
}
When the ApplicationContext starts Spring scans for annotated classes instantiates them applies post processors and registers the resulting bean definitions. Bean names follow rules derived from the class or the explicit name you provide on the annotation. Bean post processors and lifecycle callbacks run so any AOP proxying or init methods are applied before your bean gets handed off to the wiring team.
Prefer constructor injection because it makes dependencies explicit helps with immutable design and plays nicely with unit testing. Field injection is convenient for toy examples but causes headaches when you try to instantiate objects without the whole Spring context during tests.
@Component
public class OrderController {
private final OrderService orderService
@Autowired
public OrderController(OrderService orderService) {
this.orderService = orderService
}
}
Component scanning supports include and exclude filters so you can surgically pick which classes become beans. You can also control scopes with @Scope if you want prototype instances or request scoped beans in web apps. Lifecycle annotations such as @PostConstruct and @PreDestroy let you run init and cleanup logic when the context controls the object lifecycle.
@ComponentScan(
basePackages = "com.example",
includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = MyCustomAnnotation.class),
excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = SomeLegacyClass.class)
)
public class FilterConfig {
}
Spring Boot gives you sane defaults by scanning from the package of your main application class. Follow that layout and most of the time you can rely on auto configuration and stereotypes without extra declarations. When you stray outside that layout add explicit @ComponentScan settings or move the main class so scanning covers your packages.
There you have it a compact map of how @Component and @ComponentScan make Spring find register and wire beans. Follow package layout rely on stereotypes and use constructor injection so you can sleep at night knowing your dependencies are not silently missing.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.