0

I'm having trouble getting an object of Type javax.sql.DataSource in my code. I have a application.properties in the /src/main/resources/ path which looks like this:

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

I have a bean with a call to Datasource:

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;
    }
}

which is in the same package as the generated @SpringBootApplication, ServletInitializer and @WebService classes.

The application.properties seems to be working because there are no errors on running the project in tomcat. But when I call the webservice with an appropriate soap request it fails in the procedure ConfigVlr.getValor() with a null pointer on this line:

try (Connection conn = dataSource.getConnection())

This is the call to 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 created the @SpringBootApplication annotation for me in the Application class.

Sorry if this question seems familiar, but I can't find the full answer when searching stackoverflow and the internet.

Can someone please help me to find out what I'm doing wrong?

Chrispy
  • 49
  • 1
  • 2
  • 9
  • Get rid of the pom and show us where you inject and use the `ConfigVlr` bean. – Sotirios Delimanolis Jan 18 '18 at 19:05
  • @SotiriosDelimanolis, Thanks for replying. Sorry, I'm new to this. As I understood it the spring boot would configure that. How do I inject the bean? As I stated in the question, all the configurations are in the same package. – Chrispy Jan 18 '18 at 19:13
  • Configure what? I'd like to see where you use a `ConfigVlr` object. Where do you invoke `getValor` in your code? – Sotirios Delimanolis Jan 18 '18 at 19:15
  • @SotiriosDelimanolis, Ok just a minute and I'll delete the pom and show the call. – Chrispy Jan 18 '18 at 19:16
  • @SotiriosDelimanolis - updated, thanks – Chrispy Jan 18 '18 at 19:28
  • @JB Nizet, hi. I don't know how to ask for this question to be re-evaluated as not a duplicate and so I apologise if this is not the way. I made an update to the question saying that the link you sent was for `Spring` and is not for `Spring Boot` where most of the autowiring is done automatically. All I need to know is why the `@Autowired` bean is returning a null. – Chrispy Jan 18 '18 at 22:36
  • 1
    Your initial code used `new ConfigVlr()`. Spring, whether you use Boot or not, can't inject objects that you create yourself using `new`. It only injects objects that it creates. – JB Nizet Jan 18 '18 at 22:58
  • @JBNizet - Hi. thanks for the reply. As you can see from the updated question, both ConfigVlr and DataSource are being called using ´@Autowired´, but they return null. Is there some configuration I have to do to inject them? PS. I thought that the `application.properties` entries are enough to configure DataSource. Thanks – Chrispy Jan 19 '18 at 15:51
  • This question is closed. Ask another question, and post your actual, real code. The code you posted wouldn't compile. For example, it uses the Component annotation without importing it. – JB Nizet Jan 19 '18 at 15:55
  • @JBNizet - Also, the indicated answer is too old. Thanks – Chrispy Jan 19 '18 at 16:16
  • @JBNizet - I do import it, I think I must have removed it when editing out the extra code. The code I have in Netbeans compiles, it just gives an error at runtime. I'll start another question. – Chrispy Jan 19 '18 at 16:33

0 Answers0