小编典典

在Spring Boot应用程序getConnection()中使用@autowired数据源

spring-boot

我已经扫描了互联网,找到了使用方法的示例@Autowired private DataSource dataSource;。许多地方说您可以使用进行配置application.properties,但没有完整的用法示例。

更新资料

@UsmanMutawakil

到目前为止我要去的地方…

application.properties

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

第一个Spring-Boot应用程序:

package br.com.empresa.solicitacaoprocedimento30302;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Solicitacaoprocedimento30302Application {

    public static void main(String[] args) {
        SpringApplication.run(Solicitacaoprocedimento30302Application.class, args);
    }
}

第二个Servlet初始化程序:

package br.com.empresa.solicitacaoprocedimento30302;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Solicitacaoprocedimento30302Application.class);
    }

}

端点(jaxws):

package br.com.empresa.solicitacaoprocedimento30302;

import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import br.com.empresa.solicitacaoprocedimento30302.controller.TissSolicitacaoProcedimentoController;
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 tissSolicitacaoProcedimentoWSEndpoint {

    @Autowired
    private TissSolicitacaoProcedimentoController tissSolicitacaoProcedimentoController;

    public br.gov.ans.padroes.tiss.schemas.AutorizacaoProcedimentoWS tissSolicitacaoProcedimentoOperation(br.gov.ans.padroes.tiss.schemas.SolicitacaoProcedimentoWS solicitacaoProcedimento) 
            throws br.gov.ans.tiss.ws.tipos.tisssolicitacaoprocedimento.v30302.TissFault,
                   SQLException, Exception 
    {
        return tissSolicitacaoProcedimentoController.tissSolicitacaoProcedimentoOperation(solicitacaoProcedimento);
    }
}

控制器:

package br.com.empresa.solicitacaoprocedimento30302.controller;

import org.springframework.beans.factory.annotation.Autowired;
import br.gov.ans.padroes.tiss.schemas.AutorizacaoProcedimentoWS;
import br.gov.ans.padroes.tiss.schemas.SolicitacaoProcedimentoWS;
import br.gov.ans.padroes.tiss.schemas.ObjectFactory;
import br.com.example.solicitacaoprocedimento30302.auxiliar.ConfigVlr;
import java.sql.SQLException;

import org.springframework.stereotype.Controller;

@Controller("tissSolicitacaoProcedimentoController")
public class TissSolicitacaoProcedimentoController {

    @Autowired
    private ConfigVlr configVlr;

    public AutorizacaoProcedimentoWS tissSolicitacaoProcedimentoOperation(SolicitacaoProcedimentoWS solicitacaoProcedimento) throws SQLException, Exception 
    {
        ObjectFactory objFact = new ObjectFactory();

        AutorizacaoProcedimentoWS retAutorizacaoProcedimentoWS = objFact.createAutorizacaoProcedimentoWS();

        retAutorizacaoProcedimentoWS.setCabecalho(objFact.createAutorizacaoProcedimentoWSCabecalho());
        retAutorizacaoProcedimentoWS.setAutorizacaoProcedimento(objFact.createAutorizacaoProcedimentoWSAutorizacaoProcedimento());

        String item = configVlr.getValor("OWNER", "REQUIRE_LOGIN_WEB_SERVICE");

        retAutorizacaoProcedimentoWS.setHash("item=" + item);

        retAutorizacaoProcedimentoWS.setHash("tissSolicitacaoProcedimentoOperation() SQLException = " + sqlEx.getMessage() + "; class = " + sqlEx.getClass() + "; tissSolicitacaoProcedimentoOperation:GotTo = " + gotTo + "/nStack Trace:/n" + sqlEx.getStackTrace());

        return retAutorizacaoProcedimentoWS;
    }
}

零件:

package br.com.empresa.solicitacaoprocedimento30302.auxiliar;

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;
import org.springframework.stereotype.Component;


@Component("configVlr")
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.PCG_OWNER.CONFIG(?, ?, ?, ?, ?) }";


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

组态:

package br.com.empresa.solicitacaoprocedimento30302.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;

import br.com.empresa.solicitacaoprocedimento30302.controller.TissSolicitacaoProcedimentoController;
import br.com.empresa.solicitacaoprocedimento30302.auxiliar.ConfigVlr;


@Configuration
public class ApplicationConfiguration {

    @Bean
    public TissSolicitacaoProcedimentoController tissSolicitacaoProcedimentoController() {
        return new TissSolicitacaoProcedimentoController();
    }

    @Bean
    public ConfigVlr configVlr() {
        return new ConfigVlr();
    }
}

这样可以构建和部署,但是在tissSolicitacaoProcedimentoController.tissSolicitacaoProcedimentoOperation(solicitacaoProcedimento);Endpoint
的调用 中,我得到java.lang.NullPointerException

2018-02-05 23:13:25.921 ERROR 16212 --- [nio-8090-exec-5] com.sun.xml.ws.server.sei.TieHandler     : null

