有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java检测代码是否作为基于Spring的应用程序的一部分运行

我有一个库,它通常作为一个组件内置到第三方软件中。还有另一个库,它为主库添加了Spring支持,以防它在Spring中运行,在Spring中,诸如数据库连接之类的事情的处理方式是不同的

我想做的是确保当主库作为基于Spring的应用程序的一部分运行时,助手库也存在。因此,我需要以某种方式检测库运行的应用程序是否基于Spring。什么是明智的做法

我有一个可行的想法,那就是检查弹簧芯的某些部分是否存在,如下所示:

boolean isSpringApplication = false;
boolean isSpringSupportPresent = false;

// Check Spring
try {
   Class.forName("org.springframework.some.core.Class");
   isSpringApplication = true;
} catch(ClassNotFoundException e) {
   // This is fine
}

// Check Spring Support Lib
try {
   Class.forName("org.mylibrary.spring.support.Class");
   isSpringSupportPresent = true;
} catch(ClassNotFoundException e) {
   // This is fine
}

if (isSpringApplication && !isSpringSupportPresent) {
    LOGGER.error("Application is running within Spring but the Spring Support Library is not present.");
}

这是解决问题的合理方法吗?如果是这样的话,最好的Spring类是什么


共 (0) 个答案