No @Inject or default constructor found error in Spring

旧时模样 提交于 2021-01-28 13:03:21

问题


My code is fairly simple and outogenerated by Eclipse.

public class BuchAnalysisPresenter extends
            Presenter<BuchAnalysisPresenter.MyView, BuchAnalysisPresenter.MyProxy>
            implements BuchAnalysisUiHandlers {
        interface MyView extends View, HasUiHandlers<BuchAnalysisUiHandlers> {

            SimplePanel getMain();
        }

        @ContentSlot
        public static final Type<RevealContentHandler<?>> SLOT_BUCHNR = new Type<RevealContentHandler<?>>();

        @NameToken(NameTokens.buchnummer)
        @ProxyStandard
        interface MyProxy extends ProxyPlace<BuchAnalysisPresenter> {
        }

        @Inject
        VerlagServiceAsync verlagServiceAsync;

        @Inject
        BuchAnalysisPresenter(EventBus eventBus, MyView view, MyProxy proxy) {

            super(eventBus, view, proxy, HomePagePresenter.SLOT_SetGraphContent);
            getView().setUiHandlers(this);
        }
    }

But I get a compile error saying:

Unable to create or inherit binding: No @Inject or default constructor found for de.it2media.dps.statistics.client.application.buchnranalysis.BuchAnalysisPresenter$MyView

As you can see in the code that there is in fact a constructor BuchAnalysisPresenterand the @Injectannotation with it.

I'm very new to Spring and don't know why this happening.


回答1:


I found the solution. I only had to call the install() method of GinBinder and initialize my module BuchAnalysisModule like so. No other changes necessary.

public class ApplicationModule extends AbstractPresenterModule {
@Override
protected void configure() {
    install(new BuchAnalysisModule());

    bindPresenter(ApplicationPresenter.class,
                ApplicationPresenter.MyView.class, ApplicationView.class,
                ApplicationPresenter.MyProxy.class);
    }
}



回答2:


Have you defined a bean of type MyView in your context? If not, Spring will attempt to instantiate one, which won't work with an interface.

2 possible ways to fix this:

  1. Define a bean of the proper type in the Spring context
  2. Change the type of the constructor param:

    BuchAnalysisPresenter(EventBus eventBus, MyViewImpl view, MyProxy proxy)
    



回答3:


In My case the issue was that I was using wrong HashMap implementation in my View class like below:

Map<String, String> map = new HashMap<String, String>();

The issue was with imported classes

I was importing correct Map interface but wrong HashMap class

The imports were like:

import java.util.Map;
import com.google.gwt.dev.util.collect.HashMap;

I Replaced the HashMap by the following and it worked :)

import java.util.HashMap;


来源:https://stackoverflow.com/questions/33018123/no-inject-or-default-constructor-found-error-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!