Rajeev
Rajeev

Reputation: 5002

SpringBoot not loading properties from dependent library

Hi i have a library which has class B with injected Property. Property is defined in application.yml file in resources section of library. library is packaged as Jar.

Class is something like this

@Configuration
class ClassB(
    @Value("\${library.config.key}") private val key: String
) {
//Test code
}

Now i have springBoot application class with name ClassA which depends on classB. This application has its own yml file but property is not repeated here as it is already declared in library config.

@Configuration
class ClassA(
    @Autowired val classB: ClassB
) {
//Test code
}

Now app started, it fails to initialise application with error

"Could not resolve placeholder 'library.config.key' "

In the spring boot app, i have added annotation to scan all components with common package prefix name

@SpringBootApplication
@ComponentScan("com.package")
class BhadraExplorerServiceApplication {
}

all code including dependent library code comes under package com.package

Can someone guide what is missing configuration here

UPDATE I am not sure if there is a bug in framework. If i make B's configuration as properties file instead of yml file, it is loading properly.

Upvotes: 4

Views: 2834

Answers (3)

Silk0vsky
Silk0vsky

Reputation: 1042

If you have an access to a library codebase then you can configure an EnvironmentPostProcessor to pick up its properties automatically.

Please see my detailed answer: How to inherit application.properties in Spring?

Upvotes: 0

loïc
loïc

Reputation: 837

I had the same problem as OP and found out the same thing:

I am not sure if there is a bug in framework. If i make B's configuration as properties file instead of yml file, it is loading properly.

This is correct.

But I wanted to keep using yml files in my library, so instead of loading them indirectly by placing them in application.yml, I loaded them with @PropertySource.

Normally loading .yml files with @PropertySource is not possible, but they added some support to be able to implement this yourself.

See the docs: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.yaml.directly-loading

See the tutorial how to easily do this: https://www.baeldung.com/spring-yaml-propertysource (Baeldung is always a good reference)

Just tested this in my project, and the properties from the internal yml file of my library are loaded nicely in my app that uses the library.

Upvotes: 4

Tashkhisi
Tashkhisi

Reputation: 2262

As its name imply @ComponentScan("com.package") tells Spring where it should scan components of your application and not where to find the property sources.

Annotation that you are looking for is @PropertySources, it tells Spring Framework where to find property sources(which can also be property files) of your application. If you want to read properties of your application from different files you can use it like below:

@PropertySources({
    @PropertySource("classpath:application.properties"),
    @PropertySource("classpath:lib-application.properties")
})

Unfortunately names of the property files should be different if they are in the same path in classpath.

Upvotes: 1

Related Questions