首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动/依赖注入在Spring项目中不起作用

自动/依赖注入在Spring项目中不起作用
EN

Stack Overflow用户
提问于 2021-06-23 07:42:43
回答 2查看 1.9K关注 0票数 1

我从https://www.baeldung.com/spring-shell-cli开始了一个新项目

我试图在我的Autowired类上使用依赖注入或BannerProvider注释。但是春天带来了一个例外。

经过多次定制后的我的应用程序类

代码语言:javascript
复制
@SpringBootApplication
public class MyCLIApplication extends Bootstrap {
    
    public static void main(String[] args) {
        SpringApplication.exit(SpringApplication.run(MyCLIApplication.class, args));
    }
}

我的BannerProvider组件

代码语言:javascript
复制
@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依赖项

代码语言:javascript
复制
    <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>

运行错误

代码语言:javascript
复制
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.

谢谢你的支持。

EN

回答 2

Stack Overflow用户

发布于 2021-06-23 14:26:48

我自己解开了这个谜团。

关键是在线教程。它建议编写一个安装文件META-INF/spring/spring-shell-plugin.xml

该文件类似于:

代码语言:javascript
复制
<?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安装文件,并且在我的所有项目中都尽量避免它们。

票数 0
EN

Stack Overflow用户

发布于 2021-06-23 22:37:57

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

  1. @限定符注释不是用来创建bean的,而是与@autowired一起使用的,用于消除在具有相同类型的几个bean时需要注入哪一个bean的问题。如果需要指定在本例中应该使用的bean的名称(Id),请注意:如果您没有在@Bean注释中指定Bean的名称,默认情况下容器所提示的名称与方法的名称相同,在您的情况下,它是“screenSize”。

以下是您问题的解决方案:

声明Point:@Bean(name="ScreenSize") public Point screenSize() {...}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68095600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档