英文:
@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 = "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;
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论