0

I've been searching around and scratching my head for a few hours now, and I can't get it working.

I have a multi-module project with Gradle, currently with two modules: the BFF (using Spring Boot), and a module fetching some data from a database (inspired by this post). The structure looks something like this:

├── bff
|  ├── build.gradle.kts
|  ├── src
|      └── main
|           ├── java
|           |    └── some.package.bff.controller
|           |        └── CustomerController
|           └── resources
|               └── application.yaml
├── customer-service
|  ├── build.gradle.kts
|  ├── src
|      └── main
|           ├── java
|           |     ├── some.package.customer.config
|           |     |    ├── CustomerServiceConfiguration
|           |     |    └── CustomerServiceConfigurationProperties
|           |     └── ...
|           └── resources
|               └── customer-service-properties.yaml
├── build.gradle.kts
└── settings.gradle.kts

The CustomerServiceConfigurationProperties is a simple ConfigurationProperties class. The CustomerServiceConfiguration class serves as a way to "import" the module into the BFF's Spring context, inspired by this post:

@Configuration
@EnableConfigurationProperties(CustomerServiceConfigurationProperties.class)
@PropertySource("classpath:customer-service-properties.yaml") //This fails
@ComponentScan(basePackages = {
    "some.package.customer"
})
public class CustomerServiceConfiguration {
    private static final Logger log = LoggerFactory.getLogger(CustomerServiceConfiguration.class);
    private final CustomerServiceConfigurationProperties configurationProperties;

    public CustomerServiceConfiguration(final CustomerServiceConfigurationProperties configurationProperties) {
        this.configurationProperties = configurationProperties;
    }

    @Bean
   SomeBean someBean() {
       ...
    }
}

Adding properties mapped by CustomerServiceConfigurationProperties to BFF's application.yaml works as intended and are picked up to configure the Customer Service module. What doesn't work is adding configuration properties for the customer-service module that shouldn't be modified by the bff, e.g. Hibernate settings. I intend to do this using customer-service-properties.yaml. When I try to run the application with the line in question, Spring complains that that file is not found on the classpath. Sure enough, when looking at the generated jar, it isn't there. Adding it to the bff's resources works, but that's obviously not what I want.

Do I need to find a way to add the customer-service-properties.yaml file to the classpath? Should I not use the classpath but rather something else for this? Am I simply trying to do something I shouldn't? Is there a different way to go about this?

Note: I'm still quite new to Gradle, if that's at all relevant.

Let me know if I need to add or specify something else. Any help or pointers will be appreciated. Thanks!

Steen
  • 311
  • 1
  • 3
  • 11
  • You cannot read a yamo file with `@PropertySource` it only supports property files. nor will `@EnableConfigurationProperties` work with a property file loaded from an `@PropertySource`. That being said your `customer-service` jar should build a jar which contains the yaml file and this jar should be part of the classpath and thus the file would be loaded. Looks like you aren't building that jar (correctly) or including things differently in your `bff` project. – M. Deinum Apr 02 '21 at 09:35
  • I actually tried that, and that didn't work either. Besides, adding the yaml file to the `bff` does work. – Steen Apr 02 '21 at 09:37
  • I was editing my comment, si please read again. Also there is no guarantee that properties won't be overridden in your `application.yaml` file. They will all be loaded in the same `Environment` (eventually) and resolved from there. So to think that you can provide a configuration which cannot be overridden is simply not possible. – M. Deinum Apr 02 '21 at 09:38
  • Well, the `customer-service-properties.yaml` should contain whatever properties that shouldn't be modified by the `bff` (so none of the properties mapped by `CustomerServiceConfigurationProperties` should be in there). Anyway, does that mean you can't use `@PropertySource` in conjunction with `@EnableConfigurationProperties`, regardless of their content? Also, when inspecting the built jar for the `customer-service` module, the `customer-service-properties.yaml` is in there. It's not in the one for `bff`, but that would probably make sense. So, how could I get it on the classpath? Thanks – Steen Apr 02 '21 at 09:46
  • When using Spring Boot the jar should be on the classpath so all the content as well. Regardless of where the properties are, if a property is defined in `application.yaml` it will simply override the one from your custom yaml. Or when defined as a program argument. So there is no way this prevents not being able to override/change properties. Also the jar created for your service should be the jar used by Spring Boot, why should that be suddenly different? – M. Deinum Apr 02 '21 at 09:48
  • Fine, `bff` being able to override the config isn't a huge concern. But it shouldn't be *required* to configure `customer-service`, which seems to be the case right now.. Am I missing something? I'm not sure where to look next... – Steen Apr 02 '21 at 09:51
  • 1
    As stated the jar fro your service should contain everything that jar should also be used by your bff project and thus it should be available. If you do something else to create that service jar (or provide a different dependency) which doesn't include it your build is broken (too complex maybe?). – M. Deinum Apr 02 '21 at 09:54
  • You were right! It was quite simple: I forgot to add the spring boot plugin to `bff`'s `build.gradle.kts`. I didn't notice because Intellij happily ran the application regardless somehow. Feel free to post this as an answer :) – Steen Apr 02 '21 at 10:07

0 Answers0