@ConfigurationProperties 在将Spring Boot从2.7迁移到3.0.7后出现问题。

huangapple go评论48阅读模式
英文:

@ConfigurationProperties failing after migrating Springboot from 2.7 to 3.0.7

问题

在从Spring Boot 2.7.4迁移到3.0.7时,下面的代码出现了问题。构造函数的ConfigRepository参数被传递为null,而在Spring Boot 2.7.4中它被正确地填充了正确的bean。

我在迁移指南中没有找到类似这样的更改,除了(https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide#constructingbinding-no-longer-needed-at-the-type-level)之外,但我们没有使用@ConstructorBinding。

@ConfigurationProperties(ConnectorProperties.PREFIX)
public class ConnectorProperties {
    static final String PREFIX = "connector";

    private ConfigRepository configRepository;

    @Value("${graceful-shutdown-period:60}")
    private long gracefulShutdownPeriod;

    public RestConnectorProperties(ConfigRepository configRepository) {
        this.configRepository = configRepository;
    }

    public List<String> getPartners() {
       return configRepository.getPartners();
    }

    public void setGracefulShutdownPeriod(long gracefulShutdownPeriod) {                       
        this.gracefulShutdownPeriod = gracefulShutdownPeriod;                                  
    }

    public long getGracefulShutdownPeriod() {   
        return gracefulShutdownPeriod;          
    }                                                                                                                                 
}

请问是否不再支持这样的操作,或者是否有什么方法可以使其再次工作?

提前感谢。

英文:

When migrating from Springboot 2.7.4 to 3.0.7, the code below is failing. The ConfigRepository argument of the constructor is passed as null while it was filled with the correct bean in Springboot 2.7.4.

I didn't find any changes like this in the migration guide apart from (https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide#constructingbinding-no-longer-needed-at-the-type-level) but we are not using @ConstructorBinding

@ConfigurationProperties(ConnectorProperties.PREFIX)
public class ConnectorProperties {
    static final String PREFIX = &quot;connector&quot;;

    private ConfigRepository configRepository;

    @Value(&quot;${graceful-shutdown-period:60}&quot;)
    private long gracefulShutdownPeriod;


    public RestConnectorProperties(ConfigRepository configRepository) {
        this.configRepository = configRepository;
    }
    
    public List&lt;String&gt; getPartners() {
       return configRepository.getPartners();
    }
    
    public void setGracefulShutdownPeriod(long gracefulShutdownPeriod) {                       
        this.gracefulShutdownPeriod = gracefulShutdownPeriod;                                  
    }    
                                
    public long getGracefulShutdownPeriod() {   
        return gracefulShutdownPeriod;          
    }                                                                                                                                 
}

Could you please indicate if this is not supported anymore or if there is something to do to make it work again ?

Thanks in advance.

答案1

得分: 0

问题实际上是通过将ConfigRepository configRepository标记为自动装配(如Spring Boot 3.0发布说明中所述)来解决的。

> 对于大多数用户,此更新后的逻辑将允许更简单的@ConfigurationProperties类。但是,如果您有一个@ConfigurationProperties,并且希望将bean注入构造函数而不是绑定它,现在您需要添加@Autowired注解。

英文:

Actually the problem is fixed by flagging the ConfigRepository configRepository as autowired as explained in the Spring Boot 3.0 Release Notes

> For most users, this updated logic will allow for simpler @ConfigurationProperties classes. If, however, you have a @ConfigurationProperties and you want to inject beans into the constructor rather than binding it, you’ll now need to add an @Autowired annotation.

huangapple
  • 本文由 发表于 2023年6月14日 23:25:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定