【问题标题】:Autowired/Dependencies injection doesn't work in a Spring Shell project自动装配/依赖注入在 Spring Shell 项目中不起作用
【发布时间】: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


【解决方案1】:

我自己解开了这个谜。

关键在于在线教程。建议写一个安装文件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 设置文件,我在所有项目中都尽量避免使用它们。

【讨论】:

    【解决方案2】:

    创建 bean 有两个问题: @Bean @Qualifier("ScreenSize") public Point screenSize() {...}

    1. @Qualifier 注解不用于创建 bean,它与 @autowired 一起使用以消除当您有多个相同类型的 bean 时需要注入哪个 bean 的问题。
    2. 如果您需要指定在这种情况下应使用的 bean 的名称(id):@Bean("ScreenSize") *请注意,如果您没有在@Bean注解中指定bean的名称,默认情况下容器考虑的名称与方法名称相同,在您的情况下为“screenSize”。

    在您的问题的解决方案下方:

    1. 要声明您的 Point bean: @Bean(name="ScreenSize") public Point screenSize() {...}

    2. 注入 bean: @Autowired public MyBannerProvider(@Qualifier("ScreenSize") Point screenSize) { this.screenSize = screenSize; }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      相关资源
      最近更新 更多