【发布时间】:2021-06-23 07:42:43
【问题描述】:
我从https://www.baeldung.com/spring-shell-cli开始了一个新项目
我试图在我的BannerProvider 类上使用依赖注入或Autowired 注释。但是 Spring 引发了一个异常。
我的应用程序类 经过多次定制
@SpringBootApplication
public class MyCLIApplication extends Bootstrap {
public static void main(String[] args) {
SpringApplication.exit(SpringApplication.run(MyCLIApplication.class, args));
}
}
我的 BannerProvider 组件
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class MyBannerProvider extends DefaultBannerProvider {
private final Point screenSize;
public MyBannerProvider(@Qualifier("ScreenSize") Point screenSize) {
this.screenSize = screenSize;
}
[other stuff]
}
我的 POM 依赖项
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline</artifactId>
<version>${jline.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
</dependencies>
运行错误
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Qualifier("ScreenSize")
The following candidates were found but could not be injected:
- User-defined bean method 'screenSize' in 'TerminalConfiguration'
Action:
Consider revisiting the entries above or defining a bean of type 'my.namespace.Point' in your configuration.
感谢支持。
【问题讨论】:
-
Point bean 在 Configuration 类中有 ScreenSize 名称吗?像@Bean("ScreenSize") 之类的东西?
-
是的,它有``` @Bean @Qualifier("ScreenSize") public Point screenSize() {...} ``
-
所以当你有多个接口实现并且需要选择其中之一时使用限定符。限定符中的文本是确切的 bean 名称。我认为您需要从 @Bean 配置和 MyBannerProvider 中删除 Qualifier ,我认为它应该可以工作。这里也很好地解释了你的问题stackoverflow.com/a/49155590/1063509
-
不起作用:``` my.namespace.MyBannerProvider 中构造函数的参数 0 需要一个无法找到的“my.namespace.Point”类型的 bean。找到以下候选但无法注入: - 'TerminalConfiguration' 中的用户定义 bean 方法 'screenSize' 操作:考虑重新访问上面的条目或在配置中定义类型为 'my.namespace.Point' 的 bean。 ```
-
如果没有代码就很难理解出了什么问题,例如是否可以将项目上传到 github 上的某个地方?
标签: java spring spring-shell