Announcement Announcement Module
Collapse
No announcement yet.
referencing object into antoher jar with @Autowired Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • referencing object into antoher jar with @Autowired

    Hi all

    My application back-end,spring based and deployed in Tomcat 7, is composed by four different .jar and one .war,which contains jax-ws interfaces webservice,and use the mentioned jars:

    1)ServiceWS.war:
    JAX-WS interfaces to expose business-service as webservice. Here,under "resource" folder,was located Spring application-context.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	                    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    	                    http://www.springframework.org/schema/context
    	                    http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    	
    	<context:annotation-config />
    	<context:component-scan base-package="it.company.myapp.ws.impl"/>
    	<context:component-scan base-package="it.company.myapp.business.services.impl"/>
    	<context:component-scan base-package="it.company.myapp.persistence.dao.impl"/>
    	
    	<!-- Other stuff -->
              ....
              ....
    2) Business.jar and Dao..jar first contains Business Logic and second contains Data Acess Logic. The Business Logic Bean was annotad with @Service and the Data Access Bean was annotad with @Repository

    In the application-context.xml,as you can see, i refer to classes in the business.jar and dao.jar by:
    Code:
    <context:component-scan base-package="it.company.myapp.business.services.impl"/>     <context:component-scan base-package="it.company.myapp.persistence.dao.impl"/>
    So,In my ws service:

    Code:
    @Component
    @[...Other Jax-Ws Annotation...]
    public ProxyWS{
    
        /* property which refers to interface in the business.jar. It's implementation class is annotated with @Service */
       @Autowired
       private IBusinessServices services;  
    
       public int executeSum(int a,int b)
       {
           return services.sum(a,b);
        }
    
    }
    So,when Tomcat 7 server starts all seems to work and console shows me a message saying that Spring context was correctly loaded! But when I try to invoke ws-service (executeSum) I have NULL-POINTER on the "services" property.

    P.S.:
    Consider if i change the:
    Code:
    <context:component-scan base-package="it.company.myapp.business.services.impl"/>
    and insert a package which not exist, an exception on server startup is thrown:
    Code:
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: it.company.myapp.business.services.IBusinessServices it.company.myapp.ws.impl.ProxyWS.service; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [it.company.myapp.business.services.IBusinessServices] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
    	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    	... 21 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [it.company.myapp.business.services.IBusinessServices] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
    	... 23 more
    Any suggestion?

  • #2
    Ok I solved ....

    The JAX-WS ENDPOINT CLASS (in my case ProxyWS) annotated with @WebService Annotation is not recognized as a Spring component,also if it was annotated with @Component. So @Autowire can't work in this class.

    But Spring,to see JAX-WS ENDPOINT CLASS as SPRING ENABLED COMPONENT,need this directive into context.xml:

    Code:
    <bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter" />
    With this directive,Spring consider all class with @WebService annotation ad Spring Component,so,we can use @Autowired annotation into our JAX-WS ENDOPOINT!

    This is great!

    Comment

    Working...
    X