在spring boot应用程序中使用@autowired数据源getConnection()

时间:2018-01-20 04:09:14

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

我已经扫描了互联网,找到了如何使用@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);时,我得到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 个答案:

答案 0 :(得分:1)

我遇到了2个答案,这使我找到了解决方案:

  1. How can I access the ApplicationContext from within a JAX-WS web service?

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

  3. 我对这个恼人问题的最终解决方案:

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

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

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

答案 1 :(得分:0)

您没有DataSource bean。如果您在Spring环境中有一个数据源,那么它将自动连接到该实现接口位置。在配置类中配置一个dataSource bean(如果你勇敢的话,配置XML),这应该可行。您的application.proprties文件仅公开可以在应用程序中的其他位置使用的值,如$ spring.datasource.url,而不是键入实际的URL。

您可以在数据源bean创建逻辑中使用application.properties文件的属性,但只是将这些值添加到applicaton.properties不会创建所需的数据源bean。

XML示例:

void box(const Mat &dots, Mat &newdots, int rows, int cols)
{
    for (int i = 0; i < dots.rows; i++) {
        for (int j = 0; j < dots.cols; j++) {
            newdots.at<Vec3b>(i, j)[0] = dots.at<Vec3b>(i, j)[0]; 
            newdots.at<Vec3b>(i, j)[1] = dots.at<Vec3b>(i, j)[1]; //add this with 3
            newdots.at<Vec3b>(i, j)[2] = dots.at<Vec3b>(i, j)[2]; //add this with 5
        }
    }

如果使用@Configuration类,则可以在没有XML的情况下实现相同的功能。在该类中,您可以使用Java而不是XML定义与上面相同的内容,并且您需要显式值,您可以使用引用值(例如$ {blah})引用application.properties。

@Configuration Example

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
   <property name="driverClassName" value="${dataSource.driverClassName}" />
       <property name="url" value="${dataSource.url}" />
       <property name="username" value="${dataSource.username}" />
       <property name="password" value="${dataSource.password}" />
       <property name="initialSize" value="10"/>
   <property name="maxActive" value="400"/>
   <property name="testWhileIdle" value="true"/>
   <property name="validationQuery" value="${dataSource.validationQuery}"/>
   <property name="validationInterval" value="600000"/>
   <property name="timeBetweenEvictionRunsMillis" value="600000"/>
   <property name="minIdle" value="10"/>
   <property name="maxIdle" value="100"/>
   <property name="testOnBorrow" value="true"/>
   <property name="removeAbandoned" value="true"/>
   <property name="connectionProperties" value="encrypt=true;TrustServerCertificate=true"/>
</bean>