Announcement Announcement Module
Collapse
No announcement yet.
Dependency Inject with Webservices.. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dependency Inject with Webservices..

    Hi All, I've had a real hard time getting my service layer to inject into a webservice. I'm posting here out of interest in the hope someone may be able to answer why simple Autowiring never worked.

    When running a 'Test' (AbstractJpaTest), everything ties together perfectly as expected but when calling the webservice I had problems. In the end I've had to use a mix of both Autowiring and declaring the Bean in springs context files to get it to work. As a note, If I removed any of my Autowiring declaration, the extends SpringAutowiringSupport OR the definition/reference in my context file then it doesnt work. Additionally I have to use a Setter in my Webservice in order for the injection to work.

    My code follows, but as I said, I'm happy it is now working, but not so happy I don't fully understand why it doesn't work just using SpringAutowiringSupport along with @Autowired annotation.

    Thanks for reading.

    Webservice :

    Code:
    @Service("testWebService")
    @WebService(endpointInterface = "net.webservices.TestWebService")
    public class TestWebServiceImpl  extends SpringBeanAutowiringSupport implements TestWebService{
        @Autowired private ServiceManager serviceManager;
    
        @WebMethod(operationName = "sayHello")
        public String sayHello(@WebParam(name = "name")
        final String name) {
            if (serviceManager == null)
                return "Hello " + name + " Autowiring failed";
            else
                return "Hello " + name + " Autowiring succeeded!!";
        }
    
        /**
         * @return the serviceManager
         */
        public ServiceManager getServiceManager() {
            return serviceManager;
        }
    
        /**
         * @param serviceManager the serviceManager to set
         */
        public void setServiceManager(ServiceManager serviceManager) {
            this.serviceManager = serviceManager;
        }
    }
    Service Layer :

    Code:
    @Service("serviceManager")
    public class ServiceManager {
        @Autowired private DAOSample daoSample;
    
        public void doSomething() {    
            daoSample.doSomething();
        }
    }
    DAO Layer :

    Code:
    @Repository("daoSample")
    public class DAOSample {
        public void doSomething() {
            System.out.println("DAOsample did Something!");
        }
    }
    Context :

    Code:
    <bean id="testWebService"
        class="net.webservices.TestWebServiceImpl">
            <property name="serviceManager">
                <ref bean="serviceManager"/>  
            </property>
        </bean>
    Other Context :

    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"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName">
    
        <context:component-scan base-package="net"/>
               
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceUnitName" value="EPGPU" />
            <property name="dataSource" ref="dataSource" />
    
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="showSql" value="false"/>
                    <property name="generateDdl" value="false"/>
                    <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/>
                </bean>
            </property>
        </bean>
    
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost/epg"/>
            <property name="username" value="dbrown"/>
            <property name="password" value="ferret"/>
        </bean>
    
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
            <property name="dataSource" ref="dataSource"/>
        </bean>
    </beans>
    web.xml :

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/applicationContext.xml
            </param-value>
        </context-param>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <listener>
            <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
        </listener>
        <servlet>
            <servlet-name>TestWebService</servlet-name>
            <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet>
            <servlet-name>TestWebServiceImpl</servlet-name>
            <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
            </servlet>
        <servlet-mapping>
            <servlet-name>TestWebService</servlet-name>
            <url-pattern>/TestWebService</url-pattern>
        </servlet-mapping>
    
        <servlet-mapping>
            <servlet-name>TestWebServiceImpl</servlet-name>
            <url-pattern>/TestWebServiceImpl</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
      
        </web-app>

  • #2
    any solution on this? i am facing the same problem.

    Comment

    Working...
    X