Forráskód Böngészése

ConditionalOnProperty 注解测试

xiaochan 4 hónapja
szülő
commit
d738481a4e

+ 25 - 22
thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/tts/TtsController.java

@@ -3,6 +3,7 @@ package org.thyy.thirdpartapi.tts;
 import jakarta.annotation.PostConstruct;
 import jakarta.servlet.http.HttpServletResponse;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -18,33 +19,35 @@ public class TtsController {
 
     private final Map<String, Tts> ttsServices;
     private final TtsConfig ttsConfig;
-    private Tts ttsService;
+    private final Tts ttsService;
 
-    public TtsController(Map<String, Tts> ttsServices, TtsConfig ttsConfig) {
+
+    public TtsController(Map<String, Tts> ttsServices, TtsConfig ttsConfig, Tts ttsService) {
         this.ttsServices = ttsServices;
         this.ttsConfig = ttsConfig;
+        this.ttsService = ttsService;
     }
 
-    @PostConstruct
-    public void init() {
-        for (Map.Entry<String, Tts> map : ttsServices.entrySet()) {
-            String key = map.getKey();
-            if (ttsConfig.getConfig() == null) {
-                return;
-            }
-            TtsConfig.Config config = ttsConfig.getConfig().get(key);
-            if (config != null) {
-                try {
-                    map.getValue().init(ttsConfig, config);
-                    ttsService = map.getValue();
-
-                } catch (Exception e) {
-                    log.error("语言合成错误", e);
-                }
-                return;
-            }
-        }
-    }
+//    @PostConstruct
+//    public void init() {
+//        for (Map.Entry<String, Tts> map : ttsServices.entrySet()) {
+//            String key = map.getKey();
+//            if (ttsConfig.getConfig() == null) {
+//                return;
+//            }
+//            TtsConfig.Config config = ttsConfig.getConfig().get(key);
+//            if (config != null) {
+//                try {
+//                    map.getValue().init(ttsConfig, config);
+//                    ttsService = map.getValue();
+//
+//                } catch (Exception e) {
+//                    log.error("语言合成错误", e);
+//                }
+//                return;
+//            }
+//        }
+//    }
 
 
     @PostMapping("/textToSpeech")

+ 22 - 0
thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/tts/ttsService/TtsTest.java

@@ -0,0 +1,22 @@
+package org.thyy.thirdpartapi.tts.ttsService;
+
+import jakarta.servlet.http.HttpServletResponse;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.stereotype.Service;
+import org.thyy.thirdpartapi.tts.Tts;
+import org.thyy.thirdpartapi.tts.TtsRequest;
+import org.thyy.utils.result.R;
+import org.thyy.utils.result.ResultVo;
+
+@Slf4j
+@Service(value = "ttstext")
+@Configuration
+@ConditionalOnProperty(prefix = "tts", name = "service", havingValue = "ttstext")
+public class TtsTest implements Tts {
+    @Override
+    public ResultVo<String> textToSpeech(TtsRequest request, HttpServletResponse response) {
+        return R.ok("test");
+    }
+}

+ 39 - 33
thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/tts/ttsService/XfTtsApi.java

@@ -9,6 +9,8 @@ import jakarta.servlet.http.HttpServletResponse;
 import lombok.extern.slf4j.Slf4j;
 import okhttp3.Response;
 import okhttp3.WebSocket;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Configuration;
 import org.springframework.stereotype.Service;
 import org.thyy.thirdpartapi.tts.Tts;
 import org.thyy.thirdpartapi.tts.TtsConfig;
@@ -24,6 +26,8 @@ import java.util.concurrent.atomic.AtomicReference;
 
 @Slf4j
 @Service(value = "xf")
+@Configuration
+@ConditionalOnProperty(prefix = "tts", name = "service", matchIfMissing = true, havingValue = "xf")
 public class XfTtsApi implements Tts {
     private TtsClient ttsClient;
     private TtsConfig cfg;
@@ -50,39 +54,41 @@ public class XfTtsApi implements Tts {
 
         CompletableFuture<Void> future = new CompletableFuture<>();
 
-        try {
-            File file = new File(filePath);
-            ttsClient.send(request.getText(), new AbstractTtsWebSocketListener(file) {
-                @Override
-                public void onSuccess(byte[] bytes) {
-                }
-
-                @Override
-                public void onFail(WebSocket webSocket, Throwable throwable, Response response) {
-                    log.error("[onFail]生成语音文件失败:{}", throwable.getMessage());
-                }
-
-                @Override
-                public void onBusinessFail(WebSocket webSocket, TtsResponse ttsResponse) {
-                    log.error("[onBusinessFail]生成语音文件失败:{}", ttsResponse.toString());
-                }
-
-                @Override
-                public void onClosing(WebSocket webSocket, int code, String reason) {
-                    super.onClosing(webSocket, code, reason);
-                }
-
-                @Override
-                public void onClosed(WebSocket webSocket, int code, String reason) {
-                    super.onClosed(webSocket, code, reason);
-                    future.complete(null);
-                }
-            });
-        } catch (Exception e) {
-            log.error("生成语音文件失败", e);
-            log.error("错误码查询链接:https://www.xfyun.cn/document/error-code");
-            future.completeExceptionally(e);
-        }
+//        try {
+//            File file = new File(filePath);
+//            ttsClient.send(request.getText(), new AbstractTtsWebSocketListener(file) {
+//                @Override
+//                public void onSuccess(byte[] bytes) {
+//                }
+//
+//                @Override
+//                public void onFail(WebSocket webSocket, Throwable throwable, Response response) {
+//                    log.error("[onFail]生成语音文件失败:{}", throwable.getMessage());
+//                }
+//
+//                @Override
+//                public void onBusinessFail(WebSocket webSocket, TtsResponse ttsResponse) {
+//                    log.error("[onBusinessFail]生成语音文件失败:{}", ttsResponse.toString());
+//                }
+//
+//                @Override
+//                public void onClosing(WebSocket webSocket, int code, String reason) {
+//                    super.onClosing(webSocket, code, reason);
+//                }
+//
+//                @Override
+//                public void onClosed(WebSocket webSocket, int code, String reason) {
+//                    super.onClosed(webSocket, code, reason);
+//                    future.complete(null);
+//                }
+//            });
+//        } catch (Exception e) {
+//            log.error("生成语音文件失败", e);
+//            log.error("错误码查询链接:https://www.xfyun.cn/document/error-code");
+//            future.completeExceptionally(e);
+//        }
+
+        future.complete(null);
 
         String voiceUrl = cfg.getSpeechUrl() + "/" + request.getId() + ".mp3";
         AtomicReference<ResultVo<String>> resultVo = new AtomicReference<>(R.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, ""));

+ 3 - 0
thyy-thirdpart-api/src/main/resources/application.yml

@@ -6,6 +6,9 @@ server:
   servlet:
     context-path: /thyy/thirdpart/api
 
+tts:
+  service: ttstext
+
 thyy:
   tts:
     directory: D:\a.snapshot\resource\voice