java.lang.NullPointerException: null
    at br.com.empresa.solicitacaoprocedimento30302.tissSolicitacaoProcedimentoWSEndpoint.tissSolicitacaoProcedimentoOperation(tissSolicitacaoProcedimentoWSEndpoint.java:38) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
    at com.sun.xml.ws.util.Trampoline.invoke(MethodUtil.java:82) ~[jaxws-rt-2.3.0.jar:2.3.0]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
    at com.sun.xml.ws.util.MethodUtil.invoke(MethodUtil.java:107) ~[jaxws-rt-2.3.0.jar:2.3.0]
    at com.sun.xml.ws.api.server.MethodUtil.invoke(MethodUtil.java:64) ~[jaxws-rt-2.3.0.jar:2.3.0]
    at com.sun.xml.ws.api.server.InstanceResolver$1.invoke(InstanceResolver.java:250) ~[jaxws-rt-2.3.0.jar:2.3.0]
    at com.sun.xml.ws.server.InvokerTube$2.invoke(InvokerTube.java:149) ~[jaxws-rt-2.3.0.jar:2.3.0]
    at com.sun.xml.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:88) ~[jaxws-rt-2.3.0.jar:2.3.0]
    at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:1136) [jaxws-rt-2.3.0.jar:2.3.0]
    at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:1050) [jaxws-rt-2.3.0.jar:2.3.0]
    at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:1019) [jaxws-rt-2.3.0.jar:2.3.0]
    at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:877) [jaxws-rt-2.3.0.jar:2.3.0]
    at com.sun.xml.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:419) [jaxws-rt-2.3.0.jar:2.3.0]
    at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:868) [jaxws-rt-2.3.0.jar:2.3.0]
    at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:422) [jaxws-rt-2.3.0.jar:2.3.0]
    at com.sun.xml.ws.transport.http.servlet.ServletAdapter.invokeAsync(ServletAdapter.java:225) [jaxws-rt-2.3.0.jar:2.3.0]
    at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doGet(WSServletDelegate.java:161) [jaxws-rt-2.3.0.jar:2.3.0]
    at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doPost(WSServletDelegate.java:197) [jaxws-rt-2.3.0.jar:2.3.0]
    at com.sun.xml.ws.transport.http.servlet.WSServlet.doPost(WSServlet.java:81) [jaxws-rt-2.3.0.jar:2.3.0]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) [servlet-api.jar:na]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [servlet-api.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [catalina.jar:8.5.23]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.23]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-websocket.jar:8.5.23]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:8.5.23]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.23]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:8.5.23]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.23]
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108) [spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:8.5.23]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.23]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) [spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:8.5.23]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.23]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) [spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:8.5.23]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.23]
    at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:115) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.web.support.ErrorPageFilter.access$000(ErrorPageFilter.java:59) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.web.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:90) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:108) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:8.5.23]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.23]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) [catalina.jar:8.5.23]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [catalina.jar:8.5.23]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [catalina.jar:8.5.23]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [catalina.jar:8.5.23]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [catalina.jar:8.5.23]
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650) [catalina.jar:8.5.23]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [catalina.jar:8.5.23]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [catalina.jar:8.5.23]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-coyote.jar:8.5.23]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-coyote.jar:8.5.23]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-coyote.jar:8.5.23]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-coyote.jar:8.5.23]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-coyote.jar:8.5.23]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_131]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_131]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:8.5.23]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]

任何帮助深表感谢。


阅读 312

收藏
2020-05-30

共1个答案

小编典典

我对这个烦人的问题的最终解决方案是:

application.properties:

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

#Extra config
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource

#jpa config.
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

第一个Spring-Boot应用程序(来自spring-boot Inicializr):

package br.com.example.solicitacaoprocedimento30302;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Solicitacaoprocedimento30302Application {

    public static void main(String[] args) {
        SpringApplication.run(Solicitacaoprocedimento30302Application.class, args);
    }

}

第二个Servlet初始化程序(来自spring-boot Inicializr):

package br.com.example.solicitacaoprocedimento30302;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Solicitacaoprocedimento30302Application.class);
    }

}

端点(jaxws导入并带有WebApplicationContext的更改):

package br.com.example.solicitacaoprocedimento30302;

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.servlet.ServletContext;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.stereotype.Service;

import br.gov.ans.padroes.tiss.schemas.AutorizacaoProcedimentoWS;
import br.gov.ans.padroes.tiss.schemas.SolicitacaoProcedimentoWS;
import br.com.example.solicitacaoprocedimento30302.controller.TissSolicitacaoProcedimentoController;
import br.gov.ans.tiss.ws.tipos.tisssolicitacaoprocedimento.v30302.TissFault;

