Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading spring properties from a consuming app

I'm developing a Java library/package that is meant to be consumed by Spring Boot applications as a jar.

The main driver class relies on a set of props to exist from applicaton.properties, and defines its own set within the repository.

However, I'd like for these properties to be configurable by consuming apps. What's the proper way to structure this?

For example, in the project I have a file

public class Properties {
    private int maxConnectingCount; 
    private int maxIdleCount;
    // .. other properties read from application.properties
}

The main driver class looks something like this:

public class LibraryDriver {

    @Autowired 
    private Properties props
    // do stuff with these props
}

How can I make it such that a consuming application can override these properties

like image 907
xheyhenry Avatar asked Mar 11 '26 06:03

xheyhenry


2 Answers

I usually do the following is such scenarios:

public class Properties {
    private int maxConnectingCount; 
    private int maxIdleCount;

    public Properties(String maxConnectingCount, String maxIdleCount) {
        this.maxConnectingCount = maxConnectingCount;
        this.maxIdleCount = maxIdleCount;
    }
}

Then you create the bean like this:

@Configuration
public class LibraryDriverConfiguration {

    @Value("${maxConnectingCount}")
    private int maxConnectingCount;

    @Value("${maxIdleCount}")
    private int maxIdleCount;

    @Bean
    LibraryDriver libraryDriver() {
        return new LibraryDriver(new Properties(maxConnectingCount, maxIdleCount));
}

I like this approach because it allows your properties to have sensible defaults by defining different constructors.

Another option would be to create a Property bean and then autowire it to LibraryDriver; something like:

@Configuration
public class PropertiesConfiguration {

    @Value("${maxConnectingCount}")
    private int maxConnectingCount;

    @Value("${maxIdleCount}")
    private int maxIdleCount;

    @Bean
    Properties properties() {
        return new Properties(maxConnectingCount, maxIdleCount);
    }
}

And then:

@Component
public class LibraryDriver {

    private final Properties properties;

    @Autowired
    public LibraryDriver(Properties properties) {
        this.properties = properties;
    }
}
like image 131
voychris Avatar answered Mar 14 '26 00:03

voychris


The simple way to do is by using @Value()

Suppose you develop a @Component that uses a name property, as shown in the following example:

@Component
public class LibraryProperties {
private int active; 
private int idle;
// .. other properties read from application.properties
//
@Value("${name}")
private String name;
}

On your application classpath (for example, inside your jar) you can have an application.properties file that provides value for name field. When running in a new environment, an application.properties file can be provided outside of your jar that overrides the name.

Hope this helps.

like image 21
Suhas Avatar answered Mar 14 '26 00:03

Suhas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!