本文介绍了在 Spring Boot 应用程序中使用 @autowired 数据源 getConnection()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了互联网以找到如何使用 @Autowired private DataSource dataSource; 的示例.有很多地方说您可以使用 application.properties 对其进行配置,但没有完整的使用示例.

I've scanned the internet to find an example of how to use @Autowired private DataSource dataSource;. There are lots of places that say you can configure it using application.properties, but no full usage examples.

更新

@UsmanMutawakil

@UsmanMutawakil

到目前为止,我要到哪里...

Where I've got to so far...

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 应用程序:

1st the Spring-Boot Application:

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 初始化器:

2nd Servlet Initializer:

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); 时,我得到 java.lang.NullPointerException:

This builds and deploys, but on the call to tissSolicitacaoProcedimentoController.tissSolicitacaoProcedimentoOperation(solicitacaoProcedimento); in the Endpoint, I get 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]

非常感谢任何帮助.

推荐答案

我遇到了 2 个让我找到解决方案的答案:

I have come across 2 answers which led me to a solution:

  1. 如何从 JAX-WS Web 服务中访问 ApplicationContext?

http://www.benchresources.net/metro-jax-ws-soap-based-web-service-using-top-down-approach-integrating-with-spring-framework

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

My final solution to this annoying problem:

application.properties:

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 Incializr):

1st the Spring-Boot Application (from 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 Incializr):

2nd Servlet Initializer (from 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):

Endpoint (jaxws import with alterations for the 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;
    }
}

这篇关于在 Spring Boot 应用程序中使用 @autowired 数据源 getConnection()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:57