@Qualifier is not applicable for constructor

問題場景:

筆者在springboot項目中使用java_websocket做爲客戶端,調用第三方ws服務。java

最初只調用一個ws服務,如下代碼能夠正常工做:web

   @Bean
    public URI ttsUri() throws URISyntaxException {
        return new URI("ws://1.1.1.1:8888/xxx");
    }

@Slf4j
@Component("ttsOfflineClient")
public class OfflineTTSWsClient extends WebSocketClient {

  @Autowired
    public OfflineTTSWsClient(URI serverUri) throws IOException {
        super(serverUri);
    }
......
}

後來又加一個ws服務:ws://2.2.2.2:7777/aaaa 嘗試寫如下代碼報錯spring

 
 
@Bean(name = "ttsUri")
public URI ttsUri() throws URISyntaxException {
return new URI("ws://1.1.1.1:8888/xxx");
}

@Autowired @Qualifier(value
= "ttsUri") public OfflineTTSWsClient(URI serverUri) throws IOException { super(serverUri); } //報錯:@Qualifier is not applicable for constructor

正確方法

@Autowired
public OfflineTTSWsClient(@Qualifier(value = "ttsUri") URI serverUri) throws IOException {
   super(serverUri);
}

或者(注意兩段代碼差異)springboot

public OfflineTTSWsClient(@Qualifier(value = "ttsUri") URI serverUri) throws IOException {
  super(serverUri);
}

最終兩個ws正常工做代碼以下:websocket

@Bean(name = "ttsUri")
public URI ttsUri() throws URISyntaxException {
    return new URI(""ws://1.1.1.1:8888/xxx");
}

@Bean(name = "asrUri")
public URI asrUri() throws URISyntaxException {
    return new URI("ws://2.2.2.2:7777/aaaa");
}

@Slf4j
@Component("ttsOfflineClient")
public class OfflineTTSWsClient extends WebSocketClient {

@Autowired
    public OfflineTTSWsClient(@Qualifier(value = "ttsUri") URI serverUri) throws IOException {
        super(serverUri);
    }
}

@Slf4j
@Component("asrOfflineClient")
public class OfflineASRWsClient extends WebSocketClient {


    public OfflineASRWsClient(@Qualifier(value = "asrUri") URI serverUri) 
   {
        super(serverUri);
    }
}

 參考來源:https://stackoverflow.com/questions/42350828/why-qualifier-not-allowed-above-constructorapp

相關文章
相關標籤/搜索