Announcement Announcement Module
Collapse
No announcement yet.
@Autowired / @Resource issue Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @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
    I don't see where you've defined the bean of type UpdateDossierWSPortType.

    Comment


    • #3
      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

      Comment


      • #4
        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".

        Comment


        • #5
          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, 03:44 PM.

          Comment


          • #6
            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.

            Comment


            • #7
              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

              Comment


              • #8
                Thanks for the information about factories, dkarr.


                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?


                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, 06:38 AM.

                Comment


                • #9
                  Noone with an idea to troubleshoot/fix this?

                  Comment

                  Working...
                  X