Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将配置属性加载到注释参数?_Java_Spring_Spring Boot_Annotations_Configuration Files - Fatal编程技术网

Java 如何将配置属性加载到注释参数?

Java 如何将配置属性加载到注释参数?,java,spring,spring-boot,annotations,configuration-files,Java,Spring,Spring Boot,Annotations,Configuration Files,我有一个用@Entry注释的类。此注释接受两个参数,base和objectClasses。我想从静态配置文件application.yaml加载这两个参数 这是类,src/main/com.my.package/User/User.java: import org.springframework.ldap.odm.annotations.Attribute; import org.springframework.ldap.odm.annotations.Entry; import org.spr

我有一个用
@Entry
注释的类。此注释接受两个参数,
base
objectClasses
。我想从静态配置文件
application.yaml
加载这两个参数

这是类,
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

}
这是我的配置文件,
src/resources/application.yaml

ldap:
  partitionSuffix: myPartitionSuffix
  partition: myPartition
  principal: "my Principal"
  password: myPassword
  url: ldap://myURL.url
  organizationalUnitString: "my OU"     
  objectClass: User
按照当前的编写方式,我得到了一个运行时异常

org.springframework.ldap.InvalidNameException:无效名称: ${ldap.organizationalUnitString}

我已经使用
@Value
注释尝试过了,但是我遇到了编译时错误

类型不匹配:无法从值转换为字符串

有什么方法可以实现我想要的吗?我知道可以使用
公共静态final
变量,如下所示:

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
}
但是我不希望在类中硬编码参数


还有几个问题是关于将非静态变量作为注释参数传递的,我知道这是不可能的,但我的问题略有不同。我只想传递一个静态配置属性。

您根本不能这样做。注释中的值必须是静态的,并且您的值必须是动态的。如果能够做到这一点,则必须将支持构建到SpringLDAP和afaik提供的注释处理器中,事实并非如此。将字符串硬编码到类中是我的最佳选择吗?@M.Deinum,这早该出现了,但我很乐意接受您的意见作为解决方案