繁体   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