這是我的代碼減少到最小的可重現情況:@SpringBootApplication@public class IfserverApplication { private class AppContext extends AnnotationConfigServletWebServerApplicationContext { } public static void main(String[] args) { new Configurator("IFServer"); try { SpringApplication app = new SpringApplication(IfserverApplication.class); app.setApplicationContextClass(AppContext.class); app.run(args); } catch (Throwable e) { e.printStackTrace(); } }}這是我得到的錯誤:Caused by: java.lang.NoSuchMethodException: com.inlet.ifserver.IfserverApplication$AppContext.<init>() at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_192] at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_192] at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:122) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] ... 3 common frames omitted這似乎是說我的 AppContext 類沒有默認構造函數。但是,當然,這就是它的全部。我已經嘗試顯式添加默認(無參數)構造函數,但這沒有幫助,而且無論如何這都是必要的,這是沒有意義的。我想有人可能只看我的代碼就可以告訴我出了什么問題,但我認為通過使用調試器進一步挖掘來提供我所看到的內容并沒有什么壞處......我已經調試到代碼失敗的地方。這是該代碼片段:try { return instantiateClass(clazz.getDeclaredConstructor());} catch (NoSuchMethodException var3) { Constructor<T> ctor = findPrimaryConstructor(clazz); if (ctor != null) { return instantiateClass(ctor); } else { throw new BeanInstantiationException(clazz, "No default constructor found", var3); }} catch (LinkageError var4) { throw new BeanInstantiationException(clazz, "Unresolvable class definition", var4);}嘗試查找構造函數時會命中第一個 catch 塊。這是奇怪的一點。它沒有失敗,因為對 'clazz.getDeclaredConstructor' 的調用會引發異常。它似乎失敗了,因為“clazz.getDeclaredConstructor”方法不存在!嗯?'clazz' 是一個 java.lang.Class 對象。它顯然應該有這種方法。是否可能只是該方法是內置的 C 方法或其他東西,而我的 IntelliJ 調試器看不到它?假設我的調試器只是感到困惑,為什么這段代碼無法通過查找它應該具有的默認構造函數來實例化我的類?
1 回答

一只名叫tom的貓
TA貢獻1906條經驗 獲得超3個贊
嘗試制作AppContext
靜態
private static class AppContext extends AnnotationConfigServletWebServerApplicationContext
對于非靜態內部類,存在對外部類的隱式引用。我相信這會導致創建一個與無參數簽名不匹配的合成構造函數。
添加回答
舉報
0/150
提交
取消