如何在Spring Boot 2中从库中注入依赖?

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

How can I inject dependency from a library in Spring Boot 2?

问题

我有一个库,在库中我定义了一个名为MyClass的类,使用了@Component@Value,但是当我尝试在我的Spring Boot应用程序中使用它并进行自动装配时,我得到了关于Spring无法找到此类型的异常,并要求我定义一个Bean。我在应用程序中定义的所有其他类都可以正常注入。如何让MyClass能被注入?

英文:

I have a library in which I defined a class let's say MyClass with @Component along with @Value but when I try to use this in my Spring Boot application and try to Autowire it, I get exception about Spring not being able to find this type and ask me to define a Bean. All other classes gets injected just fine that I have defined in the application it self.

How can I make the MyClass to be injected?

答案1

得分: 1

没有关于您的库以及您如何使用它的完整信息,我们无法提供解决方案。假设您的库中的一切都是正确的,您可以简单地在使用您的库的应用程序上添加 @ComponentScan

创建一个如下的类,应该可以解决您的问题。

@Configuration
@ComponentScan({"您的库包名"})
public class 您的配置类 {

}

如果这不能解决您的问题,请在您的问题中添加更多信息,我会相应地更新我的回答。

英文:

Without a complete information on about your library, how you are using it, we can't provide solution. Assuming everything on your library is correct, you can simply add @ComponentScan on the application that use your library.

Create a class as below and that should fix your problem.

@Configuration
@ComponentScan({"your library package"}) 
public class YourConfig {

}

If this doesn't solve your problem, add more information on your question and I will update my answer accordingly.

huangapple
  • 本文由 发表于 2020年7月24日 00:34:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63059019.html
匿名

发表评论

匿名网友

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

确定