Spring @Validated注释可以防止@ConfigurationProproperties值注入,为什么?

时间:2019-04-02 09:45:28

标签: spring spring-boot bean-validation configurationproperties

我正在尝试@ConfigurationProperties,发现两件事:

  • 要使测试正常工作,我必须将配置属性文件(yml)放在test包中; setter和一个空的构造函数是必需的。
  • 如果我用@Validated注释了该类,则所有null值的注入都会失败。

如果您说第一个是可以理解的,第二个是可以理解的?为什么?

目的是验证配置属性的注入,以便在应用程序启动时不缺少关键的属性值。

我要映射这个Yaml:

accertify:
  fr:
    merchant1:
      host: "sandbox.accertify.dev"
      protocol: "https"
      user: "fraud"
      password: "temporal"
      merchant: "merchant1FR"
    merchant2:
      host: "sandbox.accertify.dev"
      protocol: "https"
      user: "fraud"
      password: "temporal"
      merchant: "merchant2FR"
  es:
    merchant1:
      host: "sandbox.accertify.dev"
      protocol: "https"
      user: "fraud"
      password: "temporal"
      merchant: "merchant1ES"
    merchant2:
      host: "sandbox.accertify.dev"
      protocol: "https"
      user: "fraud"
      password: "temporal"
      merchant: "merchant2ES"

我有这个课程可以映射嵌套配置:

import com.westerngun.fraud.jfps.dto.provider.accertify.country.ConfigurationEs;
import com.westerngun.fraud.jfps.dto.provider.accertify.country.ConfigurationFr;
import com.westerngun.fraud.jfps.service.client.ConnectionDetailsInterface;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;

/**
 * Class to read Accertify provider connection credentials.
 */
@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "accertify")
@Validated   // <--- this seems the problem
@NoArgsConstructor
@AllArgsConstructor
public class AccertifyConnectionDetail extends ConnectionDetails {

    @Valid
    private ConfigurationFr fr;

    @Valid
    private ConfigurationEs es;
}

而且,它的界面:

public abstract class ConnectionDetails {
    public abstract ConfigurationFr getFr();
    public abstract ConfigurationEs getEs();
}

而且ConfigurationEsConfigurationFr都是以下子类:

@Component
public class CountryConfigDetail {
    @Getter
    @Setter
    @Valid
    private Merchant1 merchant1;

    @Getter
    @Setter
    @Valid
    private Merchant2 merchant2;
}

Merchant1Merchant2是的子类:

@Data
@Component
public class MerchantConfigDetail {

    @NotEmpty
    @NotNull
    private String user;

    @NotEmpty
    @NotNull
    private String password;

    ...
    @NotEmpty
    @NotNull
    private Map<String, String> headers;
}

当我尝试在第一堂课中插入一些@Autowired测试而没有@Validated时,它会起作用(fres在yml文件中有值)

enter image description here

当我添加@Validated时,fres为空。

测试:

@RunWith(SpringRunner.class)
@SpringBootTest
public class AccertifyConfigurationTest {

    @Autowired
    private AccertifyConnectionDetail accertifyConnectionDetail; // <--- this field should be injected


    @Test
    public void givenAAccertifyClientService_WhenClientIsBuild_ShouldHaveTheProviderConfigurationCorrect() {
        AccertifyConfiguration accertifyConfiguration = new AccertifyConfiguration(accertifyConnectionDetail);
        accertifyConfiguration.buildConfiguration("FR", "Merchant1");

        Assert.assertEquals("fraud", accertifyConfiguration.getUser());
        Assert.assertEquals("temporal", accertifyConfiguration.getPassword());
        Assert.assertTrue(accertifyConfiguration.getHost().startsWith("sandbox.accertify."));
        Assert.assertEquals("merchant1FR", accertifyConfiguration.getMerchantCode());
    }
...

0 个答案:

没有答案