简体   繁体   English

在 Spring Boot 中为接口使用 @Autowired 注释

[英]Using @Autowired anotation for Interface in Spring boot

I have an interface(QBuilder) and there are two classes(MBuilder, TBuilder) implementing this interface.我有一个接口(QBuilder),并且有两个类(MBuilder、TBuilder)实现了这个接口。 The interface contains a test method.该接口包含一个测试方法。 This method receives parameter type of MCubeInfo in MBuilder and TCubeInfo in TBuilder.该方法接收 MBuilder 中的 MCubeInfo 和 TBuilder 中的 TCubeInfo 的参数类型。

public interface QBuilder<T> {

    public String test(T cubeInfo);
}

public class MBuilder implements QBuilder<MCubeInfo> {

    @Override
    public String test(MCubeInfo cubeInfo) {
        System.out.println("MCube Info");
        return "MCube";
    }
}


public class TBuilder implements QBuilder<TCubeInfo> {
        @Override
        public String test(TCubeInfo cubeInfo) {
            System.out.println("TCube Info");
            return "TCube";
        }
}

I am expecting that when I call test method in QuerySvc, qBuilder redirect to me according to the parameter type.我期望当我在 QuerySvc 中调用测试方法时,qBuilder 根据参数类型重定向到我。 However in autowired QBuilder set automatically with MBuilder.但是在自动装配的 QBuilder 中使用 MBuilder 自动设置。 Therefore when I sent TCubeInfo object to the test function, occurs an error that it can not be convert MCubeInfo.因此,当我将 TCubeInfo 对象发送到测试函数时,会出现无法转换 MCubeInfo 的错误。

@RestController
public class QuerySvc {

     private QBuilder qBuilder;

     @Autowired
     public void setQBuilder(QBuilder q){
          qBuilder = q)
     }

     @RequestMapping(value = "/boot", method = RequestMethod.GET)
     public ResponseEntity<String> getTest(){

           .
           .
           .
           TCubeInfo cube = .....
           qBuilder.test(cube);

     }


}

When I search the problem, I encountered with @Qualifier annotation but I cannot adapt it to my problem.当我搜索问题时,我遇到了@Qualifier 注解,但无法适应我的问题。

I think you should make two different beans of these two Service/Component Class that you defined.我认为您应该为您定义的这两个服务/组件类制作两个不同的 bean。

public class MBuilder   //two different beans in configuration Class.
public class Tuilder 

Spring-boot Configuration Class. Spring-boot 配置类。

@Bean(name="mBuilder") //define bean name
public MBuilder mBuilder(){    //mBuilder bean for MBuilder Class.
    return new MBuilder();
}

@Bean(name="tBuilder")   //Define bean name
public TBuilder tBuilder(){   //tBuilder bean for TBuilder Class.
    return new TBuilder();
}

Now, In Your RestController try to inject two beans with different @Qualifier statement.现在,在您的RestController尝试使用不同的@Qualifier语句注入两个 bean。 As shown below.如下所示。

RestController Class. RestController 类。

@RestController
public class QuerySvc {

     @Qualifier("tBuilder")  //Now use tBuilder Object as per Your Need.
     @Autowired
     private QBuilder tBuilder;
     @Qualifier("mBuilder") // You can use mBuilder Object as per need.
     @Autowired 
     private QBuilder mBuilder;

     @Autowired
     public void setQBuilder(QBuilder q){
          qBuilder = q)
     }

     @RequestMapping(value = "/boot", method = RequestMethod.GET)
     public ResponseEntity<String> getTest(){

           .
           .
           .
           TCubeInfo cube = .....
           qBuilder.test(cube);
     }
}

Note :- Here You Used generics Typed parameters which resolve at Compile Time Only.注意:- 这里您使用了泛型类型的参数,这些参数仅在编译时解析。 Here TCubeInfo and MCubeInfo both are different classes (they are not in relationship heirarchy).这里TCubeInfoMCubeInfo都是不同的类(它们不在关系层次结构中)。 So, It is impossible to cast the object which not comes under heirarchy.因此,不可能投射不属于层次结构的对象。 It will raise ClassCastException .它将引发ClassCastException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM