How to Use Nested @ConfigurationProperties for Multiple Connections in Spring Boot

Question

Is it possible to configure multiple connections in Spring Boot using nested @ConfigurationProperties?

# application.yml
cassandra:
  connections:
    default:
      driver:   "%database_driver%"
      host:     "%database_host%"
      port:     "%database_port%"
      dbname:   "%database_name%"
      user:     "%database_user%"
      password: "%database_password%"
      charset:  UTF8
    customer:
      driver:   "%database_driver2%"
      host:     "%database_host2%"
      port:     "%database_port2%"
      dbname:   "%database_name2%"
      user:     "%database_user2%"
      password: "%database_password2%"
      charset:  UTF8

Answer

Yes, it is possible to configure multiple connections in Spring Boot using the @ConfigurationProperties annotation with nested classes. This mechanism allows you to map complex configuration properties directly to Java objects, making it easier to manage and use them in your application.

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@ConfigurationProperties(prefix = "cassandra")
public class CassandraProperties {
    private Map<String, ConnectionProperties> connections = new HashMap<>();

    public Map<String, ConnectionProperties> getConnections() {
        return connections;
    }

    public static class ConnectionProperties {
        private String driver;
        private String host;
        private String port;
        private String dbname;
        private String user;
        private String password;
        private String charset;

        // Getters and setters...
    }
}

@Configuration
public class ApplicationConfig {
    @Bean
    public CassandraProperties cassandraProperties() {
        return new CassandraProperties();
    }
}

Solutions

  • Define a class to hold connection settings with nested classes for each connection type.
  • Use the @ConfigurationProperties annotation to bind properties from your application.yml or application.properties file to your classes.
  • Create a configuration class to enable the properties binding.

Common Mistakes

Mistake: Not using the correct prefix in @ConfigurationProperties.

Solution: Ensure that the prefix in your @ConfigurationProperties annotation matches the root key in your configuration file.

Mistake: Forgetting to add getters and setters in the nested class.

Solution: Make sure to include all necessary getter and setter methods for accessing properties.

Helpers

  • Spring Boot
  • ConfigurationProperties
  • nested properties
  • multiple connections
  • application.yml
  • Cassandra connection
  • Spring Boot configuration

Related Questions

⦿How to Efficiently Write Large CSV Files in Java?

Discover the best practices for writing large CSV files in Java including performance optimization techniques and efficient code examples.

⦿How to Link to a Class in Another Package Using Javadoc?

Learn how to correctly use Javadoc to link to classes in different packages in Java including syntax and common mistakes.

⦿What Does the $1 Suffix in Java Class Files Indicate?

Discover the meaning behind the 1 suffix in Java class files its causes and solutions. Understand inner classes and their implications.

⦿How to Read the Excel Cell Value Instead of the Formula Using Java POI?

Learn how to retrieve the actual value of an Excel cell instead of the formula using Apache POI. Solutions to common issues included.

⦿How to Combine Formatted Strings with Placeholders and HTML Tags in Android?

Learn how to use formatted strings with placeholders and HTML tags in Android. Detailed explanations with code examples.

⦿How to Inject User Input Parameter into a Dagger 2 Object?

Learn how to inject a userinputted String parameter into a Dagger 2managed object alongside other dependencies.

⦿How to Create a Deep Copy of a HashMap in Java

Learn how to create a deep copy of HashMapInteger ListMySpecialClass in Java ensuring the original map remains unchanged when modifying the copy.

⦿How to Replace Newline Characters with <br /> in Java

Learn how to effectively replace newline characters in Java strings with HTML break tags using regex.

⦿How Can I Rotate JPEG Images Based on Orientation Metadata?

Learn how to properly rotate JPEG images based on EXIF orientation metadata for accurate thumbnail generation.

⦿How to Access Nested Elements of a JSON Object Using the getJSONArray Method?

Learn how to successfully access nested JSON elements using getJSONArray method in Java and tackle common issues like JSONException.

© Copyright 2025 - CodingTechRoom.com

close