您可以使用@ConfigurationProperties
它并确保它是@Context
范围,因为默认情况下,Micronaut bean 不会在启动时创建。通过使它们成为@Context
范围,将在应用程序启动时创建 bean。
以下:
import io.micronaut.context.annotation.*;
import javax.validation.constraints.*;
@ConfigurationProperties("my")
@Context
class MyConfig {
private @NotNull String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
结果是
08:38:38.568 [main] ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Bean definition [fresh.java.MyConfig] could not be loaded: Error instantiating bean of type [fresh.java.MyConfig]
Message: Validation failed for bean definition [fresh.java.MyConfig]
List of constraint violations:[
url - must not be null
]
服务器启动时。
确保添加此依赖项
compile "io.micronaut.configuration:micronaut-hibernate-validator"