Java enum as spring组件—spring允许这样做,但不注入在enum中声明的成员
我对将java枚举作为带有Java enum as spring组件—spring允许这样做,但不注入在enum中声明的成员,java,spring,spring-boot,dependency-injection,enums,Java,Spring,Spring Boot,Dependency Injection,Enums,我对将java枚举作为带有@component注释的spring托管组件时的行为有一个疑问。我看到spring允许这样做(@组件毕竟有@Target(value=TYPE)),但当我尝试用@Autowired注入枚举成员时,它不会被注入。下面是我们的示例代码片段(ObjectMapper未在下面注入) 同样在上面,如果我取消注释上面的注释行,并将方法从抽象方法更改为抽象方法,我会在应用程序启动时出现奇怪的错误 rest-sample-service - [WARN ] 03:57:49.385
@component
注释的spring托管组件时的行为有一个疑问。我看到spring允许这样做(@组件
毕竟有@Target(value=TYPE)
),但当我尝试用@Autowired
注入枚举成员时,它不会被注入。下面是我们的示例代码片段(ObjectMapper
未在下面注入)
同样在上面,如果我取消注释上面的注释行,并将方法从抽象方法更改为抽象方法,我会在应用程序启动时出现奇怪的错误
rest-sample-service - [WARN ] 03:57:49.385 [main] org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'assetTypeEnum' defined in file [/BOOT-INF/classes/com/**/**/sampleservice/common/AssetTypeEnum.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
rest-sample-service - [INFO ] 03:57:49.574 [main] org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener -
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
rest-sample-service - [ERROR] 03:57:50.776 [main] org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter -
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.**.**.sampleservice.common.AssetTypeEnum required a bean of type 'java.lang.String' that could not be found.
Action:
Consider defining a bean of type 'java.lang.String' in your configuration.
有人知道这是怎么回事吗
谢谢这是一个有趣的难题,我希望您能从中学习有用的信息,但不要在应用程序中这样做。除此之外,枚举是隐式静态的,这带来了DI想要解决的所有共享状态问题,当我重构代码时,这是偶然发生的,然后我意识到spring没有抱怨…然后我尝试添加一个成员进行注入,这是在注入失败时…所以我想我可以请求…将更新我学到的这个线程。。。
rest-sample-service - [WARN ] 03:57:49.385 [main] org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'assetTypeEnum' defined in file [/BOOT-INF/classes/com/**/**/sampleservice/common/AssetTypeEnum.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
rest-sample-service - [INFO ] 03:57:49.574 [main] org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener -
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
rest-sample-service - [ERROR] 03:57:50.776 [main] org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter -
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.**.**.sampleservice.common.AssetTypeEnum required a bean of type 'java.lang.String' that could not be found.
Action:
Consider defining a bean of type 'java.lang.String' in your configuration.