Using the @ConditionalOnClass annotation in the method results in "java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy",
The main reason is to traverse all methods of the class in org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor, call the method object to get all annotations on the method, but throw an exception when the conditional class does not exist
See my code examples and exception information
Comment From: wilkinsona
Unfortunately, this is how the JVM works and there’s nothing Spring Boot or Framework can do about it. That is why the javadoc for @ConditionalOnClass urges you to be careful about using it on @Bean methods and recommends isolating the condition on a separate @Configuration class instead. Alternatively you could use the annotation’s name attribute at the cost of it then being stringly typed.
Comment From: ccoderJava
@Bean
@ConditionalOnClass(MongoTemplate.class)
public MongoDataLayerAdapter mongoDataLayerAdapter() {
return new MongoDataLayerAdapter();
}
When I use this @Bean and @ConditionalOnClass to inject SpringBean. The injection needs to be done when the MongoTemplate exists.
But in fact, when the class MongoTemplate.class does not exist, the following exception stack information will appear:
Caused by: java.lang.IllegalStateException: Failed to introspect annotated methods on class com.xxx.xxx.xxx.DataComplianceAutoConfiguration
at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:162) ~[spring-core-5.3.21.jar:5.3.21]
at org.springframework.context.annotation.ConfigurationClassParser.retrieveBeanMethodMetadata(ConfigurationClassParser.java:403) ~[spring-context-5.3.21.jar:5.3.21]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:326) ~[spring-context-5.3.21.jar:5.3.21]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250) ~[spring-context-5.3.21.jar:5.3.21]
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:600) ~[spring-context-5.3.21.jar:5.3.21]
... 17 common frames omitted
Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:724) ~[na:1.8.0_201]
at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:531) ~[na:1.8.0_201]
at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:355) ~[na:1.8.0_201]
at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:286) ~[na:1.8.0_201]
at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120) ~[na:1.8.0_201]
at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72) ~[na:1.8.0_201]
at java.lang.reflect.Executable.declaredAnnotations(Executable.java:599) ~[na:1.8.0_201]
at java.lang.reflect.Executable.declaredAnnotations(Executable.java:597) ~[na:1.8.0_201]
at java.lang.reflect.Executable.getDeclaredAnnotations(Executable.java:588) ~[na:1.8.0_201]
at java.lang.reflect.Method.getDeclaredAnnotations(Method.java:630) ~[na:1.8.0_201]
at java.lang.reflect.AccessibleObject.getAnnotations(AccessibleObject.java:207) ~[na:1.8.0_201]
at org.springframework.core.type.StandardAnnotationMetadata.isAnnotatedMethod(StandardAnnotationMetadata.java:170) ~[spring-core-5.3.21.jar:5.3.21]
at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:153) ~[spring-core-5.3.21.jar:5.3.21]
... 21 common frames omitted
Now I modified it to use name injection. And using the fully qualified package name of the class can bypass Java's reflect problem.
@Bean
@ConditionalOnClass(name = "org.springframework.data.mongodb.core.MongoTemplate")
public MongoDataLayerAdapter mongoDataLayerAdapter() {
return new MongoDataLayerAdapter();
}
now. it works. 😃
Comment From: oneice2020