Spring boot component scan include a single class [duplicate]

耗尽温柔 提交于 2020-02-21 10:35:46

问题


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 ?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/37757768/spring-boot-component-scan-include-a-single-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!