gpt4 book ai didi

java - JAX-WS 网络服务 : Beans not injected : NullPointerException

转载 作者:行者123 更新时间:2023-11-29 04:43:40 24 4
gpt4 key购买 nike

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

网络服务类:

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

最佳答案

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

标记你的服务等级
@Component

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

@Autowired

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

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

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

关于java - JAX-WS 网络服务 : Beans not injected : NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38175784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com