3

I have a class which is annotated with @Entry. This annotation accepts two arguments, base and objectClasses. I want to load these two arguments from a static configuration file, application.yaml.

This is the class, src/main/com.my.package/User/User.java:

import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;

import javax.naming.Name;


@Entry(base = "${ldap.organizationalUnitString}" , objectClasses = "${ldap.objectClass}")
public class User {

    @Id
    private Name id;

    private @Attribute(name = "cn") String username;
    private @Attribute(name = "sn") String password;

    //Getters and setters

}

This is my configuration file, src/resources/application.yaml:

ldap:
  partitionSuffix: myPartitionSuffix
  partition: myPartition
  principal: "my Principal"
  password: myPassword
  url: ldap://myURL.url
  organizationalUnitString: "my OU"     
  objectClass: User

The way it's currently written, I get a runtime exception

org.springframework.ldap.InvalidNameException: Invalid name: ${ldap.organizationalUnitString};

I have tried this, https://stackoverflow.com/a/38125875/4640960, using the @Value annotation, but I'm stuck with the compile time error

Type mismatch: cannot convert from Value to String

Is there some way to achieve what I'm trying to? I know that it's possible with a public static final variable, looking like this:

import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;

import javax.naming.Name;

import static com.my.package.User.User.base;
import static com.my.package.User.User.objectClass;


@Entry(base = base , objectClasses = objectClass)
public class User {

    public static final String base = "my OU";
    public static final String objectClass = "User";

    @Id
    private Name id;

    private @Attribute(name = "cn") String username;
    private @Attribute(name = "sn") String password;

    //Getters and setters
}

But I'd rather not hardcode the parameters inside the class.

Several other questions ask about passing non-static variables as annotation arguments, which I know is not possible, but my question is slightly different. I only want to pass a static configuration property.

Magnus
  • 589
  • 8
  • 26
  • 1
    You simply cannot do that. Values in annotations have to be static and your values are dynamic. If it could be done support would have to be build into the annotation processor provided by Spring Ldap and afaik that isn't the case. – M. Deinum Jul 19 '18 at 13:21
  • Ok. Is my best option to hard code the strings into the class? – Magnus Jul 19 '18 at 13:27
  • 1
    @M.Deinum, this is long overdue, but I'd be happy to accept your comment as solution – Magnus Aug 05 '20 at 07:36

0 Answers0