javaspringspring-boot

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


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?


Solution

  • You can simply add @ComponentScan on the application that use your library.

    Create a class as below.

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