无法解析值“classpath:/ldap-${spring.profiles.active}.properties”中的占位符“spring.profiles.active”

use*_*155 5 java spring spring-boot application.properties

我正在尝试从 ldap-TEST.properties 文件中读取 ldap 属性并尝试将其绑定到一个 java 配置类。因为我已经指定了 @PropertSource 并为 propertysourcesplaceholderconfigurer 定义了一个静态 Bean。我仍然得到无法解析占位符 'spring.profiles.active' 在价值“classpath:/ldap-${spring.profiles.active}.properties”下面是项目文件请帮助我

@Configuration
@PropertySource("classpath:/ldap-${spring.profiles.active}.properties")
public class LdapConfig { 
 @Autowired
 Environment env;
@Bean
public LdapContextSource contextSource() {
    LdapContextSource contextSource = new LdapContextSource();
    contextSource.setUrl(env.getRequiredProperty("ldap.url"));
    contextSource.setBase(env.getRequiredProperty("ldap.base"));
    contextSource.setUserDn(env.getRequiredProperty("ldap.userDn"));
    contextSource.setPassword(env.getRequiredProperty("ldap.password"));
    contextSource.afterPropertiesSet();
    return contextSource;
}

@Bean
public LdapTemplate ldapTemplate() {
    return new LdapTemplate(contextSource());
}

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}
Run Code Online (Sandbox Code Playgroud)

}

//ldap-TEST.properties file
ldap.base=dc=example,dc=com
ldap.password=password
ldap.port=839
ldap.userDn=cn=read-only-admin,dc=example,dc=com
ldap.url=ldap://ldap.forumsys.com:389
Run Code Online (Sandbox Code Playgroud)

我的主要应用

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
       SpringApplication.run(Application.class, args);
 }
Run Code Online (Sandbox Code Playgroud)

}

Mah*_*abi 2

您不能${spring.profiles.active}在 spring 中使用类型注释的内部字符串值之类的属性。这些属性将被注入到注释中,就像@Value属性或方法的注释一样。