From 7c0c83d39d770e03f54b04f9f2a39b7133b1b154 Mon Sep 17 00:00:00 2001 From: wangchunxiang <526328077@qq.com> Date: Wed, 15 Oct 2025 17:17:17 +0800 Subject: [PATCH] =?UTF-8?q?refactor(config):=20=E7=A7=BB=E9=99=A4BrowserCo?= =?UTF-8?q?nfig=E9=85=8D=E7=BD=AE=E7=B1=BB=E5=B9=B6=E5=BC=95=E5=85=A5Playw?= =?UTF-8?q?rightManager=E5=8D=95=E4=BE=8B=E7=AE=A1=E7=90=86=E5=99=A8=20?= =?UTF-8?q?=E5=B0=86=E5=8E=9F=E5=85=88=E9=80=9A=E8=BF=87Spring=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E7=9A=84Playwright=E5=AE=9E=E4=BE=8B=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E4=BD=BF=E7=94=A8=E8=87=AA=E5=AE=9A=E4=B9=89=E7=9A=84?= =?UTF-8?q?=E5=8D=95=E4=BE=8B=E6=A8=A1=E5=BC=8F=E8=BF=9B=E8=A1=8C=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E7=AE=A1=E7=90=86=EF=BC=8C=20=E6=8F=90=E5=8D=87?= =?UTF-8?q?=E8=B5=84=E6=BA=90=E6=8E=A7=E5=88=B6=E8=83=BD=E5=8A=9B=E4=B8=8E?= =?UTF-8?q?=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F=E7=AE=A1=E7=90=86=E7=9A=84?= =?UTF-8?q?=E7=81=B5=E6=B4=BB=E6=80=A7=E3=80=82=E5=90=8C=E6=97=B6=E5=9C=A8?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E5=85=B3=E9=97=AD=E6=97=B6=E4=B8=BB=E5=8A=A8?= =?UTF-8?q?=E9=87=8A=E6=94=BEPlaywright=E8=B5=84=E6=BA=90=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E5=86=85=E5=AD=98=E6=B3=84=E6=BC=8F=E3=80=82?= =?UTF-8?q?-=20=E5=88=A0=E9=99=A4`BrowserConfig`=E7=B1=BB=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E9=80=9A=E8=BF=87Spring=E5=AE=B9=E5=99=A8?= =?UTF-8?q?=E7=AE=A1=E7=90=86Playwright=E5=AE=9E=E4=BE=8B=20-=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E`PlaywrightManager`=E5=8D=95=E4=BE=8B=E7=B1=BB?= =?UTF-8?q?=EF=BC=8C=E7=BB=9F=E4=B8=80=E7=AE=A1=E7=90=86Playwright?= =?UTF-8?q?=E7=9A=84=E5=88=9B=E5=BB=BA=E3=80=81=E8=8E=B7=E5=8F=96=E5=92=8C?= =?UTF-8?q?=E9=94=80=E6=AF=81-=20=E5=9C=A8`DesktopApplication`=E4=B8=AD?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9Playwright=E5=AE=9E=E4=BE=8B?= =?UTF-8?q?=E7=9A=84=E5=85=B3=E9=97=AD=E8=B0=83=E7=94=A8=20-=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0`FtbCrawlNetDy`=E5=92=8C`FtbCrawlNetMt`=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E7=B1=BB=E4=B8=AD=E7=9A=84Playwright=E5=AE=9E?= =?UTF-8?q?=E4=BE=8B=E8=8E=B7=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fantaibao/DesktopApplication.java | 7 ++ .../com/fantaibao/config/BrowserConfig.java | 15 ---- .../fantaibao/config/PlaywrightManager.java | 70 +++++++++++++++++++ .../com/fantaibao/service/FtbCrawlNetDy.java | 7 +- .../com/fantaibao/service/FtbCrawlNetMt.java | 7 +- 5 files changed, 83 insertions(+), 23 deletions(-) delete mode 100644 src/main/java/com/fantaibao/config/BrowserConfig.java create mode 100644 src/main/java/com/fantaibao/config/PlaywrightManager.java diff --git a/src/main/java/com/fantaibao/DesktopApplication.java b/src/main/java/com/fantaibao/DesktopApplication.java index d4f222d..2d87eaa 100644 --- a/src/main/java/com/fantaibao/DesktopApplication.java +++ b/src/main/java/com/fantaibao/DesktopApplication.java @@ -1,6 +1,7 @@ package com.fantaibao; import com.fantaibao.config.GlobalConfig; +import com.fantaibao.config.PlaywrightManager; import com.fantaibao.page.InitialiNetWork; import com.fantaibao.page.LoginView; import javafx.application.Application; @@ -41,11 +42,15 @@ public class DesktopApplication extends Application { GlobalConfig.addIcon(primaryStage); // 直接启动登录界面 new LoginView(primaryStage); + // 异步加载浏览器加快启动时间 + } @Override public void stop() { closeApplicationContext(); + // 关闭Playwright实例 + PlaywrightManager.getInstance().close(); } /** @@ -60,6 +65,8 @@ public class DesktopApplication extends Application { private void handleCloseEvent(WindowEvent event) { // 关闭Spring Boot应用上下文 closeApplicationContext(); + // 关闭Playwright实例 + PlaywrightManager.getInstance().close(); // 退出JVM Platform.exit(); System.exit(0); diff --git a/src/main/java/com/fantaibao/config/BrowserConfig.java b/src/main/java/com/fantaibao/config/BrowserConfig.java deleted file mode 100644 index 1bc5ad7..0000000 --- a/src/main/java/com/fantaibao/config/BrowserConfig.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.fantaibao.config; - -import com.microsoft.playwright.Playwright; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class BrowserConfig { - - @Bean - public Playwright playwright() { - return Playwright.create(); - } - -} diff --git a/src/main/java/com/fantaibao/config/PlaywrightManager.java b/src/main/java/com/fantaibao/config/PlaywrightManager.java new file mode 100644 index 0000000..63830ff --- /dev/null +++ b/src/main/java/com/fantaibao/config/PlaywrightManager.java @@ -0,0 +1,70 @@ +package com.fantaibao.config; + +import com.microsoft.playwright.Playwright; +import lombok.extern.slf4j.Slf4j; + +/** + * Playwright单例管理类 + * 用于全局管理和提供Playwright实例 + */ +@Slf4j +public class PlaywrightManager { + + private static volatile PlaywrightManager instance; + private Playwright playwright; + + private PlaywrightManager() { + if (instance != null) { + throw new RuntimeException("请使用getInstance()方法获取实例"); + } + try { + this.playwright = Playwright.create(); + } catch (Exception e) { + log.error("创建Playwright实例失败", e); + throw new RuntimeException("创建Playwright实例失败", e); + } + } + + /** + * 获取PlaywrightManager单例实例 + * @return PlaywrightManager实例 + */ + public static PlaywrightManager getInstance() { + if (instance == null) { + synchronized (PlaywrightManager.class) { + if (instance == null) { + instance = new PlaywrightManager(); + } + } + } + return instance; + } + + /** + * 获取Playwright实例 + * @return Playwright实例 + */ + public Playwright getPlaywright() { + if (playwright == null) { + throw new RuntimeException("Playwright实例未初始化或已被销毁"); + } + return playwright; + } + + /** + * 关闭Playwright实例并释放资源 + */ + public void close() { + if (playwright != null) { + try { + playwright.close(); + } catch (Exception e) { + log.error("关闭Playwright实例时出错", e); + } finally { + playwright = null; + instance = null; + } + } + } + +} \ No newline at end of file diff --git a/src/main/java/com/fantaibao/service/FtbCrawlNetDy.java b/src/main/java/com/fantaibao/service/FtbCrawlNetDy.java index b2d7680..bf7b1c0 100644 --- a/src/main/java/com/fantaibao/service/FtbCrawlNetDy.java +++ b/src/main/java/com/fantaibao/service/FtbCrawlNetDy.java @@ -2,6 +2,7 @@ package com.fantaibao.service; import com.fantaibao.base.FtbCrawlNetBase; import com.fantaibao.config.GlobalConfig; +import com.fantaibao.config.PlaywrightManager; import com.microsoft.playwright.*; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; @@ -14,13 +15,11 @@ import java.util.concurrent.atomic.AtomicBoolean; @Slf4j public class FtbCrawlNetDy extends AbstractFtbCrawlNetBase implements FtbCrawlNetBase { - @Resource - private Playwright playwright; - @Override public void executeCookieIntercept() { // 启动可见浏览器 - try (Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false))) { + try (Browser browser = PlaywrightManager.getInstance().getPlaywright() + .chromium().launch(new BrowserType.LaunchOptions().setHeadless(false))) { // 设置浏览器窗口大小为1920x1080 Browser.NewContextOptions contextOptions = new Browser.NewContextOptions() .setViewportSize(1680, 900); diff --git a/src/main/java/com/fantaibao/service/FtbCrawlNetMt.java b/src/main/java/com/fantaibao/service/FtbCrawlNetMt.java index fb3528e..55dbbaa 100644 --- a/src/main/java/com/fantaibao/service/FtbCrawlNetMt.java +++ b/src/main/java/com/fantaibao/service/FtbCrawlNetMt.java @@ -2,6 +2,7 @@ package com.fantaibao.service; import com.fantaibao.base.FtbCrawlNetBase; import com.fantaibao.config.GlobalConfig; +import com.fantaibao.config.PlaywrightManager; import com.microsoft.playwright.*; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; @@ -14,13 +15,11 @@ import java.util.concurrent.atomic.AtomicBoolean; @Slf4j public class FtbCrawlNetMt extends AbstractFtbCrawlNetBase implements FtbCrawlNetBase { - @Resource - private Playwright playwright; - @Override public void executeCookieIntercept() { // 启动可见浏览器 - try (Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false))) { + try (Browser browser = PlaywrightManager.getInstance().getPlaywright() + .chromium().launch(new BrowserType.LaunchOptions().setHeadless(false))) { // 设置浏览器窗口大小为1920x1080 Browser.NewContextOptions contextOptions = new Browser.NewContextOptions() .setViewportSize(1680, 900);