我正在尝试@ConfigurationProperties
,发现两件事:
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();
}
而且ConfigurationEs
和ConfigurationFr
都是以下子类:
@Component
public class CountryConfigDetail {
@Getter
@Setter
@Valid
private Merchant1 merchant1;
@Getter
@Setter
@Valid
private Merchant2 merchant2;
}
Merchant1
和Merchant2
是的子类:
@Data
@Component
public class MerchantConfigDetail {
@NotEmpty
@NotNull
private String user;
@NotEmpty
@NotNull
private String password;
...
@NotEmpty
@NotNull
private Map<String, String> headers;
}
当我尝试在第一堂课中插入一些@Autowired
测试而没有@Validated
时,它会起作用(fr
和es
在yml文件中有值)>
当我添加@Validated时,fr
和es
为空。
测试:
@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());
}
...