2

我想将微流的 EmbeddedStorageManager 配置为 Spring Boot 应用程序(2.5.0)中的 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;
    }
}

并将其注入存储库类

@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
4

1 回答 1

6

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

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

请参阅应用程序服务器中的类加载器

于 2021-06-15T05:30:55.347 回答