java - Correct and standard way of putting application.properties files in a Springboot multi-module project and deploying as a

I am trying to find the correct and standard way of putting application.properties Files in a Springboo

I am trying to find the correct and standard way of putting application.properties Files in a Springboot multi-module project and deploying as as JAR file I have a Springboot project with two modules- library and application. Library contains only a utility service which needs to be consumed by application(main) module. In the library module I have put application.properties files which contains a key- values pairs:

service.message.test=Hello from Library Module

I am using this property in one of the service class file in library module using the below code:

 @Service
@EnableConfigurationProperties(ServiceProperties.class)


public class MyService {

    @Value("${service.message.test}")
    private String libMessage;



    private final ServiceProperties serviceProperties;

    public MyService(ServiceProperties serviceProperties) {
        this.serviceProperties = serviceProperties;
    }

    public String message() {
        return this.serviceProperties.getMessage()+libMessage; };
    }

Also, i have a application module(main) which also has a application.properties files in the src/main/resources folder.This file contains other key-value pairs for application

service.message=Hello, World

Now i am using the mvn clean package command to package the app in a JAR file. But on running the command, i am getting the error as :

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'service.message.test' in value "${service.message.test}"

Now, I have the following questions:

  1. If we are creating SpribgBoot mullti-module project,can't we keep application.properties in each module but rather should always use @ConfigurationProperties("service") only?

  2. I need to pass several properties to various classes in the library module .I am doing this by passing it from properties files at module level using @Value("${xxxx}”) annotation .Since, its giving error, is this not allowed?

  3. Is application.properties file only allowed in application(main) module and not in the dependent individual modules?

  4. What is the standard way of managing properties files in Springboot multi-module project ?

I am trying to find the correct and standard way of putting application.properties Files in a Springboot multi-module project and deploying as as JAR file I have a Springboot project with two modules- library and application. Library contains only a utility service which needs to be consumed by application(main) module. In the library module I have put application.properties files which contains a key- values pairs:

service.message.test=Hello from Library Module

I am using this property in one of the service class file in library module using the below code:

 @Service
@EnableConfigurationProperties(ServiceProperties.class)


public class MyService {

    @Value("${service.message.test}")
    private String libMessage;



    private final ServiceProperties serviceProperties;

    public MyService(ServiceProperties serviceProperties) {
        this.serviceProperties = serviceProperties;
    }

    public String message() {
        return this.serviceProperties.getMessage()+libMessage; };
    }

Also, i have a application module(main) which also has a application.properties files in the src/main/resources folder.This file contains other key-value pairs for application

service.message=Hello, World

Now i am using the mvn clean package command to package the app in a JAR file. But on running the command, i am getting the error as :

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'service.message.test' in value "${service.message.test}"

Now, I have the following questions:

  1. If we are creating SpribgBoot mullti-module project,can't we keep application.properties in each module but rather should always use @ConfigurationProperties("service") only?

  2. I need to pass several properties to various classes in the library module .I am doing this by passing it from properties files at module level using @Value("${xxxx}”) annotation .Since, its giving error, is this not allowed?

  3. Is application.properties file only allowed in application(main) module and not in the dependent individual modules?

  4. What is the standard way of managing properties files in Springboot multi-module project ?

Share Improve this question asked Nov 17, 2024 at 17:10 ashish chauhanashish chauhan 3851 gold badge3 silver badges16 bronze badges 2
  • 1. No you cannot. (applies to 2 and 3 as well). As application.properties is on the classpath Spring will only find the one from the application as that is the closest to the class that it is being loaded from. You should put all the properties in this, the application.properties in the jars more or less contain defaults, hardcode these into your classes and allow them to be overridden with the main application.properties. – M. Deinum Commented Nov 18, 2024 at 7:59
  • Thanks @Denium for the reply. I got it now. – ashish chauhan Commented Nov 18, 2024 at 15:14
Add a comment  | 

1 Answer 1

Reset to default 1

In multi-module Spring (Maven or Gradle) you can achieve what you want, but there is a gotcha with the application.properties file... each module has its own and only the "outer" one wins.

So if you have some properties that you want to specify in the library, use a different name. I suggest you use the convention of Spring (Active) Profiles.

In the Library name the file application-library.properies

Then in the main application specify the Active Profile as library and Spring will load whatever is in the main module's application.properties AND what is found in application-library.properies.

There are several ways to specify active profiles, one is simply on the execute of the outer application (e.g. in your IDE's Run configuration), see Set the Active Spring Profiles for many other ways

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745630028a4637048.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信