@Service("tissSolicitacaoProcedimento")
@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 TissSolicitacaoProcedimentoWSEndpoint {

    @Autowired
    private TissSolicitacaoProcedimentoController tissSolicitacaoProcedimentoController;

    @Resource
    private WebServiceContext context;

    public AutorizacaoProcedimentoWS tissSolicitacaoProcedimentoOperation(SolicitacaoProcedimentoWS solicitacaoProcedimento) 
            throws TissFault
    {
        WebApplicationContext webApplicationContext = null;

        ServletContext servletContext = (ServletContext) context.getMessageContext().get(MessageContext.SERVLET_CONTEXT);

        webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

        tissSolicitacaoProcedimentoController = webApplicationContext.getBean(TissSolicitacaoProcedimentoController.class);

        return tissSolicitacaoProcedimentoController.tissSolicitacaoProcedimentoOperation(solicitacaoProcedimento);
    }
}

控制器接口:

package br.com.example.solicitacaoprocedimento30302.controller;

import br.gov.ans.padroes.tiss.schemas.AutorizacaoProcedimentoWS;
import br.gov.ans.padroes.tiss.schemas.SolicitacaoProcedimentoWS;
import br.gov.ans.tiss.ws.tipos.tisssolicitacaoprocedimento.v30302.TissFault;

public interface TissSolicitacaoProcedimentoController {

    public AutorizacaoProcedimentoWS tissSolicitacaoProcedimentoOperation(SolicitacaoProcedimentoWS solicitacaoProcedimento) throws TissFault;

}

控制器实现:

package br.com.example.solicitacaoprocedimento30302.controller;

import br.com.example.solicitacaoprocedimento30302.TissSolicitacaoProcedimentoWSEndpoint;
import org.springframework.beans.factory.annotation.Autowired;
import br.gov.ans.padroes.tiss.schemas.AutorizacaoProcedimentoWS;
import br.gov.ans.padroes.tiss.schemas.SolicitacaoProcedimentoWS;
import br.gov.ans.padroes.tiss.schemas.ObjectFactory;
import br.com.example.solicitacaoprocedimento30302.dao.ConfigVlrDAO;
import br.gov.ans.tiss.ws.tipos.tisssolicitacaoprocedimento.v30302.TissFault;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.stereotype.Service;

@Service("tissSolicitacaoProcedimentoController")
public class TissSolicitacaoProcedimentoControllerImpl implements TissSolicitacaoProcedimentoController {

    @Autowired
    private ConfigVlrDAO configVlrDAO;

    public AutorizacaoProcedimentoWS tissSolicitacaoProcedimentoOperation(SolicitacaoProcedimentoWS solicitacaoProcedimento) throws TissFault 
    {
        ObjectFactory objFact = new ObjectFactory();

        AutorizacaoProcedimentoWS retAutorizacaoProcedimentoWS = objFact.createAutorizacaoProcedimentoWS();

        retAutorizacaoProcedimentoWS.setCabecalho(objFact.createAutorizacaoProcedimentoWSCabecalho());
        retAutorizacaoProcedimentoWS.setAutorizacaoProcedimento(objFact.createAutorizacaoProcedimentoWSAutorizacaoProcedimento());

        try 
        {
            String item = configVlrDAO.getValor("SIS", "OBRIGA_LOGIN_WEB_SERVICE");

            retAutorizacaoProcedimentoWS.setHash("item=" + item);

            return retAutorizacaoProcedimentoWS;
        } catch (SQLException sqlEx) {
            Logger.getLogger(TissSolicitacaoProcedimentoWSEndpoint.class.getName()).log(Level.SEVERE, null, sqlEx);
            throw new RuntimeException("Erro ao acessar o banco! " + sqlEx.getMessage(), sqlEx);
        } catch (Exception ex) {
            Logger.getLogger(TissSolicitacaoProcedimentoWSEndpoint.class.getName()).log(Level.SEVERE, null, ex);
            throw new RuntimeException("Falha default: " + ex.getMessage(), ex);
        }

    }
}

仓库接口:

package br.com.example.solicitacaoprocedimento30302.dao;

import java.sql.SQLException;

public interface ConfigVlrDAO {

    public String getValor(String owner, String variavel) throws SQLException, Exception;

}

存储库实现:

package br.com.example.solicitacaoprocedimento30302.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;
import java.sql.SQLException;
import org.springframework.stereotype.Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.tomcat.jdbc.pool.DataSource;

@Repository("configVlrDAO")
public class ConfigVlrDAOImpl implements ConfigVlrDAO
{
    @Autowired
    private DataSource dataSource; // Don't need to create the DataSource bean!

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

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

            cstmt.setString(1, owner);
            cstmt.setString(2, variavel);

            cstmt.registerOutParameter(3, Types.VARCHAR);
            cstmt.registerOutParameter(4, Types.NUMERIC);
            cstmt.registerOutParameter(5, Types.VARCHAR);

            cstmt.execute();

            ret = cstmt.getString(3);
            nErroProc = cstmt.getInt(4);
            sErroProc = cstmt.getString(5);

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

        return ret;
    }
}
2020-05-30