diff --git a/boot/src/main/java/com/zfoo/boot/BootAutoConfiguration.java b/boot/src/main/java/com/zfoo/boot/BootAutoConfiguration.java index 1dbf3833..a8040f63 100644 --- a/boot/src/main/java/com/zfoo/boot/BootAutoConfiguration.java +++ b/boot/src/main/java/com/zfoo/boot/BootAutoConfiguration.java @@ -14,15 +14,12 @@ package com.zfoo.boot; import com.fasterxml.jackson.databind.ObjectMapper; import com.zfoo.protocol.util.JsonUtils; -import com.zfoo.protocol.util.ReflectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import java.lang.reflect.Field; - /** * @author godotg * @version 3.0 @@ -34,12 +31,9 @@ public class BootAutoConfiguration { @Bean @ConditionalOnMissingBean - public ObjectMapper getObjectMapper() throws NoSuchFieldException { - // 拿到JsonUtils中的ObjectMapper - Field field = JsonUtils.class.getDeclaredField("MAPPER"); - ReflectionUtils.makeAccessible(field); + public ObjectMapper getObjectMapper() { logger.info("Jackson auto config successfully!"); - return (ObjectMapper) ReflectionUtils.getField(field, null); + return JsonUtils.MAPPER; } } diff --git a/boot/src/test/java/com/zfoo/boot/StorageTest.java b/boot/src/test/java/com/zfoo/boot/StorageTest.java new file mode 100644 index 00000000..eb348354 --- /dev/null +++ b/boot/src/test/java/com/zfoo/boot/StorageTest.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.boot; + +import com.zfoo.boot.resource.StudentResource; +import com.zfoo.protocol.util.JsonUtils; +import com.zfoo.storage.StorageContext; +import com.zfoo.storage.model.config.StorageConfig; +import com.zfoo.storage.model.vo.Storage; +import com.zfoo.util.ThreadUtils; +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.core.env.Environment; + +/** + * @author godotg + * @version 3.0 + */ +@Ignore +@SpringBootApplication(exclude = { + // 排除MongoDB自动配置 + MongoAutoConfiguration.class +}) +public class StorageTest { + + private static final Logger logger = LoggerFactory.getLogger(StorageTest.class); + + @Bean + public StorageConfig storageConfig(Environment environment) { + var config = new StorageConfig(); + config.setScanPackage("com.zfoo.boot"); + config.setResourceLocation("classpath:/excel"); + config.setWriteable(true); + return config; + } + + @Test + public void startApplication() { + var springBoot = new SpringApplicationBuilder(); + springBoot.sources(StorageTest.class).web(WebApplicationType.NONE).run(); + + var storage = (Storage) StorageContext.getStorageManager().getStorage(StudentResource.class); + for (StudentResource resource : storage.getAll()) { + logger.info(JsonUtils.object2String(resource)); + } + + ThreadUtils.sleep(Long.MAX_VALUE); + } + +} diff --git a/boot/src/test/java/com/zfoo/boot/resource/StudentResource.java b/boot/src/test/java/com/zfoo/boot/resource/StudentResource.java new file mode 100644 index 00000000..e372b9f4 --- /dev/null +++ b/boot/src/test/java/com/zfoo/boot/resource/StudentResource.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.boot.resource; + +import com.zfoo.protocol.IPacket; +import com.zfoo.storage.model.anno.Id; +import com.zfoo.storage.model.anno.Resource; + +/** + * @author godotg + * @version 4.0 + */ +@Resource +public class StudentResource implements IPacket { + + @Id + public int id; + + public String name; + public int age; + public float score; + public String[] courses; + public User[] users; + public User user; + +} + +class User implements IPacket { + + public String id; + public String name; + public String sex; + public int age; + +} diff --git a/boot/src/test/resources/excel/StudentResource.xlsx b/boot/src/test/resources/excel/StudentResource.xlsx new file mode 100644 index 00000000..d7378b7a Binary files /dev/null and b/boot/src/test/resources/excel/StudentResource.xlsx differ diff --git a/boot/src/test/resources/logback-test.xml b/boot/src/test/resources/logback-test.xml new file mode 100644 index 00000000..fadb70c5 --- /dev/null +++ b/boot/src/test/resources/logback-test.xml @@ -0,0 +1,44 @@ + + + + + com.zfoo.boot + + + + + + + + ${PATTERN_CONSOLE} + UTF-8 + + + + + + + + + + + + + + + + + + + + + +