2016-07-04 46 views
0

我開發了基於JAX-WS的Web服務。我有Web服務層,服務層和Dao層。當我從Web服務類調用服務方法時,它會給出空指針異常。原因是服務類bean沒有被注入。JAX-WS web服務:Bean未注入:NullPointerException

網絡服務類:

package com.test.webservice.controller; 
import javax.jws.WebMethod; 
import javax.jws.WebService; 

import com.test.salary.service.SalaryService; 

@WebService 
public class EmployeeSalaryWebService { 

    private SalaryService salaryService; 


    /** 
    * @param salaryService the salaryService to set 
    */ 
    @WebMethod(exclude = true) 
    public void setSalaryService(SalaryService salaryService) { 
     this.salaryService = salaryService; 
    } 


    @WebMethod 
    public double getEmployeeSalary(String name){ 

     System.out.println("==== Inside getEmployee Salary === "+salaryService); 
     return salaryService.calculateSalary(name); 
    } 
} 

應用上下文

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

    <bean name="salaryWebService" 
     class="com.test.webservice.controller.EmployeeSalaryWebService"> 
     <property name="salaryService" ref="salaryService" /> 
    </bean> 

    <bean name="salaryService" class="com.test.salary.service.SalaryServiceImpl"> 
     <property name="salaryDAO" ref="salaryDAO" /> 
    </bean> 

    <bean name="salaryDAO" class="com.test.salary.dao.SalaryDaoImpl"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
    </bean> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close"> 
     <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> 
     <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" /> 
     <property name="username" value="LOCAL" /> 
     <property name="password" value="abcdef" /> 
    </bean> 

</beans> 

web.xml中:

<web-app 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" 
    version="2.5"> 
    <display-name>Archetype Created Web Application</display-name> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/salaryConfiguration.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

</web-app> 

請讓我知道爲什麼SalaryService salaryService沒有被注入。

回答

1

上下文中的服務類和bean是兩個單獨的事物。我相信你不會從上下文獲得bean,只是使用類,不是嗎? 我建議你

@Component 

標記服務類這將使你的類成爲春豆。 然後你可以在裏面使用註解。

@Autowired 

這將試圖在spring上下文中找到帶有註釋元素類型的合適bean。 不要忘記把你的情況。

<context:component-scan base-package="..." /> 

這將搜索標記爲@Component的所有類,並將其作爲bean添加到spring上下文中。 如需更詳細的說明,你可以檢查此文章 https://www.javacodegeeks.com/2010/11/jaxws-with-spring-and-maven-tutorial.html

+0

我跟着文章,它的工作。現在需要了解它是如何工作的:-) – Pankaj

0

讓您SalaryService自動佈線如下:

public class EmployeeSalaryWebService { 

@Autowired 
private SalaryService salaryService; 
....