gpt4 book ai didi

java - spring bean 配置中的微流 EmbeddedStorageManager

转载 作者:行者123 更新时间:2023-12-01 23:22:22 25 4
gpt4 key购买 nike

我想在 spring boot 应用程序 (2.5.0) 中将 microstream 的 EmbeddedStorageManager 配置为 bean。

@Configuration
public class MicrostreamConfig {

@Value("${microstream.store.location}")
String location;

@Bean
DataRoot dataRoot() {
DataRoot dataRoot = new DataRoot();
dataRoot.setProjectList(new ArrayList<>());
return dataRoot;
}

@Bean
public EmbeddedStorageManager storageManager() {

EmbeddedStorageManager storageManager = EmbeddedStorage.start(
dataRoot(), // root object
Paths.get(location) // storage directory
);
return storageManager;
}
}

并将其注入(inject)到存储库类中

@Component
public class DataRepository {

@Autowired
private DataRoot dataRoot;

@Autowired
private EmbeddedStorageManager storageManager;

public void addProject(Project project) {
dataRoot.getProjectList().add(project);
storageManager.storeAll(dataRoot.getProjectList());
}

public List<Project> getProjectList() {
return dataRoot.getProjectList();
}

@PreDestroy
public void onDestroy() throws Exception {
storageManager.shutdown();
log.info("Spring Container is destroyed!");
}
}

DataRoot 看起来像这样

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class DataRoot {
private List<Project> projectList;
}

我可以启动应用程序并根据需要添加和列出项目。据我所知,项目已保存,因为存储文件大小增加了。

所以到目前为止一切似乎都工作正常,直到我重新启动 Spring 应用程序

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [one.microstream.storage.types.EmbeddedStorageManager]: Factory method 'storageManager' threw exception; nested exception is one.microstream.exceptions.TypeCastException: Cannot cast ch.wesr.projectz.projapi.storage.DataRoot to ch.wesr.projectz.projapi.storage.DataRoot
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.7.jar:5.3.7]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.7.jar:5.3.7]
... 38 common frames omitted
Caused by: one.microstream.exceptions.TypeCastException: Cannot cast ch.wesr.projectz.projapi.storage.DataRoot to ch.wesr.projectz.projapi.storage.DataRoot
at one.microstream.persistence.binary.internal.AbstractBinaryHandlerReflective.updateState(AbstractBinaryHandlerReflective.java:521) ~[persistence.binary-04.01.00-MS-GA.jar:na]
at one.microstream.persistence.binary.internal.AbstractBinaryHandlerReflective.updateState(AbstractBinaryHandlerReflective.java:1) ~[persistence.binary-04.01.00-MS-GA.jar:na]
at one.microstream.persistence.binary.types.BinaryLoader$Default.buildInstances(BinaryLoader.java:447) ~[persistence.binary-04.01.00-MS-GA.jar:na]
at one.microstream.persistence.binary.types.BinaryLoader$Default.build(BinaryLoader.java:382) ~[persistence.binary-04.01.00-MS-GA.jar:na]
at one.microstream.persistence.binary.types.BinaryLoader$Default.get(BinaryLoader.java:825) ~[persistence.binary-04.01.00-MS-GA.jar:na]
at one.microstream.persistence.binary.types.BinaryLoader$Default.loadRoots(BinaryLoader.java:869) ~[persistence.binary-04.01.00-MS-GA.jar:na]
at one.microstream.storage.types.EmbeddedStorageManager$Default.loadExistingRoots(EmbeddedStorageManager.java:306) ~[storage.embedded-04.01.00-MS-GA.jar:na]
at one.microstream.storage.types.EmbeddedStorageManager$Default.initialize(EmbeddedStorageManager.java:326) ~[storage.embedded-04.01.00-MS-GA.jar:na]
at one.microstream.storage.types.EmbeddedStorageManager$Default.start(EmbeddedStorageManager.java:214) ~[storage.embedded-04.01.00-MS-GA.jar:na]
at one.microstream.storage.types.EmbeddedStorageManager$Default.start(EmbeddedStorageManager.java:1) ~[storage.embedded-04.01.00-MS-GA.jar:na]
at one.microstream.storage.types.EmbeddedStorage.createAndStartStorageManager(EmbeddedStorage.java:626) ~[storage.embedded-04.01.00-MS-GA.jar:na]
at one.microstream.storage.types.EmbeddedStorage.start(EmbeddedStorage.java:484) ~[storage.embedded-04.01.00-MS-GA.jar:na]
at ch.wesr.projectz.projapi.config.MicrostreamConfig.storageManager(MicrostreamConfig.java:33) ~[classes/:na]
at ch.wesr.projectz.projapi.config.MicrostreamConfig$$EnhancerBySpringCGLIB$$d68ff25d.CGLIB$storageManager$1(<generated>) ~[classes/:na]
at ch.wesr.projectz.projapi.config.MicrostreamConfig$$EnhancerBySpringCGLIB$$d68ff25d$$FastClassBySpringCGLIB$$586f2b14.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.3.7.jar:5.3.7]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.3.7.jar:5.3.7]
at ch.wesr.projectz.projapi.config.MicrostreamConfig$$EnhancerBySpringCGLIB$$d68ff25d.storageManager(<generated>) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.7.jar:5.3.7]
... 39 common frames omitted

最佳答案

转换可能会失败,因为 Microstream 可能使用与您的 spring boot 应用程序不同的类加载器。您可以将 Microstream 配置为使用不同的,例如,当前线程的类加载器:

EmbeddedStorageManager storageManager = EmbeddedStorage.Foundation(Paths.get(location))
.onConnectionFoundation(cf -> cf.setClassLoaderProvider(ClassLoaderProvider.New(
Thread.currentThread().getContextClassLoader())))
.start(dataRoot());

参见 Class Loader in Application Server

关于java - spring bean 配置中的微流 EmbeddedStorageManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67860686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com