尝试从@Autowired属性访问DataSource时出现空指针

时间:2018-01-18 19:02:14

标签: java oracle tomcat spring-boot spring-data-jpa

我在代码中遇到javax.sql.DataSource类型的对象时遇到问题。我在application.properties路径中有/src/main/resources/,如下所示:

spring.datasource.url=jdbc:oracle:oci:@(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = IP_database)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = service_name)))
spring.datasource.username=usr_name
spring.datasource.password=usr_pass
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

我有一个调用Datasource的bean:

package com.example.chrisMavenSpringBootWSDemo;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.JDBCType;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;

@Component
public class ConfigVlr 
{
    @Autowired 
    private DataSource dataSource;

    public String getValor(String owner, String variavel) throws SQLException, Exception 
    {
        String ret = null;
        Integer nErroProc = 0;
        String sErroProc = null;
        String call = "{ call OWNER.PACKAGE.PROCEDURE(?, ?, ?, ?, ?) }";

        try (Connection conn = dataSource.getConnection())
        {
            CallableStatement cstmt = conn.prepareCall(call);

            cstmt.setString(1, owner);
            cstmt.setString(2, variavel);
            cstmt.registerOutParameter(3, JDBCType.VARCHAR, ret);
            cstmt.registerOutParameter(4, JDBCType.NUMERIC, nErroProc);
            cstmt.registerOutParameter(5, JDBCType.VARCHAR, sErroProc);

            cstmt.execute();

            if (nErroProc != 0) {
                throw new Exception("Proc error = " + nErroProc.toString() + " - " + sErroProc);
            }
        }

        return ret;
    }
}

与生成的@SpringBootApplication,ServletInitializer和@WebService类在同一个包中。

application.properties似乎正在运行,因为在tomcat中运行项目没有错误。但是,当我使用适当的soap请求调用webservice时,它在过程ConfigVlr.getValor()中失败并在此行上带有空指针:

try (Connection conn = dataSource.getConnection())

这是对ConfigVlr的调用:

package com.example.chrisMavenSpringBootWSDemo;

import javax.jws.WebService;
import br.gov.ans.padroes.tiss.schemas.*;
import java.lang.Exception;
import java.sql.SQLException;

@WebService(serviceName = "tissSolicitacaoProcedimento", 
            portName = "tissSolicitacaoProcedimento_Port", 
            endpointInterface = "br.gov.ans.tiss.ws.tipos.tisssolicitacaoprocedimento.v30302.TissSolicitacaoProcedimentoPortType", 
            targetNamespace = "http://www.ans.gov.br/tiss/ws/tipos/tisssolicitacaoprocedimento/v30302", 
            wsdlLocation = "WEB-INF/wsdl/tissSolicitacaoProcedimentoV3_03_02.wsdl")
public class chrisMavenSpringBootWSDemoFromWSDL {

    @Autowired
    private ConfigVlr cfgVlr;

    public AutorizacaoProcedimentoWS tissSolicitacaoProcedimentoOperation(SolicitacaoProcedimentoWS solicitacaoProcedimentoWS) 
            throws br.gov.ans.tiss.ws.tipos.tisssolicitacaoprocedimento.v30302.TissFault {
        ObjectFactory objFact = new ObjectFactory();

        AutorizacaoProcedimentoWS retAutorizacaoProcedimentoWS = objFact.createAutorizacaoProcedimentoWS();

        retAutorizacaoProcedimentoWS.setCabecalho(solicitacaoProcedimentoWS.getCabecalho());

        try 
        {
            //ConfigVlr cfgVlr = new ConfigVlr();

            String item = cfgVlr.getValor("OWNER", "CONFIG_VARIABLE");

            retAutorizacaoProcedimentoWS.setHash("item=" + item + " " + gotTo);
        } catch (SQLException sqlEx) {
            retAutorizacaoProcedimentoWS.setHash("tissSolicitacaoProcedimentoOperation SQLException=" + sqlEx.getMessage());
        } catch (Exception ex) {
            retAutorizacaoProcedimentoWS.setHash("tissSolicitacaoProcedimentoOperation Exception=" + ex.getMessage());
        }

        return retAutorizacaoProcedimentoWS;
    }

}

SpringBoot在Application类中为我创建了@SpringBootApplication注释。

很抱歉,如果这个问题看起来很熟悉,但在搜索stackoverflow和互联网时我找不到完整的答案。

有人可以帮助我找出我做错的事吗?

0 个答案:

没有答案