Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - how to inject components from another module into a SpringBoot application

In my project I have two modules:

  • com.demo.shared
  • com.demo.app

In com.demo.shared I have a component

@Component
class Address(
    @Value("\${config.address.host}") val host: String,
    @Value("\${config.address.port}") val port: Int
)

In com.demo.app I want to have Spring's IoC container inject the component

@SpringBootApplication
class Application(address: Address) {
    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            SpringApplication.run(Application::class.java, *args)
        }
    }

    private val client: HttpClient("http://${address.host}:${address.port}/")
}

When I run the application I get this error:

Parameter 0 of constructor in com.demo.app.Application required a bean of type 'com.demo.shared.Address' that could not be found.

What am I missing?

NOTE: I've tagged Java because, even though the modules are using Kotlin, if anyone can provide Java examples of what to do, I'll be able to port it relatively easily.

like image 282
Matthew Layton Avatar asked Oct 27 '25 19:10

Matthew Layton


1 Answers

It looks like you just need to add a component scan annotation: http://www.springboottutorial.com/spring-boot-and-component-scan

@ComponentScan(“com.in28minutes.springboot”)

like image 157
Jonathan S. Fisher Avatar answered Oct 30 '25 10:10

Jonathan S. Fisher