我正在尝试将微服务从 spring 'default' 迁移到 spring native,但有些事情出了问题。当我的应用程序启动时,我收到此错误:
2021-08-19 20:19:59.513 ERROR [CUSTOMER-APP,,] 1 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'customerService' defined in class path resource [br/com/project/customer/service/v1/CustomerService.class]:
Unsatisfied dependency expressed through constructor customer 0; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'customerRepository' defined in br.com.project.customer.repository.CustomerRepository
defined in @EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration:
Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException:
Could not create query for public abstract
java.util.Optional br.com.project.customer.repository.CustomerRepository.findByKey(java.lang.String)!
Reason: Class org.springframework.data.domain.Example[] is instantiated reflectively but was never registered.
Register the class by using org.graalvm.nativeimage.hosted.RuntimeReflection;
nested exception is java.lang.IllegalArgumentException: Class org.springframework.data.domain.Example[]
is instantiated reflectively but was never registered.
Register the class by using org.graalvm.nativeimage.hosted.RuntimeReflection
看了异常,明白需要注册Example类,所以我把这段代码放到我的Application上,但是没有效果
@AotProxyHints(
value = [
AotProxyHint(
targetClass = ConfigServicePropertySourceLocator::class,
interfaces = [Retryable::class],
proxyFeatures = IS_STATIC
),
AotProxyHint(
targetClass = Application::class,
interfaces = [ScopedObject::class, Serializable::class, AopInfrastructureBean::class]
)
]
)
@NativeHints(
value = [
NativeHint(
types = [
TypeHint(
types = [Example::class],
access = ALL
)],
)
]
)
@InitializationHint(types = [Example::class], initTime = BUILD)
@SpringBootApplication
class Application {
companion object {
@JvmStatic
fun main(args: Array<String>) {
System.setProperty("spring.cloud.bootstrap.enabled", "true")
SpringApplication.run(Application::class.java, *args)
}
}
}
构建后,生成的 reflect-config.json 具有预期的配置
...
{
"name": "org.springframework.data.domain.Example",
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allDeclaredMethods": true
}
...
这里可以看到Example类的定义:https ://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/Example.html