【问题标题】:Spring boot relaxed binding not workingSpring Boot 轻松绑定不起作用
【发布时间】:2018-01-17 19:19:13
【问题描述】:

我是弹簧靴的新手。我想在 Spring Boot 中实现轻松的绑定。根据本文档https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-relaxed-binding。 它说,即使我们在 .properties 文件中有带破折号的名称(如名字),它也可以映射到没有破折号的变量(如名字)。但它似乎没有工作。

我有如下 application.properties 文件:

person.first-name=orcl
person.address=xyz 

我的 Properties util java 文件看起来像:

@ConfigurationProperties(prefix="person")
@Component
@PropertySource("file: application.properties")
public class ApplicationPropertiesUtil
{
    private String firstName;
    private String address;

    public String getfirstName()
    {
        return firstName;
    }

    public void setfirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getaddress()
    {
        return address;
    }
    public void setaddress(String address)
    {
        this.address = address;
    }
}

address 属性正在正确绑定,但对于 firstname,它为 null。

【问题讨论】:

  • 将其设置为驼峰式是否有效?您没有像应有的那样严格遵循参考代码firstname -> firstName
  • 我更新了代码,现在使用的是名字。仍然没有约束力
  • 如果您在 src/main/resources 下有 application.properties,则不需要 PropertySource。即使你不这样做,最好使用“classpath:application.properties”。最后,“@Component”是多余的,因为“@ConfigurationProperties”已经被 springs 组件扫描拾取。

标签: java spring spring-boot


【解决方案1】:

问题是您的 setter 方法不符合 java bean 标准。

它应该命名为“setFirstName”,大写F。

【讨论】:

  • 很高兴我能帮上忙
猜你喜欢
  • 2016-03-17
  • 2015-07-09
  • 1970-01-01
  • 2018-01-16
  • 2018-10-15
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多