diff --git a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/WebApplication.java b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/WebApplication.java index 96143fc..3e4c180 100644 --- a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/WebApplication.java +++ b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/WebApplication.java @@ -2,15 +2,12 @@ package com.lanyuanxiaoyao.service.ai.web; import com.blinkfox.fenix.EnableFenix; import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties; -import org.springframework.beans.BeansException; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import org.springframework.retry.annotation.EnableRetry; import org.springframework.scheduling.annotation.EnableScheduling; @@ -27,27 +24,13 @@ import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling @EnableFenix @EnableJpaAuditing -public class WebApplication implements ApplicationRunner, ApplicationContextAware { - private static ApplicationContext context; +public class WebApplication implements ApplicationRunner { public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); } - public static T getBean(Class clazz) { - return context.getBean(clazz); - } - - public static T getBean(String name, Class clazz) { - return context.getBean(name, clazz); - } - @Override public void run(ApplicationArguments args) { } - - @Override - public void setApplicationContext(ApplicationContext context) throws BeansException { - WebApplication.context = context; - } } diff --git a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/configuration/SpringBeanGetter.java b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/configuration/SpringBeanGetter.java new file mode 100644 index 0000000..9610a2f --- /dev/null +++ b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/configuration/SpringBeanGetter.java @@ -0,0 +1,24 @@ +package com.lanyuanxiaoyao.service.ai.web.configuration; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +@Component +public class SpringBeanGetter implements ApplicationContextAware { + private static ApplicationContext context; + + public static T getBean(Class clazz) { + return context.getBean(clazz); + } + + public static T getBean(String name, Class clazz) { + return context.getBean(name, clazz); + } + + @Override + public void setApplicationContext(ApplicationContext context) throws BeansException { + SpringBeanGetter.context = context; + } +} diff --git a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/tools/ChartTool.java b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/tools/ChartTool.java index 3393066..052d259 100644 --- a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/tools/ChartTool.java +++ b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/tools/ChartTool.java @@ -1,6 +1,6 @@ package com.lanyuanxiaoyao.service.ai.web.tools; -import com.lanyuanxiaoyao.service.ai.web.WebApplication; +import com.lanyuanxiaoyao.service.ai.web.configuration.SpringBeanGetter; import lombok.extern.slf4j.Slf4j; import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.tool.annotation.Tool; @@ -44,7 +44,7 @@ public class ChartTool { """) String request ) { log.info("Enter method: mermaid[request]. request:{}", request); - ChatClient.Builder builder = WebApplication.getBean("chat", ChatClient.Builder.class); + ChatClient.Builder builder = SpringBeanGetter.getBean("chat", ChatClient.Builder.class); ChatClient client = builder.build(); return client.prompt() // language=TEXT diff --git a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/tools/KnowledgeTool.java b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/tools/KnowledgeTool.java index e22509a..5c04247 100644 --- a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/tools/KnowledgeTool.java +++ b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/tools/KnowledgeTool.java @@ -2,7 +2,7 @@ package com.lanyuanxiaoyao.service.ai.web.tools; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; -import com.lanyuanxiaoyao.service.ai.web.WebApplication; +import com.lanyuanxiaoyao.service.ai.web.configuration.SpringBeanGetter; import com.lanyuanxiaoyao.service.forest.service.KnowledgeService; import org.springframework.ai.tool.annotation.Tool; import org.springframework.ai.tool.annotation.ToolParam; @@ -27,7 +27,7 @@ public class KnowledgeTool { """) String query ) { - KnowledgeService knowledgeService = WebApplication.getBean(KnowledgeService.class); + KnowledgeService knowledgeService = SpringBeanGetter.getBean(KnowledgeService.class); var documents = knowledgeService.query(knowledgeId, query, 10, 0.5); if (ObjectUtil.isNotEmpty(documents)) { return StrUtil.format(""" diff --git a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/tools/TableTool.java b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/tools/TableTool.java index 7ec632f..b7d235c 100644 --- a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/tools/TableTool.java +++ b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/tools/TableTool.java @@ -1,7 +1,7 @@ package com.lanyuanxiaoyao.service.ai.web.tools; import cn.hutool.core.util.StrUtil; -import com.lanyuanxiaoyao.service.ai.web.WebApplication; +import com.lanyuanxiaoyao.service.ai.web.configuration.SpringBeanGetter; import com.lanyuanxiaoyao.service.forest.service.InfoService; import java.time.LocalDate; import java.time.LocalDateTime; @@ -27,7 +27,7 @@ public class TableTool { """) String sql ) { log.info("Enter method: executeJdbc[sql]. sql:{}", sql); - InfoService infoService = WebApplication.getBean(InfoService.class); + InfoService infoService = SpringBeanGetter.getBean(InfoService.class); String result = infoService.jdbc(sql) .collect(map -> map.valuesView().makeString(",")) .makeString("\n"); @@ -48,7 +48,7 @@ public class TableTool { """) String type ) { log.info("Enter method: tableCount[type]. type:{}", type); - var infoService = WebApplication.getBean(InfoService.class); + var infoService = SpringBeanGetter.getBean(InfoService.class); return switch (type) { case "logic" -> StrUtil.format(""" 逻辑表共{}张,其中重点表{}张 @@ -83,7 +83,7 @@ public class TableTool { String type ) { log.info("Enter method: version[date, type]. date:{},type:{}", date, type); - InfoService infoService = WebApplication.getBean(InfoService.class); + InfoService infoService = SpringBeanGetter.getBean(InfoService.class); String version = date; if (StrUtil.isBlank(version)) { version = LocalDateTime.now().minusDays(1).format(FORMATTER); diff --git a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/tools/YarnTool.java b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/tools/YarnTool.java index 45f9bba..728f2c9 100644 --- a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/tools/YarnTool.java +++ b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/tools/YarnTool.java @@ -1,7 +1,7 @@ package com.lanyuanxiaoyao.service.ai.web.tools; import cn.hutool.core.util.StrUtil; -import com.lanyuanxiaoyao.service.ai.web.WebApplication; +import com.lanyuanxiaoyao.service.ai.web.configuration.SpringBeanGetter; import com.lanyuanxiaoyao.service.configuration.entity.yarn.YarnApplication; import com.lanyuanxiaoyao.service.configuration.entity.yarn.YarnQueue; import com.lanyuanxiaoyao.service.configuration.entity.yarn.YarnRootQueue; @@ -27,7 +27,7 @@ public class YarnTool { """) String cluster ) { log.info("Enter method: yarnStatus[cluster]. cluster:{}", cluster); - YarnService yarnService = WebApplication.getBean(YarnService.class); + YarnService yarnService = SpringBeanGetter.getBean(YarnService.class); YarnRootQueue status = yarnService.cluster(cluster); return (status.getUsedCapacity() * 100.0) / status.getCapacity(); } @@ -45,7 +45,7 @@ public class YarnTool { """) String queue ) { log.info("Enter method: yarnQueueStatus[cluster, queue]. cluster:{},queue:{}", cluster, queue); - YarnService yarnService = WebApplication.getBean(YarnService.class); + YarnService yarnService = SpringBeanGetter.getBean(YarnService.class); YarnQueue status = yarnService.queueDetail(cluster, queue); return (status.getAbsoluteCapacity() * 100.0) / status.getAbsoluteMaxCapacity(); } @@ -66,7 +66,7 @@ public class YarnTool { """) String type ) { log.info("Enter method: yarnTaskStatus[cluster, type]. cluster:{},type:{}", cluster, type); - YarnService yarnService = WebApplication.getBean(YarnService.class); + YarnService yarnService = SpringBeanGetter.getBean(YarnService.class); ImmutableList applications = yarnService.jobList(cluster).select(app -> StrUtil.isNotBlank(type) && StrUtil.contains(app.getName(), type)); return StrUtil.format( """