Results 1 to 9 of 9

Thread: @Autowired / @Resource issue

  1. #1
    Join Date
    Dec 2011
    Posts
    5

    Default @Autowired / @Resource issue

    Dear Spring Users,

    I've recently encountered an issue while injecting my beans, I'll try to give a short explaination which will hopefully be enough for your Spring-guru's!

    I have to call some SOAP webservice, where I generated the classes from the necessary WSDL. (Done with Apache CXF).
    At this point I have some POJO's, a webservice class and a PortType class.

    <!-- WEB SERVICE -->
    <bean id="updateDossierSOAP" class="dostk.updatedossierws_tws.UpdateDossierWS"
    factory-bean="updateDossierSOAPFactory" factory-method="create">
    </bean>

    <bean id="updateDossierSOAPFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean" >
    <property name="serviceClass" value="dostk.updatedossierws_tws.UpdateDossierWSPo rtType" />
    <property name="address" value="http://some-url-to-my-service/webservices/DOSTK/UpdateDossierWS.tws" />
    <property name="properties">
    <map>
    <entry key="schema-validation-enabled" value="true" />
    </map>
    </property>
    </bean>


    I have my factory and it needs to create my PortType class which I will use to call my webserver (UpdateDossierWSPortType).
    So here's the issue. In my class when I use @Autowiring (and the other examples below), it keeps telling me it cannot find a matching bean of type UpdateDossierWSPortType.

    @Autowired
    private UpdateDossierWSPortType updateDossierSOAP;

    @Inject
    @Named("updateDossierSOAP")
    private UpdateDossierWSPortType updateDossierSOAP;

    @Autowired
    @Qualifier("updateDossierSOAP")


    None of the above tests will load up my service class, I will keep getting "No matching bean of type found.."
    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private dostk.updatedossierws_tws.UpdateDossierWSPortType be.aquafin.dao.impl.ProjectDaoImpl.updateDossierSO AP; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [dostk.updatedossierws_tws.UpdateDossierWSPortType] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Aut owired(required=true)}

    Only when I use the @Resource annotation, it will be injected without any problem.
    I'm trying to figure out what is going wrong with using the @Autowired/@Inject annotations and what I have to do to get it working.

    Strange thing as well is the error message is talking about the type which is not found, but even when I change my applicationContext.xml to use autowiring by name (default-autowire="byName"), it will still give the same error.

    Any advice, thoughts?

    Thanks in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Seattle, Washington
    Posts
    467

    Default

    I don't see where you've defined the bean of type UpdateDossierWSPortType.

  3. #3
    Join Date
    Dec 2011
    Posts
    5

    Default

    Hi Dkarr,
    thanks for your response already.

    It is defined in the serviceClass of the factory.
    <bean id="updateDossierSOAPFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean" >
    <property name="serviceClass" value="dostk.updatedossierws_tws.UpdateDossierWSPo rtType" />

    The strange thing is it has no problem when the @Resource("updateDossierSOAP") is used.
    What we did in the end is wire the DAO that implements this class in the applicationContext:

    <bean id="projectDao" class="be.aquafin.dao.impl.ProjectDaoImpl">
    <property name="updateDossierSOAP" ref="updateDossierSOAP" />
    <property name="restTemplate" ref="restTemplate" />
    <property name="objectMapper" ref="objectMapper" />
    <property name="credentials" ref="credentials" />
    </bean>

    So this works, as does the @Resource("updateDossierSOAP").
    Now, since I'm interested what this is doing differently when using @Autowired/@Inject and why it cannot find this bean even when we are using @Qualifier/@Named.

    Any idea what could be causing this? Or how do I have the use the qualifier / named tag to make this work as it does now in the XML and/or @Resource tag

  4. #4
    Join Date
    Jan 2006
    Location
    Seattle, Washington
    Posts
    467

    Default

    As far as I can tell, it's a common misconception that "@Qualifier" looks at the "id" attribute. It does not. Only the "@Resource" annotation does that.

    Your "updateDossierSOAP" bean is of type "dostk.updatedossierws_tws.UpdateDossierWS", not "UpdateDossierWSPortType".

  5. #5
    Join Date
    Dec 2011
    Posts
    5

    Default

    Pretty strange.

    UpdateDossierWSPortType (this class has the @WebService annotation) & UpdateDossierWS (this class has the @WebServiceClient annotation) are both generated classed by CXF (from the WSDL).
    PortType is the class where we have a method to actually call the webservice.

    This is the one that is currently being called in my test, so when I used @Resource("updateDossierSOAP") it actually calls the method in UpdateDossierWSPortType. I'm not exactly sure why this is even doing that.

    But, in any case even if we change the class in the context to UpdateDossierWSPortType:
    <bean id="updateDossierSOAP" class="dostk.updatedossierws_tws.UpdateDossierWSPo rtType"
    factory-bean="updateDossierSOAPFactory" factory-method="create">
    </bean>


    What would be the correct case to get @Autowired / @Inject working? Or why is it not doing what is supposed to do? Since it is still throwing the same exception:
    Caused by: org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [dostk.updatedossierws_tws.UpdateDossierWSPortType] found for dependency: expected at least 1 bean which qualifies as autowire

    Doing this does not work either:
    @Autowired
    @Qualifier("main")
    private UpdateDossierWSPortType updateDossierSOAP;


    <bean id="updateDossierSOAP" class="dostk.updatedossierws_tws.UpdateDossierWSPo rtType"
    factory-bean="updateDossierSOAPFactory" factory-method="create">
    <qualifier value="main"/>
    </bean>


    Thanks for your response.
    Last edited by Dozzeh; Dec 8th, 2011 at 02:44 PM.

  6. #6
    Join Date
    Jan 2006
    Location
    Seattle, Washington
    Posts
    467

    Default

    First, note the following from the Spring documentation:

    'If the "factory-bean" attribute is present, the "class" attribute is not
    used, and the factory method will be an instance method on the object
    returned from a getBean call with the specified bean name. The factory
    bean may be defined as a singleton or a prototype."'

    Although your factory bean's "create()" method will return an object of type "dostk.updatedossierws_tws.UpdateDossierWSPortType " at runtime, it doesn't appear that Spring can detect that while initializing the context.

  7. #7
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

    do you have

    <context:annotation-config/>
    or
    <context:component-scan>

    in your configuration so that Spring includes the classes that search and act on @Autowired and @Inject?

    Mark

  8. #8
    Join Date
    Dec 2011
    Posts
    5

    Default

    Thanks for the information about factories, dkarr.


    Quote Originally Posted by dkarr View Post
    Although your factory bean's "create()" method will return an object of type "dostk.updatedossierws_tws.UpdateDossierWSPortType " at runtime, it doesn't appear that Spring can detect that while initializing the context.
    Can you think of anything why this behavior happens?


    Quote Originally Posted by bytor99999 View Post
    do you have
    <context:annotation-config/>
    or
    <context:component-scan>
    in your configuration so that Spring includes the classes that search and act on @Autowired and @Inject?
    Mark

    <context:component-scan base-package="package.subpackage" />
    All other beans in my class are being @Autowired at the moment without any problem.
    Only this bean is causing trouble.
    Last edited by Dozzeh; Dec 9th, 2011 at 05:38 AM.

  9. #9
    Join Date
    Dec 2011
    Posts
    5

    Default

    Noone with an idea to troubleshoot/fix this?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •