Using @ComponentScan or <context:component-scan /> with only one class
What @Bart said for XML.
If you need to pull in that one class using annotations, add the following to one of your @Configuration
classes
@ComponentScan(
basePackageClasses = YourClass.class,
useDefaultFilters = false,
includeFilters = {
@ComponentScan.Filter(type = ASSIGNABLE_TYPE, value = YourClass.class)
})
Simply add is as a bean to your context e.g.
<bean class="my.package.MyClass" />
In addition to the method described by Emerson Farrugia there is a less verbose solution which has been supported since Spring Framework 4.2 as mentioned in the documentation here.
As of Spring Framework 4.2,
@Import
also supports references regular component classes, analogous to theAnnotationConfigApplicationContext.register
method. This is particularly useful if you want to avoid component scanning, by using a few configuration classes as entry points to explicitly define all your components.
So your example would simply become:
@Import(YourClass.class)