Spring boot SpringBoot:System.loadLibrary运行两次
我有Spring Boot应用程序版本1.5.3.发布 在主类中,我调用System.loadLibrary:Spring boot SpringBoot:System.loadLibrary运行两次,spring-boot,Spring Boot,我有Spring Boot应用程序版本1.5.3.发布 在主类中,我调用System.loadLibrary: @SpringBootApplication @EnableScheduling public class BlaApplication { static { System.out.println("xxxxxxxxxxxxxxxxxx Loading Lib"); System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
@SpringBootApplication
@EnableScheduling
public class BlaApplication {
static {
System.out.println("xxxxxxxxxxxxxxxxxx Loading Lib");
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
public static void main(String[] args) {
SpringApplication.run(BlaApplication.class, args);
}
}
当应用程序启动时,System.loadLibrary被调用两次:
xxxxxxxxxxxxxxxxxx Loading Lib
11:10:51.766 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []
11:10:51.771 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-starter/target/classes/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot/target/classes/, /spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/]
11:10:51.771 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/Users/tomas/workspace/formater/formater-backend/target/classes/]
xxxxxxxxxxxxxxxxxx Loading Lib
Exception in thread "restartedMain" java.lang.UnsatisfiedLinkError: Native Library /usr/local/Cellar/opencv/2.4.13.2/share/OpenCV/java/libopencv_java2413.dylib already loaded in another classloader
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1907)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at com.martoma.BlaApplication.<clinit>(BlaApplication.java:16)
...
xxxxxxxxxxxxxxxx加载库
11:10:51.766[main]DEBUG org.springframework.boot.devtools.settings.DevToolsSettings-包含的重启模式:[]
11:10:51.771[main]DEBUG org.springframework.boot.devtools.settings.DevToolsSettings-排除的重新启动模式:[/spring boot starter/target/classes/,/spring boot autoconfigure/target/classes/,/spring boot starter-[\w-]+/,/spring boot/target/classes/,/spring boot-executor/target/classes/,/spring boot devtools/]
11:10:51.771[main]DEBUG org.springframework.boot.devtools.restart.changeableURL-匹配用于重新加载的URL:[文件:/Users/tomas/workspace/formatter/formatter backend/target/classes/]
XXXXXXXXXXXXXXXXXXXX加载库
线程“restartedMain”java.lang.UnsatisfiedLinkError中出现异常:本机库/usr/local/ceral/opencv/2.4.13.2/share/opencv/java/libopencv_java2413.dylib已加载到另一个类加载器中
位于java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1907)
位于java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857)
位于java.lang.Runtime.loadLibrary0(Runtime.java:870)
位于java.lang.System.loadLibrary(System.java:1122)
在com.martoma.BlaApplication.(BlaApplication.java:16)
...
为什么要调用两次?解决方案是将System.loadLibrary(Core.NATIVE\u LIBRARY\u NAME)在其自己的配置类中:
@Configuration
public class LibLoading {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
}
Java异常处理–未满足LinkError
static {
try {
// Attempt to load library.
System.loadLibrary("XXXXXX");
} catch (UnsatisfiedLinkError error) {
// Output expected UnsatisfiedLinkErrors.
} catch (Error | Exception error) {
// Output unexpected Errors and Exceptions.
}
}
可能还有另一个类在运行时加载库。如果删除此加载,库是否已加载?但问题是,此特定静态块会运行两次-如果我将logger置于System.loadLibrary(…)之上,我会在控制台中获得2个日志如果删除System.loadLibrary(…),lib根本没有加载到app中,任何对OpenCV的调用都会失败,我猜这是因为为
BlaApplication
创建了类代理。导致两个不同的类具有相同的静态块,因此被初始化两次。如何避免这种情况?