如何让ChangeUnit与我的应用中的其他Spring Bean一起使用?我尝试过通过构造注入和setter注入向ChangeUnit添加依赖项,并使用post构造来构建我想要播种的数据。不过我看着Caused by: java.lang.IllegalArgumentException: documents can not be null
运行更改单元时出现异常,位于第
mongoDatabase.getCollection("form_layouts", FormLayout.class)
.insertMany(clientSession, formLayouts)
.subscribe(insertSubscriber);
这里的代码
private List<FormLayout> formLayouts;
@Autowired
private Jackson2ObjectMapperBuilder objectMapperBuilder;
@Autowired
private AppProperties appProperties;
@Value("classpath:form_layouts.json")
private Resource layouts;
@PostConstruct
public void initializeLayoutsJson() throws IOException {
ObjectMapper objectMapper = objectMapperBuilder.failOnUnknownProperties(Boolean.TRUE).build();
this.formLayouts = objectMapper.readValue(Files.readString(Path.of(layouts.getFile().getAbsolutePath())), new TypeReference<>() {
});
for (FormLayout layout : formLayouts) {
layout.setRealmId(appProperties.getRealmId());
}
log.debug("Loaded {} Layouts from file seeder JSON {}", formLayouts.size(), layouts.getFilename());
int i = 0;
for (FormLayout layout : formLayouts) {
i++;
log.debug("Layout-{} : {}", i, layout);
}
}
@Execution
public void migrationMethod(ClientSession clientSession, MongoDatabase mongoDatabase) {
final SubscriberSync<InsertManyResult> insertSubscriber = new MongoSubscriberSync<>();
mongoDatabase.getCollection("form_layouts", FormLayout.class)
.insertMany(clientSession, this.formLayouts)
.subscribe(insertSubscriber);
InsertManyResult result = insertSubscriber.getFirst();
log.info("{}.execution() wasAcknowledged: {}", this.getClass().getSimpleName(), result.wasAcknowledged());
result.getInsertedIds()
.forEach((key, value) -> log.info("Added Object[{}] : {}", key, value));
}
我还尝试了构造函数注入
private final List<FormLayout> formLayouts;
public LayoutsDataInitializer(@Autowired Jackson2ObjectMapperBuilder objectMapperBuilder, @Autowired AppProperties appProperties,
@Value("classpath:form_layouts.json") Resource layouts) throws IOException {
ObjectMapper objectMapper = objectMapperBuilder.failOnUnknownProperties(Boolean.TRUE).build();
this.formLayouts = objectMapper.readValue(Files.readString(Path.of(layouts.getFile().getAbsolutePath())), new TypeReference<>() {
});
for (FormLayout layout : formLayouts) {
layout.setRealmId(appProperties.getRealmId());
}
}
@Execution
public void migrationMethod(ClientSession clientSession, MongoDatabase mongoDatabase) throws IOException {
final SubscriberSync<InsertManyResult> insertSubscriber = new MongoSubscriberSync<>();
mongoDatabase.getCollection("form_layouts", FormLayout.class)
.insertMany(clientSession, formLayouts)
.subscribe(insertSubscriber);
InsertManyResult result = insertSubscriber.getFirst();
log.info("{}.execution() wasAcknowledged: {}", this.getClass().getSimpleName(), result.wasAcknowledged());
result.getInsertedIds()
.forEach((key, value) -> log.info("Added Object[{}] : {}", key, value));
}
这给出了一个关于ChangeLogRuntimeImpl
中的错误Dependency Caused by: io.mongock.driver.api.common.DependencyInjectionException: Wrong parameter[Resource]. Dependency not found.
的相当隐晦的错误。
将整个逻辑移动到执行方法中也没有帮助。
@Value("classpath:form_layouts.json")
Resource layouts;
@Execution
public void migrationMethod(ClientSession clientSession, MongoDatabase mongoDatabase) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.TRUE);
List<FormLayout> formLayouts = objectMapper.readValue(Files.readString(Path.of(layouts.getFile().getAbsolutePath())), new TypeReference<>() {
});
for (FormLayout layout : formLayouts) {
layout.setRealmId(REALM_ID);
}
final SubscriberSync<InsertManyResult> insertSubscriber = new MongoSubscriberSync<>();
mongoDatabase.getCollection("form_layouts", FormLayout.class)
.insertMany(clientSession, formLayouts)
.subscribe(insertSubscriber);
InsertManyResult result = insertSubscriber.getFirst();
log.info("{}.execution() wasAcknowledged: {}", this.getClass().getSimpleName(), result.wasAcknowledged());
result.getInsertedIds()
.forEach((key, value) -> log.info("Added Object[{}] : {}", key, value));
}
失败,因为布局对象被报告为错误。
1条答案
按热度按时间kpbwa7wx1#
Mongock支持Spring依赖注入。正如文档中所解释的,有两种方法,在构造函数和方法本身中。如果你有多个构造函数,你需要通过使用
ChangeUnitConstructor
注解来向Mongock指明你想要使用哪一个。请注意,您不需要使用
@Autowired
注解注入。如果它被注入到spring上下文中,Mongock足够聪明,可以推断出它。然而,Mongock支持的是注解
@Named
,这将使Mongock通过类型和bean名称来查看依赖项。