Web服务中的Java空指针异常@autowired

时间:2019-02-06 09:58:41

标签: java spring oracle nullpointerexception jdeveloper

错误:domain.SpeakerRecog.test上的java.lang.NullPointerException

speakermapper为空,错误speakermapper.insertUserAndEnrollmentId(userid, enrollmentid)上它引发Java空指针异常。数据源是在控制台中创建的。我尝试添加@Configurationimplements之类的注释,但仍然无法正常工作。我试图将数据插入localhost中数据库的表中。它与线程“为什么我的Spring @Autowired字段为null为何”不同。因为我没有创建新的构造函数,但是我遵循了那里给出的建议,但仍然无法正常工作

WebService Java文件:

package domain;

import javax.jws.WebMethod;
import javax.jws.WebService;
import mapper.Speakermapper;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import org.springframework.beans.factory.annotation.Autowired;


@WebService(serviceName = "SpeakerRecognitionWS")

public class SpeakerRecog extends SpringBeanAutowiringSupport{


    @Autowired
    private Speakermapper speakermapper;


    @WebMethod
    public String test(){


        String userid = "111";
        String enrollmentid = "111";
        try{ 
        speakermapper.insertUserAndEnrollmentId(userid, enrollmentid);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }    
    }
}

这是Speakermapper.java文件:

package mapper;
import org.apache.ibatis.annotations.Param;

public interface Speakermapper {
    public void insertUserAndEnrollmentId(@Param("userid") String userid, 
                            @Param("enrollmentid") String enrollmentid);

}

**这里是Speakermapper.xml:

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace = "mapper.Speakermapper">
<insert id = "insertUserAndEnrollmentId" >
Insert into SpeakerIds 
(userid, enrollmentid)
Values (#{userid}, #{enrollmentid})
</insert>
</mapper>

**这是applicationContext.xml:此xml是否存在错误。我想不通。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation= "http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx 
                http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop 
                http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
        <context:component-scan base-package="domain"/> 
        <context:component-scan base-package="mapper"/> 
        <bean id="SpeakerRecog"  class="domain.SpeakerRecog" />
        <bean id="smapper"  class="mapper.speakermapper" />

        <bean id="dataSourceSpeaker" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="Speaker"/>
        </bean>

        <bean id="sqlSessionFactorySpeaker" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSourceSpeaker"/>
            <property name="typeAliasesPackage" value="domain"/>
            <property name="configLocation" value="/WEB-INF/mybatis-config.xml"/>
        </bean>

        <bean id="mapperSpeaker" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="mapper" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactorySpeaker" />
        </bean>
</beans>

1 个答案:

答案 0 :(得分:0)

您可以将其明确标记为服务,或者不使用接口而是使用类。

再次查看--->也许是问题

  <bean id="smapper"  class="mapper.speakermapper" />

大S的发言人映射器

  <bean id="smapper"  class="mapper.Speakermapper" />
package mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

@Service
public class Speakermapper {
    public void insertUserAndEnrollmentId(@Param("userid") String userid, 
                            @Param("enrollmentid") String enrollmentid);

}

更新3

<bean id="speakermapper"  class="mapper.Speakermapper" />
<bean id="SpeakerRecog"  class="domain.SpeakerRecog" >
     <property name="speakermapper" ref="speakermapper" />
</bean>

也请检查此链接:

Spring Autowired in Web Service not working