23

I am using spring component scan to auto detect beans as:

@ComponentScan({"com.org.x, com.org.y"})

The issue is I want all classes in com.org.x to be scanned but I want a single class, com.org.y.SomeService.class, alone to be scanned from com.org.y

How can I achieve this ?

Also apart from using context scan, how can I created this bean and inject in the application context ?

Anand Sunderraman
  • 7,900
  • 31
  • 90
  • 150

2 Answers2

28

@Import(com.org.y.SomeService.class) works in my case (even when SomeService is a @Service, not a @Configuration)

Ilya Serbis
  • 21,149
  • 6
  • 87
  • 74
  • I didn't know that one can even import `@Service`s. This is especially helpful if one can't call a constructor due to visibility restrictions. – Harold L. Brown Feb 23 '23 at 15:41
3

You should just define your bean using a method annotated with @Bean in your configuration class, as explained in the documentation.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255