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.