3

I have a application.properties:

app.cert.identity.subject.organizationalUnit=test
app.cert.identity.subject.O=Pacific College
app.cert.identity.subject.L=CanTho
app.cert.identity.subject.ST=CanTho
app.cert.identity.subject.C=VN

My class:

@Configuration
@ConfigurationProperties(prefix = "app.cert.identity")
@EnableConfigurationProperties
@Data
public class IdentityCertificateDefinition {

    private Subject subject;

    @Data
    @Configuration
    public static class Subject {

        private String organizationalUnit;    //Does work

        @Value("${app.cert.identity.subject.O}")    //Does NOT work
        private String organization;

        @Value("${app.cert.identity.subject.L}")    //Does NOT work
        private String location;

        @Value("${app.cert.identity.subject.ST}")    //Does NOT work
        private String state;

        @Value("${app.cert.identity.subject.C}")    //Does NOT work
        private String countryCode;

        @Value("${app.cert.identity.validity.not-after-in-days}")    //Does NOT work
        private int notAfterInDays;

    }

}

And here is the result: enter image description here You guys can see just the organizationalUnit work, the rest doesn't work (all are null). I do not know how to make the rest properties work. I would like to keep application.properties.

10
  • I think the application.properties file have some conflicts with SPACE and _ underscore So we cannot add them in properties file. So please try to remove it Commented Nov 17, 2020 at 11:09
  • or for testing try on thing app.cert.identity.subject.O=Pacific College move this from second position to last position. You will observe all the properies values are accessable but not the last one Commented Nov 17, 2020 at 11:09
  • Hi, I think it's not a problem. I just need to change the properties name to match with the fields in java code and it works, but I don't prefer that way. I want to keep property file
    – SoT
    Commented Nov 17, 2020 at 11:12
  • is the problem resolved ?? Commented Nov 17, 2020 at 11:16
  • 1
    The problem should be solved easily by @Milgo's comment, or I just need to rename the properties to match with the java fields, but both solutions here are not my expectation
    – SoT
    Commented Nov 17, 2020 at 11:19

2 Answers 2

2

You can use the static class configuration with this code:

@Configuration
@Data
public class IdentityCertificateDefinition {

    @Autowired
    private Subject subject;

    @Data
    @Configuration
    public static class Subject {

        private String organizationalUnit;

        @Value("${app.cert.identity.subject.O}")
        private String organization;

        @Value("${app.cert.identity.subject.L}")
        private String location;

        @Value("${app.cert.identity.subject.ST}")
        private String state;

        @Value("${app.cert.identity.subject.C}")
        private String countryCode;

        @Value("${app.cert.identity.validity.not-after-in-days}")
        private int notAfterInDays;
    }
}

If you don't need to use a static class in a configuration class, just use:

@Configuration
@Data
public class IdentityCertificateDefinition {

    @Value("${app.cert.identity.subject.OU}")
    private String organizationalUnit;

    @Value("${app.cert.identity.subject.O}")
    private String organization;

    @Value("${app.cert.identity.subject.L}")
    private String location;

    @Value("${app.cert.identity.subject.ST}")
    private String state;

    @Value("${app.cert.identity.subject.C}")
    private String countryCode;

    @Value("${app.cert.identity.validity.not-after-in-days}")
    private int notAfterInDays;
}
2
  • Yes, since I have a lot of properties between identity and subject. I just did not put the code here (to make the content shorter)
    – SoT
    Commented Nov 17, 2020 at 10:12
  • It's a bit clunky and I'd definitely prefer the second configuration but it's a valid configuration. Since there is no 'official' way, you can use it if you prefer/need it this way.
    – Milgo
    Commented Nov 18, 2020 at 7:27
2

The problem should be related to

@ConfigurationProperties(prefix = "app.cert.identity")

Mainly you're say the properties have a common prefix, but then when you inject their values your putting the prefix again:

 @Value("${app.cert.identity.subject.L}")

So Spring is acting to find a property named (prefix + class + value of @Value):

 @Value("${app.cert.identity.subject.app.cert.identity.subject.L}")

Change

@Value("${app.cert.identity.subject.L}")

To

@Value("${L}")
1
  • Error creating bean with name 'identityCertificateDefinition.Subject': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'O' in value "${O}"
    – SoT
    Commented Nov 17, 2020 at 10:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.