我从https://www.baeldung.com/spring-shell-cli开始了一个新项目
我试图在我的Autowired类上使用依赖注入或BannerProvider注释。但是春天带来了一个例外。
经过多次定制后的我的应用程序类
@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.谢谢你的支持。
发布于 2021-06-23 14:26:48
我自己解开了这个谜团。
关键是在线教程。它建议编写一个安装文件META-INF/spring/spring-shell-plugin.xml。
该文件类似于:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="my.namespace.too.specific.path"/>
</beans>但是它对所有的 Spring行为都有影响,所以您需要将所有bean配置为my.namespace.too.specific.path。否则什么都不管用。
也许是新手的错误。但是我讨厌META-INF安装文件,并且在我的所有项目中都尽量避免它们。
发布于 2021-06-23 22:37:57
在创建bean时有两个问题:@Bean @Qualifier("ScreenSize") public Point screenSize() {...}
以下是您问题的解决方案:
声明Point:@Bean(name="ScreenSize") public Point screenSize() {...}的
注入bean的@Autowired public MyBannerProvider(@Qualifier("ScreenSize") Point screenSize) { this.screenSize = screenSize; }:
https://stackoverflow.com/questions/68095600
复制相似问题