Skip to content
Advertisement

Spring Boot – having all autowired config variables in one class

I am working on a microservice in Spring Boot, and I have a common configuration class where I store all the config variabes that I got from application.properties file. It looks something like this:

Config.java

package programming

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonConfiguration {
    
    @Value("${user-pool.id}")
    private String userPoolId;

    @Value("${client.id}")
    private String clientId;
    ...
    ...
    ...
}
 

Then whenever I need these variables in other classes, I autowire Config.java class, and simply use it like this:

@Autowired
private Config config;

public void method1() {
    ...
    String key = items.get(config.getClientId()).toString();
}

Would having a common configuration class that stores all the autowired variables and use these when needed in other classes a good practice in spring boot? If not, what would be the best way to go about this? Any help would be greatly appreicated. Thank you!

Advertisement

Answer

The better way is to do something ging like this

@ConfigurationProperties(prefix = "optional.key")
Public class MyProperties{
    Int example;
    String example2;
    Getters/setters
}

In your application.properties you can now type

optional.key.example=5

You can the Autowerire MyProperties where ever you need them

Edit It’s also good to add the following dependency to maven, it generates a helper file so that your idea can recognise the defined properties

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
</dependency>

Small addition to replay on the question in the comments You can leave the prefix empty and just annotate you class with @ConfigurationProperties if you don’t want to have a general key Subkeys can be easily handled with subclasses I will post an example of one of my projects here

@ConfigurationProperties(prefix = "swagger")
public class SpringFoxProperties {
    private Info info = new Info();
    private Config config = new Config();

    public Info getInfo() {
        return info;
    }

    public void setInfo(Info info) {
        this.info = info;
    }

    public Config getConfig(){
        return config;
    }

    public void setConfig(Config config) {
        this.config = config;
    }

    public static class Config {
        private String paths = "";
        private String documentationType = "openApi";

        public String getPaths() {
            return paths;
        }

        public void setPaths(String paths) {
            this.paths = paths;
        }

        public String getDocumentationType() {
            return documentationType;
        }

        public void setDocumentationType(String documentationType) {
            this.documentationType = documentationType;
        }
    }

    public static class Info {
        private String title = "";
        private String description = "";
        private String version = "";
        private String termsOfServiceUrl = "";
        private Contact contact = new Contact();
        private License license = new License();

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }

        public String getTermsOfServiceUrl() {
            return termsOfServiceUrl;
        }

        public void setTermsOfServiceUrl(String termsOfServiceUrl) {
            this.termsOfServiceUrl = termsOfServiceUrl;
        }

        public Contact getContact() {
            return contact;
        }

        public void setContact(Contact contact) {
            this.contact = contact;
        }

        public License getLicense() {
            return license;
        }

        public void setLicense(License license) {
            this.license = license;
        }

        public static class Contact {
            private String name  = "";
            private String url  = "";
            private String email  = "";

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getUrl() {
                return url;
            }

            public void setUrl(String url) {
                this.url = url;
            }

            public String getEmail() {
                return email;
            }

            public void setEmail(String email) {
                this.email = email;
            }
        }

        public static class License {
            private String name  = "";
            private String url  = "";

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getUrl() {
                return url;
            }

            public void setUrl(String url) {
                this.url = url;
            }
        }
    }
} 

example application.yml

swagger:
  info:
    title: title
    description: description
    version: version
    termsOfServiceUrl: termsOfServiceUrl
    contact:
      name: name
      url: url
      email: email
    license:
      name: name
      url: url
  config:
    paths: /api/.*
    documentation-type: swagger
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement