简体   繁体   中英

Why can't Spring instantiate my custom ApplicationContext?

Here's my code reduced down to the minimum reproducible case:

@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();
        }
    }

}

Here's the error I'm getting:

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

This seems to be saying that my AppContext class has no default constructor. But, of course, that's ALL it has. I've tried adding the default (no args) constructor explicitly, but that doesn't help, and it makes no sense that this would be necessary anyway.

I figure someone can probably tell me what's wrong just looking at my code, but I thought it couldn't hurt to provide what I'm seeing by digging further with my debugger...

I've debugged down to where the code is failing. Here's that code snippet:

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);
}

The first catch block is hit when trying to find the constructor. Here's the strange bit. It's not failing because the call to 'clazz.getDeclaredConstructor' throws an exception. It seems to fail because the 'clazz.getDeclaredConstructor' method DOES NOT EXIST! Huh? 'clazz' is a java.lang.Class object. It should clearly have that method.

在此处输入图片说明

Is it maybe just that that method is a built-in C method or something and my IntelliJ debugger can't see it?

Assuming that my debugger is just confused, why is this code failing to instantiate my class by looking for its default constructor, which it should have?

Trying making AppContext static

    private static class AppContext extends AnnotationConfigServletWebServerApplicationContext 

With non-static inner classes, there is an implicit reference to the outer class. I believe this causes a synthetic constructor to be created that doesn't match to no-arg signature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM