英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论