【问题标题】:Could not resolve placeholder 'spring.profiles.active' in value "classpath:/ldap-${spring.profiles.active}.properties"无法解析值“classpath:/ldap-${spring.profiles.active}.properties”中的占位符“spring.profiles.active”
【发布时间】:2019-01-27 08:11:26
【问题描述】:

我正在尝试从ldap-TEST.properties 文件中读取 ldap 属性 并尝试将其绑定到我指定的 java config class.for @PropertSource 并为 propertysourcesplaceholderconfigurer 定义了一个静态 Bean。 我仍然得到无法解析占位符spring.profiles.active in value 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();
}

}

//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.profiles.active} 的值吗?
  • -Dspring.profiles.active=TEST 在启动应用程序时传递参数
  • @OresteViron in application.properties 我定义了 spring.profiles.active=TEST
  • @Abdul 我如何通过 -Dspring.profiles.active=TEST 我正在将我的应用程序部署到 jboss 我没有从命令行运行你能告诉我我在哪里可以提供参数

标签: java spring spring-boot application.properties


【解决方案1】:

你不能在 spring 的 Type 注释的字符串值中使用像 ${spring.profiles.active} 这样的属性。此类属性将被注入到 @Value 等用于属性或方法的注释中。

【讨论】:

    【解决方案2】:

    spring.profiles.active 后面的值实际上是一个数组。因此,即使该值被正确扩展,也可能会出现无法按您想要的方式工作的极端情况。

    如果通过@PropertySource 配置的路径与application.properties|yml 的工作方式相同,那就太好了,但目前情况并非如此(an active issue on GitHub 关于这一点)。所以必须考虑替代方案:

    1. 最简单的替代方法是使用常规文件名application.properties|ymlapplication-{profile}.properties|yml。我看不出有什么理由不这样做,但我不知道您的项目要求,所以......
    2. 稍微复杂一点,使用 Java 代码获取配置好的配置文件,并以编程方式配置 Spring 环境。 See this SO answer for more details

    【讨论】:

    • 我刚刚替换了 ${spring.profiles.active} ${test.profile} 并调用了@PropertySource("classpath:/ldap-${test.profile}.properties") 定义的测试。 prfoile=TEST 我仍然得到同样的错误
    • 这很奇怪,按照你的描述,它应该可以工作。虽然我不确定 Mahdi 的回答是否正确(我没有在 Spring 文档中看到它说可以使用 ${spring.profiles.active},但也没有说它也不会),但使用另一个变量是消除这个潜在的原因。看起来你的application.properties 甚至没有被阅读。难道是根本就没有放对地方?
    • 我的意思是如果你把注解放在类上,而不是因为它的属性或方法,你可能无法在字符串中使用属性。其他原因是您正在定义属性资源,因此第一步是读取资源,第二步是将资源值放入字符串中定义的属性中。我的问题是,在没有指定属性资源的情况下,如何将值放入字符串中?你当然做不到?
    • Spring 中这类变量的值可以来自很多地方,包括环境变量或命令行参数,Spring 在应用程序启动时就可以读取这些变量。一个很好的问题确实是在application.properties 中的变量是否可以在那里使用,你可能是对的,事实并非如此。 ? 但是我实际上使用了与过去的问题类似的策略(@PropertySource 中的变量),并且它至少在通过命令行参数或 Tomcat 上下文配置提供的值的情况下确实有效。
    【解决方案3】:

    如果您在本地机器上运行,那么只需更改属性文件名 application-default.property 即可临时立即工作。

    对于永久解决方案,您必须检查您的项目是否在 docker 上运行:

    如果在 docker 上运行,则使用以下命令:

    1. mvn clean package -DskipTests=true && sudo docker build
    2. sudo docker run -d -e spring.profiles.active="local" -e <other key and value of bootstrap.property file>

    如果在云服务器上运行,请按照以下步骤操作:

    1. 在 bootstrap.property 文件中放入 spring.profiles.active=local
    2. 将应用程序文件重命名为application-local.properties

    您也可以参考:2.3.3. Profile Specific Files 部分configuration document

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-24
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      相关资源
      最近更新 更多