1

application.properties file contains properties that have sub properties:

status.available=00, STATUS.ALLOWED
status.forbidden=01, STATUS.FORBIDDEN
status.authdenied=05, STATUS.AUTH_DENIED

The idea was to get those properties into the application like this:

@Configuration
@ConfigurationProperties(prefix = "status")
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
public class StatusProperties {

    private Map <String, List <String>> statusMapping;

    public Map <String, List <String>> getStatusMapping () {
        return statusMapping;
    }

    public void setStatusMapping (Map <String, List <String>> statusMapping) {
        this.statusMapping = statusMapping;
    }
}

The problem is that this Map is returned empty. I must be doing something wrong. Maybe this is not even possible in Spring to do like this?

2 Answers 2

3

I'm not sure about your choice regarding the data type and its assignment. I'd suggest you to rethink this design.


To your main question:

Spring can't know, that status.* should be mapped to private Map <String, List <String>> statusMapping;. Also as your class is named *properties, It seems that you don't want it to be a @Configuration class. Consider the following pattern:

First, create a properties class to hold the properties:

@ConfigurationProperties(prefix = "status")
public class StatusProperties {

    private Map.Entry<Integer, String> available;
    private Map.Entry<Integer, String> forbidden;
    private Map.Entry<Integer, String> authdenied;

    public Map.Entry<Integer, String> getAvailable() {
        return available;
    }

    public void setAvailable(Map.Entry<Integer, String> available) {
        this.available = available;
    }

    public Map.Entry<Integer, String> getForbidden() {
        return forbidden;
    }

    public void setForbidden(Map.Entry<Integer, String> forbidden) {
        this.forbidden = forbidden;
    }

    public Map.Entry<Integer, String> getAuthdenied() {
        return authdenied;
    }

    public void setAuthdenied(Map.Entry<Integer, String> authdenied) {
        this.authdenied = authdenied;
    }
}

Now, your IDE should be able to read the docs from the setters while editing application.properties and check the validity. Spring can autowire the fields and automatically create the correct data types for you.

Consider mapping the Entries to a Map (Or, as I already told, change the design)

Now, you can use this properties class in your configuration:

@Configuration
@EnableConfigurationProperties(StatusProperties.class)
public class StatusConfiguration {
    @Bean
    public MyBean myBean(StatusProperties properties) {
        return new MyBean(properties);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is the standard way of doing. The idea here was to dynamically insert properties without Spring knowing the specific types of properties. That way a user can modify the application.properties file but doesn't need to modify the code.
Than you could even go back to using Objects or Strings as Data Type. IMHO the strict typing of app props is a great benefit
I found a solution to my problem, it was here: mkyong.com/spring-boot/… I'm going to post the answer.
Which is what I meant, suggesting to change your design
2

I found the solution:

application.properties:

app.statuses[0].id=00
app.statuses[0].title=STATUS.ALLOWED
app.statuses[1].id=01
app.statuses[1].title=STATUS.FORBIDDEN
app.statuses[2].id=02
app.statuses[2].title=STATUS.CONTRACT_ENDED

Properties.java

@Component
@ConfigurationProperties(prefix = "app")
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
public class StatusProperties {

    private List<Status> statuses = new ArrayList<>();

    public List <Status> getStatuses () {
        return statuses;
    }

    public void setStatuses (List <Status> statuses) {
        this.statuses = statuses;
    }

    public static class Status {
        private String id;
        private String title;

        public String getId () {
            return id;
        }

        public void setId (String id) {
            this.id = id;
        }

        public String getTitle () {
            return title;
        }

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.