【问题标题】:JAX-WS web service : Beans not injected : NullPointerExceptionJAX-WS Web 服务:未注入 Bean:NullPointerException
【发布时间】:2016-07-04 01:20:16
【问题描述】:

我开发了一个基于 JAX-WS 的 Web 服务。我有 Web 服务层、服务层和道层。当我从 Web 服务类调用服务方法时,它会给出空指针异常。原因是服务类 bean 没有被注入。

网络服务类:

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 没有被注入。

【问题讨论】:

    标签: java spring web-services


    【解决方案1】:

    上下文中的服务类和 bean 是两个不同的东西。我相信您不会从上下文中获取 bean 而只是使用类,不是吗? 我建议你用

    标记你的服务等级
    @Component
    

    这将使您的班级成为spring bean。 然后你可以在下面的注解里面使用。

    @Autowired
    

    这将尝试在 spring 上下文中找到具有注释元素类型的适当 bean。 并且不要忘记放入您的上下文中。

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

    这将搜索所有标记为@Component 的类并将其作为bean 添加到spring 上下文中。 有关更详细的说明,您可以查看这篇文章 https://www.javacodegeeks.com/2010/11/jaxws-with-spring-and-maven-tutorial.html

    【讨论】:

    • 我关注了这篇文章,它奏效了。现在需要了解它是如何工作的:-)
    【解决方案2】:

    让您的 SalaryService 自动连接如下:

    public class EmployeeSalaryWebService {
    
    @Autowired
    private SalaryService salaryService;
    ....
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      相关资源
      最近更新 更多