Question – @ComponentScan(basePackageClasses = AdminController.class) <- why has it broken my application?

huangapple go评论73阅读模式
英文:

Question - @ComponentScan(basePackageClasses = AdminController.class) <- why has it broken my application?

问题

我遇到了一个问题,我无法理解,问题是:

***************************
应用启动失败
***************************

描述:

webService.controller.RequestController中的setApplicant方法的参数0需要一个类型为'com.service.applicant.Applicant'的bean,但找不到该bean。


操作:

请考虑在配置中定义一个类型为'com.service.applicant.Applicant'的bean。

我已经进行了一些研究,但我仍然无法解决这个问题,当我从主应用程序类中简单地删除了 @ComponentScan,问题就解决了。

我删除了:

//@ComponentScan(basePackageClasses = AdminController.class)

一切都好了,但我感到很好奇... 有人可以帮助我吗?

英文:

i was getting an issue which I couldn't understand, which was:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method setApplicant in webService.controller.RequestController required a bean of type &#39;com.service.applicant.Applicant&#39; that could not be found.


Action:

Consider defining a bean of type &#39;com.service.applicant.Applicant&#39; in your configuration.

I've done some researches but I still couldn't fixed this problem, when I simply removed the @ComponentScan from my Main Application Class and it worked.

I removed:

//@ComponentScan(basePackageClasses = AdminController.class)

Everything is fine, but i got curious... Can someone please help me?

答案1

得分: 0

你应该首先在类 Applicant 上使用 @Component 进行注解。
在你的主类上,使用 @Configuration@EnableAutoConfiguration@ComponentScan({ "com.service.applicant", "your.admin.controller.path" }) 对对象进行注解。

在类 RequestController 中,将类型为 Applicant 的属性使用 @Autowired 进行注解。

英文:

you should annotate the class Applicant with @Component first.
in your main class annotate the object with @Configuration, EnableAutoConfiguration and @ComponentScan({&quot;com.service.applicant&quot;,&quot;your.admin.controller.path&quot;}).

in the class RequestController annotate the property of type Applicant with @Autowire.

答案2

得分: 0

当您不显式使用@ComponentScan时,您的主类上的@SpringBootApplication注解中包含了@ComponentScan的实现(以及@EnableAutoConfiguration)。因此,所有标记为@Component(或类似的注解,如@Controller、@Service等)的类都会被自动扫描,前提是它们与主类在同一个包或子包中定义。

现在在您的情况下,您在主类中添加了//@ComponentScan(basePackageClasses = AdminController.class)。这样做的效果是只为类AdminController创建了一个Bean,并忽略了所有其他类。

英文:

When you don't use @ComponentScan explicity, @SpringBootApplication annotation on your main class has implementation of @ComponentScan in it(along with @EnableAutoConfiguration). So all the classes marked as @component(or similar annotation like controller, service etc.) get scanned automatically provided they are in the same or sub-package where main class is defined.

Now in your case you added //@ComponentScan(basePackageClasses = AdminController.class) in your main class. What this did that it only created bean for the class AdminController and ignored all other classes.

huangapple
  • 本文由 发表于 2020年8月24日 22:51:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63563407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定