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

标签 java spring spring-boot application.properties

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

@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();
}

}

//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

我的主要应用

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
       SpringApplication.run(Application.class, args);
 }

}

最佳答案

您不能在 Spring 的 Type 注释的字符串值内使用诸如 ${spring.profiles.active} 之类的属性。这些属性将被注入(inject)到诸如 @Value 之类的用于属性或方法的注释中。

关于java - 无法解析值 'spring.profiles.active' 中的占位符 "classpath:/ldap-${spring.profiles.active}.properties",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51943584/

相关文章:

java - Spring Cloud Gateway 不适用于 @Bean DiscoveryClientRouteDefinitionLocator

java - DefaultOidcUser 不能转换为类 CustomOAuth2User

java - 作业: "Exception in thread "main"java. lang.NullPointerException”

java - 如何在 jsp 上显示呈现为图像的 jfreecharts 的工具提示

spring - SpEL可以与spring xml配置中的import语句一起使用吗

java - 在 Spring Beans 中初始化字段的正确方法是什么?

java - 映射到许多值

java - Spring 5 处理 null Beans 的方式发生了变化?

java - 如果未使用所有依赖项 jar 进行编译,上传到 Apache Livy 的应用程序将失败

java - Liskov 替换原则 VS 接口(interface)隔离原则