有以下的条件
public class MinioCondition extends AllNestedConditions {
public MinioCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnClass(MinioClient.class)
static class Classes {
}
@ConditionalOnMissingBean(StorageService.class)
static class Beans {
}
@ConditionalOnProperty(prefix = "application.storage", name = "type", havingValue = "minio")
static class Properties {
}
}
有以下的配置
@Configuration
public class MinioConfiguration {
// 希望满足条件时候才创建StorageService实例Bean,同时,如果有MinioClient类型的Bean则会使用该Bean
@Conditional(MinioCondition.class)
@Bean
public StorageService storageService(@Autowired(required = false) MinioClient minioClient) {
return new MinioServiceImpl(minioClient);
}
}
该代码以jar包的形式被别的工程引入,该工程中没有添加minio的依赖包, 在启动工程的时候出现了以下错误
java.lang.IllegalStateException: Unable to load cache item
at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:75) ~[spring-core-6.1.10.jar:6.1.10]
at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34) ~[spring-core-6.1.10.jar:6.1.10]
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:130) ~[spring-core-6.1.10.jar:6.1.10]
at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:317) ~[spring-core-6.1.10.jar:6.1.10]
at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:562) ~[spring-core-6.1.10.jar:6.1.10]
at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:407) ~[spring-core-6.1.10.jar:6.1.10]
at org.springframework.context.annotation.ConfigurationClassEnhancer.createClass(ConfigurationClassEnhancer.java:156) ~[spring-context-6.1.10.jar:6.1.10]
at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:113) ~[spring-context-6.1.10.jar:6.1.10]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:534) ~[spring-context-6.1.10.jar:6.1.10]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:311) ~[spring-context-6.1.10.jar:6.1.10]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:363) ~[spring-context-6.1.10.jar:6.1.10]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:153) ~[spring-context-6.1.10.jar:6.1.10]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:788) ~[spring-context-6.1.10.jar:6.1.10]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:606) ~[spring-context-6.1.10.jar:6.1.10]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.3.1.jar:3.3.1]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-3.3.1.jar:3.3.1]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) ~[spring-boot-3.3.1.jar:3.3.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) ~[spring-boot-3.3.1.jar:3.3.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363) ~[spring-boot-3.3.1.jar:3.3.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352) ~[spring-boot-3.3.1.jar:3.3.1]
at com.example.retry.Application.main(Application.java:18) ~[classes/:na]
Caused by: java.lang.NoClassDefFoundError: io/minio/MinioClient
at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na]
at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3502) ~[na:na]
at java.base/java.lang.Class.getDeclaredMethods(Class.java:2601) ~[na:na]
at org.springframework.cglib.core.ReflectUtils.addAllMethods(ReflectUtils.java:396) ~[spring-core-6.1.10.jar:6.1.10]
at org.springframework.cglib.proxy.Enhancer.getMethods(Enhancer.java:627) ~[spring-core-6.1.10.jar:6.1.10]
at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:664) ~[spring-core-6.1.10.jar:6.1.10]
at org.springframework.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:35) ~[spring-core-6.1.10.jar:6.1.10]
at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:26) ~[spring-core-6.1.10.jar:6.1.10]
at org.springframework.cglib.core.ClassLoaderAwareGeneratorStrategy.generate(ClassLoaderAwareGeneratorStrategy.java:57) ~[spring-core-6.1.10.jar:6.1.10]
at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:366) ~[spring-core-6.1.10.jar:6.1.10]
at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:575) ~[spring-core-6.1.10.jar:6.1.10]
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.lambda$new$1(AbstractClassGenerator.java:107) ~[spring-core-6.1.10.jar:6.1.10]
at org.springframework.cglib.core.internal.LoadingCache.lambda$createEntry$1(LoadingCache.java:52) ~[spring-core-6.1.10.jar:6.1.10]
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317) ~[na:na]
at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:57) ~[spring-core-6.1.10.jar:6.1.10]
... 20 common frames omitted
Caused by: java.lang.ClassNotFoundException: io.minio.MinioClient
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na]
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
... 35 common frames omitted
即没有找到类io.minio.MinioClient
。从配置来看,我们是根据条件来决定是否创建StorageService实例Bean的,为什么还会报错呢?
原因是,参数中已经在进行MinioClient
类型Bean的依赖注入操作了。
如果要达到上面的效果,只需要将条件@Conditional(MinioCondition.class)
放到配置类上即可。
@Configuration
// 将条件放到配置类上
@Conditional(MinioCondition.class)
public class MinioConfiguration {
@Bean
public StorageService storageService(@Autowired(required = false) MinioClient minioClient) {
return new MinioServiceImpl(minioClient);
}
}