Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.
Get started with mocking and improve your application tests using our Mockito guide:
Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.
Get started with understanding multi-threaded applications with our Java Concurrency guide:
Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:
Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.
But these can also be overused and fall into some common pitfalls.
To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:
Get started with Spring and Spring Boot, through the Learn Spring course:
>> LEARN SPRINGExplore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:
Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.
I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.
You can explore the course here:
Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.
Get started with Spring Data JPA through the guided reference course:
Refactor Java code safely β and automatically β with OpenRewrite.
Refactoring big codebases by hand is slow, risky, and easy to put off. Thatβs where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.
Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions β one for newcomers and one for experienced users. Youβll see how recipes work, how to apply them across projects, and how to modernize code with confidence.
Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.
1. Overview
Netflix Archaius is a powerful configuration management library.
Simply put, itβs a framework that can be used to gather configuration properties from many different sources, offering fast, thread-safe access to them.
On top of this, the library allows properties to change dynamically at runtime, making it possible for the system to get these variations without having to restart the application.
In this introductory tutorial, weβll set up a simple Spring Cloud Archaius configuration, weβll explain whatβs happening under the hood, and finally, weβll see how Spring allows extending the basic setup.
2. Netflix Archaius Features
As we know, Spring Boot already provides instruments to manage externalized configurations, so why bother setting up a different mechanism?
Well, Archaius offers some handy and interesting features that arenβt contemplated by any other configuration framework. Some of its key points are:
- Dynamic and Typed properties
- A Callback mechanism that gets invoked on property mutations
- Ready for use implementations of dynamic configuration sources such as URLs, JDBC and Amazon DynamoDB
- A JMX MBean that can be accessed by Spring Boot Actuator or JConsole to inspect and manipulate the properties
- Dynamic properties validation
These perks can be beneficial in many scenarios.
Therefore, Spring Cloud has worked on a library that allows to easily configure a βSpring Environment Bridgeβ so that Archaius can read properties from the Spring Environment.
3. Dependencies
Letβs add the spring-cloud-starter-netflix-archaius to our application, itβll add all the necessary dependencies to our project.
Optionally, we can also add spring-cloud-netflix to our dependencyManagement section and rely on its specification of the versions of the artifacts:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-archaius</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix</artifactId>
<version>2.0.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Note: we can check Maven Central to verify weβre using the latest version of the starter library.
4. Usage
Once we add the required dependency, weβll be able to access the properties managed by the framework:
DynamicStringProperty dynamicProperty
= DynamicPropertyFactory.getInstance()
.getStringProperty("baeldung.archaius.property", "default value");
String propertyCurrentValue = dynamicProperty.get();
Letβs work on a short example to see how this is available just out-of-the-box.
4.1. Quick Example
By default, it manages dynamically all the properties defined in a file named config.properties in the applicationβs classpath.
So letβs add it to our resources folder with some arbitrary properties:
#config.properties
baeldung.archaius.properties.one=one FROM:config.properties
Now weβll need a way to check the propertiesβ values at any specific moment. In this case, weβll create a RestController that retrieves the values as a JSON response:
@RestController
public class ConfigPropertiesController {
private DynamicStringProperty propertyOneWithDynamic
= DynamicPropertyFactory.getInstance()
.getStringProperty("baeldung.archaius.properties.one", "not found!");
@GetMapping("/property-from-dynamic-management")
public String getPropertyValue() {
return propertyOneWithDynamic.getName() + ": " + propertyOneWithDynamic.get();
}
}
Letβs try it out. We can send a request to this endpoint, and the service will retrieve the values stored in config.properties as expected.
No big deal so far, right? Ok, letβs go on and change the values of the property in the classpath file, without restarting the service. As a result, after a minute or so, a call to the endpoint should retrieve the new values. Pretty cool, isnβt it?
Next, weβll try to understand whatβs going on under the hood.
5. How Does It Work?
First of all, letβs attempt to comprehend the big picture.
Archaius is an extension of the Apacheβs Commons Configuration library, adding some nice features like a polling framework for dynamic sources, with high throughput and thread-safe implementation.
The spring-cloud-netflix-archaius library comes then into play, merging all the different property sources, and auto-configuring the Archaius tools with these sources.
5.1. The Netflix Archaius Library
It operates defining a Composite Configuration, a collection of various configurations obtained from different sources.
Moreover, some of those Configuration Sources might support being polled at runtime for changes. Archaius provides interfaces and some pre-defined implementations to configure these types of sources.
The collection of sources is hierarchized so that if a property is present in multiple configurations, the final value will be the one in the topmost slot.
Finally, a ConfigurationManager handles the system-wide Configuration and deployment context. It can install the final Composite Configuration, or retrieve the installed one for modification.
5.2. Spring Cloud Support
The main task of the Spring Cloud Archaius library is to merge all the different configuration sources as a ConcurrentCompositeConfiguration and install it using the ConfigurationManager.
The order of precedence in which the library defines the sources is:
- Any Apache Common Configuration AbstractConfiguration bean defined in the context
- All the sources defined in the Autowired Spring ConfigurableEnvironment
- The default Archaius sources, which we saw in the example above
- Apacheβs SystemConfiguration and EnvironmentConfiguration sources
Another useful feature this Spring Cloud library provides is the definition of an Actuator Endpoint to monitor and interact with the properties. Its usage is out of the scope of this tutorial.
6. Adapting and Extending the Archaius Configuration
Now that we have a better understanding of how Archaius works, we are in good shape to analyze how to adapt the configuration to our application, or how to extend the functionality using our configuration sources.
6.1. Archaius Supported Configuration Properties
If we want Archaius to take into account other configuration files similar to the config.properties one, we can define the archaius.configurationSource.additionalUrls system property.
The value is parsed to a list of URLs separated by a comma, so, for example, we can add this system property when we launch the application:
-Darchaius.configurationSource.additionalUrls=
"classpath:other-dir/extra.properties,
file:///home/user/other-extra.properties"
Archaius will read the config.properties file first, and then the other ones, in the specified order. Because of this, the properties defined in the latter files will have priority over the prior ones.
There are a couple of other system properties we can use to configure various aspects of the Archaius default configuration:
- archaius.configurationSource.defaultFileName: the default configuration file name in the classpath
- archaius.fixedDelayPollingScheduler.initialDelayMills: initial delay before reading the configuration source
- archaius.fixedDelayPollingScheduler.delayMills: delay between two reads of the source; the default value is 1 minute
6.2. Adding Additional Configuration Sources with Spring
How could we add a different Configuration Source to be managed by the described framework? And how could we manage dynamic properties with higher precedence than the ones defined in the Spring Environment?
Reviewing what we mentioned in section 4.2, we can realize that the highest configurations in the Composite Configuration defined by Spring are the AbstractConfiguration beans defined in the context.
Thus, all we need to do is add an implementation of this Apacheβs abstract class to our Spring Context using some of the functionality provided by Archaius, and the Springβs autoconfiguration will spontaneously add it to the managed configuration properties.
To keep things simple, weβll see an example where we configure a properties file similar to the default config.properties but with the difference of having a higher precedence than the rest of Spring environment and application properties:
@Bean
public AbstractConfiguration addApplicationPropertiesSource() {
URL configPropertyURL = (new ClassPathResource("other-config.properties")).getURL();
PolledConfigurationSource source = new URLConfigurationSource(configPropertyURL);
return new DynamicConfiguration(source, new FixedDelayPollingScheduler());
}
Lucky for us, it contemplates several configuration sources that we can set up with almost no effort. Their configuration is out of the scope of this introductory tutorial.
7. Conclusion
To sum up, weβve learned about Archaius and some of the cool features it offers to take advantage of configuration management.
Also, we saw how the Spring Cloud autoconfiguration library comes into play allowing us to use this libraryβs API conveniently.
