0

I have this scenario. I'm developing a REST service with Spring Boot and I have a dependency jar which is another Spring Boot project that I included as a dependency using this method:

https://docs.spring.io/spring-boot/docs/2.1.4.RELEASE/maven-plugin/examples/repackage-classifier.html

Everything compiles perfectly and I can call classes from the dependency jar. Inside the code in the dependency jar I use @Value annotations to read values from application.properties of the "father" project, but it always return null.

If I try to read the same value from the father project it works.

I removed the original application.properties file inside the dependency jar, in order to avoid weird double properties file, but nothing changes.

Any idea on how to solve this problem?

Thanks in advance.

CSoft
  • 31
  • 1
  • 8

3 Answers3

1

I can think of two options you could try.

#1

Try including the parent application.properties in a @PropertySources annotation. You might have to make sure that the properties file is different between the two applications. It is described more in this answer.

#2

Setup the parent project to have a config property class that you can inject in your application, and retrieve the necessary properties.

The parent project might have a class like this:

@Data // Part of the Lombok project, creates the getters/setters automatically
@ConfigurationProperties(prefix = "application.specific.prefix")
public class ApplicationConfig {
    private String someValue;
    private Integer importantNumber;
}

Your application.properties would look like this:

application.specific.prefix.some-value=Spring Boot is great!
application.specific.prefix.important-number=42

In your Spring controlled classes you could include this resource with:

@Autowired
private ApplicationConfig applicationConfig;

And then you could call the properties you needed with:

applicationConfig.getSomeValue();

Baeldung has a helpful guide to @ConfigurationProperties

mnd
  • 2,709
  • 3
  • 27
  • 48
1

Solved. Thank you for your response mnd!

I solved the problem and write here the solution, maybe it could be useful to someone else.

First of all, I made a silly mistake, I forgot to put the bean under @Autowired! :-D

But there's another thing to do, in order to make the parent project autowire the dependency beans, we must add its packages with the @ComponentScan in the Config class of the parent project.

After this, all the properties in the parent project are correctly injected in the dependency beans.

Thank you.

Best Regards.

CSoft
  • 31
  • 1
  • 8
0

This issue was resolved in this post. If yo are using Maven try to remove <package> tag from your main pom.xml for use default (jar) and say to Spring-Boot load and read resources.

More info about Maven lifecycle reference here