我有一个Spring Boot Configuration类,如下所示:
package foo.bar;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
@Configuration @ConfigurationProperties(prefix = "myConfig") public class MyConfig
{
private String foo;
private int myValue;
@NotNull private String requiredString;
@Min(0) @Max(100) private int smallPositiveInt;
public void setFoo(String foo) { this.foo = foo; }
public void setMyValue(int myValue) { this.myValue = myValue; }
public void setRequiredString(String requiredString) { this.requiredString = requiredString; }
public void setSmallPositiveInt(int smallPositiveInt)
{
this.smallPositiveInt = smallPositiveInt;
}
public String getRequiredString() { return requiredString; }
public int getSmallPositiveInt() { return smallPositiveInt; }
public String getFoo() { return foo; }
public int getMyValue() { return myValue; }
}
和YAML配置文件看起来像:
server:
port: 0
myConfig:
myValue: 9876543
foo: Goodbye
requiredString: Here
---
我的SpringApplication代码如下所示:
package foo.bar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication public class MyService implements ApplicationRunner
{
// Define the logger object for this class
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired private MyConfig myConfig;
// myConfig is null when I uncomment these lines:
// @Bean
// public Validator configurationPropertiesValidator()
// {
// return new MyConfigValidator();
// }
@Autowired ApplicationContext applicationContext;
@Override public void run(ApplicationArguments applicationArguments) throws Exception
{
log.info("Running application...");
log.info("MyConfig values:");
log.info("foo=" + myConfig.getFoo());
log.info("myValue=" + myConfig.getMyValue());
log.info("smallPositiveInt=" + myConfig.getSmallPositiveInt());
log.warn("Example warning log message.");
log.error("Example error log message.");
log.debug("Example debug log message.");
}
public static void main(String[] args) { SpringApplication.run(MyService.class, args); }
}
当我取消注释验证器时,自动装配的配置为空,但是当它被注释掉时它可以正常工作。任何想法可能会发生依赖注入?
MyConfigValidator现在完全空白,只是实现了Validator而没有实际功能我可以发布它,如果有人认为可能是问题。
更新 当我调试代码时,我可以看到在MyConfigValidator中调用了validate(),其中MyConfig对象具有YAML文件中的正确值。当Spring作为Bean包含在代码中时,Spring是否有可能通过Spring Boot Application将此对象注入MyConfigValidator?
更新2: 在查看Spring Boot property validation example之后,我通过创建一个实现ApplicationRunner的静态类来实现这一点。但是,我不明白为什么这是必要的,并希望避免这样做。
答案 0 :(得分:2)
我认为myConfig
为空的原因是因为configurationPropertiesValidator
提前实例化了。这导致MyService
在AutowiredAnnotationBeanPostProcessor
可用于注入字段之前实例化。
如果你将configurationPropertiesValidator()
方法设为静态,那么事情应该可以正常工作:
@SpringBootApplication
public class MyService implements ApplicationRunner {
// ...
@Bean
public static Validator configurationPropertiesValidator() {
return new MyConfigValidator();
}
// ...
}
答案 1 :(得分:1)
似乎对我的项目最有效的方法是将验证器嵌入到配置类中,如下所示:
@Component
@ConfigurationProperties(prefix = "myConfig")
public class MyConfig implements Validator
{
private String foo;
private int myValue;
private String requiredString;
private int smallPositiveInt;
private final Logger log = LoggerFactory.getLogger(this.getClass());
// This means that this validator only supports validating configs of type "MyConfig".
@Override public boolean supports(Class<?> type) { return type == MyConfig.class; }
@Override public void validate(Object o, Errors errors)
{
MyConfig c = (MyConfig)o;
log.info("Validating: " + c.toString());
if(c.getSmallPositiveInt() == 60)
{
errors.rejectValue("smallPositiveInt", "error", "Cannot be 60!");
}
}
}
请告诉我使用此方法的任何缺点,它似乎是使用多个配置对象时的最佳解决方案,因为它们似乎在通过Spring依赖注入创建时进行验证。