Failed to instantiate [..]: No default constructor found;

Report
Question

Please briefly explain why you feel this question should be reported .

Report Cancel

I’m getting:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mz.server.rest.braintree.webhooks.SubscriptionWebhook]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.mz.server.rest.braintree.webhooks.SubscriptionWebhook.<init>()
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098)
    ... 22 more

Even though I have defined this constructor in my applicatioContext-restapi.xml file:

<bean id="subscriptionWebhook" class="com.mz.server.rest.braintree.webhooks.SubscriptionWebhook">
    <constructor-arg ref="dslContext" />
</bean>

Any idea why?

@RestController
public class SubscriptionWebhook {

    private final static Logger LOGGER = Logger.getLogger(SubscriptionWebhook.class.getName());

    private AdminService adminService;      

    public SubscriptionWebhook(DSLContext ctx) {
        this.adminService = new AdminService(ctx);
    }
}
solved 0
1 Answer 26 views 0

Answer ( 1 )

    1
    December 24, 2016 at 3:01 pm

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    Since Spring 3(ish) you can configure your container by applying @Component annotations to classes. @Controller annotation is defined as follows:

    @Target(value=TYPE)
    @Retention(value=RUNTIME)
    @Documented
    @Component
    public @interface Controller
    

    Which means classes annotated by it are going to get picked up, too. And RestController is just Controller and ResponseBody put together.

    Anyway as you admitted in the comments you have enabled component scanning, and the configuration in xml is not going to get picked up in this case.

    What you could do is convert the xml configuration to annotation-based injection, like that:

    @RestController
    public class SubscriptionWebhook {
    
        private final static Logger LOGGER = Logger.getLogger(SubscriptionWebhook.class.getName());
    
        private AdminService adminService;      
    
        public SubscriptionWebhook(@Qualifier("dslContext") DSLContext ctx) {
            this.adminService = new AdminService(ctx);
        }
    }
    

    Qualifier annotation is going to look in the container for a bean with name/id dslContext and inject it in the constructor. Alternatively you can use the javax.inject Named annotation, or if this is the only bean with that type, Spring’s @Autowired or JSR-330’s @Inject.

    Best answer

Leave an answer

Browse

What is the capital of Egypt ? ( Cairo )

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>