Alexandre Liscia
Alexandre Liscia

Reputation: 328

Get application.yml infos in spring

I have add some config inside my application.yml file and I want to read it from my Java code.

The added node inside the YAML file looks like this:

myConfig:
    projectOne:
        mantisID: 501
        user: username
        password: passwd
    projectTwo:
        mantisID: 502
        user: username
        password: passwd

What I want is to get a List of Project objects where

Project.mantisID = 501,
Project.user = "username",
Project.password = "passwd",

etc...

I know spring can read this file with some @Value annotation but how can I use this in order to get what I need?

Upvotes: 3

Views: 5509

Answers (2)

Alexandre Liscia
Alexandre Liscia

Reputation: 328

Just to finish, I answered myself to my second question. This is what my service looks like now :

@Service
public class MantisProjectService {

    private final Map<String, MantisProjectConfiguration.Project> projects;

    private List<MantisProjectConfiguration.Project> mantisProjects = new ArrayList<>();

    @Autowired
    public MantisProjectService(MantisProjectConfiguration mantisProjectConfiguration)
    {
        this.projects = mantisProjectConfiguration.getMantisProjectConfiguration();

        for (Map.Entry<String, MantisProjectConfiguration.Project> project : projects.entrySet())
        {
            MantisProjectConfiguration.Project mantisProject = project.getValue();
            mantisProject.setName(project.getKey());
            mantisProjects.add(mantisProject);
        }
    }

    public List<MantisProjectConfiguration.Project> getMantisProjects()
    {
        return mantisProjects;
    }

}

It returns a List of all the projects. And it is awesome! =)

Upvotes: 0

Edd
Edd

Reputation: 2032

You can use @ConfigurationProperties annotation to map your configuration to a Bean, then you'll be able to inject your Bean anywhere and fetch those properties.

To do so, first create a class which represents the data structure in your configuration. Then annotate it with @ConfigurationProperties and @Configuration annotations.

@Configuration
@ConfigurationProperties
public class MyConfig {

    private final Map<String, Project> myConfig = new HashMap<>();

    public Map<String, Project> getMyConfig() {
        return myConfig;
    }

    public static class Project {

        private String mantisID;
        private String password;
        private String user;

        // Getters and setters...
    }
}

Note that getters and setters are required in the Project class. Also keep in mind that naming of getters and setters is important here.

After you have setup this class, you can inject it anywhere in your project and access its properties.

@Service
public class SomeService {

    private final Map<String, MyConfig.Project> projects;

    @Autowired
    public SomeService(MyConfig config) {
        this.projects = config.getMyConfig();

        projects.get("projectOne").getMantisID();
        projects.get("projectTwo").getPassword();
    }
}

You can read more about this here.

Upvotes: 2

Related Questions