我正在尝试从 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/