簡體   English   中英

春季啟動:考慮在配置中定義類型為“ com.repository.services.interfacename”的bean

[英]Spring boot: Consider defining a bean of type 'com.repository.services.interfacename' in your configuration

我已經部署了一個Spring Boot應用程序,在其中創建了一個接口並用兩個類實現它。

public interface interfacename {
    LinkedHashSet<String> names(String path);
}

和實現的類是

@Component
public class class1 implements interfacename {
......
}

@Component
public class class2 implements interfacename {
......
}

現在,我嘗試使用接口名稱為兩個類創建一個實例,

@Autowired
@Qualifier("class1")
interfacename imp1;

@Autowired
@Qualifier("class2")
interfacename imp2;

這是配置類,

@Configuration
public class interfacenameConfig {

    @Bean
    @ConditionalOnProperty(name = "class1", matchIfMissing = true)
    public interfacename class1Service() {
        return new class1();
    }

    @Bean
    @ConditionalOnProperty(name = "stanfordname")
    public interfacename class2Service() {
        return new class2();
    }
}

我的項目結構是

com.repository
    application.java(@SpringApplcation)
com.repository.controller
    applicationcontroller.java(@RestController)
com.repository.services
    interfacename.java
    interfacenameconfig.java(@configuration)
    class1.java(@component)
    class2.java(@component)

它將引發以下錯誤操作:

Consider defining a bean of type 'com.repository.services.interfacename' in your configuration.

請有人指導我解決這個問題。

提前致謝

在您使用時,您要說的是class2 ID /名稱分別為class1class2 bean:

@Autowired
@Qualifier("class1")
interfacename imp1;

@Autowired
@Qualifier("class2")
interfacename imp2;

但是在配置中,您給了他們不同的名稱:

@Configuration
public class interfacenameConfig {

    @Bean
    @ConditionalOnProperty(name = "class1", matchIfMissing = true)
    public interfacename class1Service() {
        return new class1();
    }

    @Bean
    @ConditionalOnProperty(name = "stanfordname")
    public interfacename class2Service() {
        return new class2();
    }
}

即: class1Serviceclass2Service 這些Id源自實例化bean的函數的名稱

兩種可能的修復:

  1. 使用@Bean("class1")@Bean("class2")他們提供所需的名稱。

要么

  1. 使用其在限定符中的名稱,即: @Qualifier("class1Service")@Qualifier("class2Service")

在您的配置類中,您應該有一個注釋,以提示您將組件掃描到您的接口interfacename所屬的軟件包。

例如:

@ComponentScan({"com.repository.services"})

在Spring-boot中,通常在Spring Boot應用程序類中具有此批注

例如

@SpringBootApplication
@ComponentScan({"com.repository.services"})
public class MyApplication {

}

更新

如果您有多個實現接口的類,則可以在將它們注釋為@Component時使用value屬性。

@Component(value="class1")
public class class1 implements interfacename 

@Component(value="class2")
public class class2 implements interfacename

然后@Autowire@Qualifier一起使用,就像您已經做的那樣。

根據您的最新更新,由於@SpringBootApplication位於spring-managed bean的父目錄中,我認為您可以省略@ComponentScan批注。 Spring默認會掃描com.repository下的所有子包。

但是我仍然相信interfacenameconfig類是多余的。 為什么要聲明與注釋為@Component bean相同的bean? 就我所知, @Component @Bean@Bean都沒有理由讓相同的bean都使用,這可能是問題的根源。

您需要在接口實現上方添加@Service批注。

例如@Component public interface interfacename { LinkedHashSet<String> names(String path); } @Component public interface interfacename { LinkedHashSet<String> names(String path); }

  @Service
  public class interfaceDefinition implements interfacename{
  LinkedHashSet<String> names(String path){
   // write code here
  }
  }

我已經將限定符注釋和@Component注釋一起添加了。 然后,我確保應用程序運行正常。

@Component
@Qualifier("class1")
public class class1 implements interfacename {
......
}

謝謝回復

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM