xheyhenry
xheyhenry

Reputation: 1069

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

Upvotes: 3

Views: 1153

Answers (2)

voychris
voychris

Reputation: 141

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;
    }
}

Upvotes: 3

Suhas
Suhas

Reputation: 57

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.

Upvotes: 1

Related Questions