How can I spoof a jndi lookup for a datasource without a app server

后端 未结 3 1332
别跟我提以往
别跟我提以往 2021-02-04 18:24

I want to test some new functionality which is part of an internal web app. This new code uses a database connection normally provided by an app server (tomcat).

I do no

3条回答
  •  野性不改
    2021-02-04 18:44

    With TomcatJNDI you can access every JNDI resource you configured within Tomcat as used to do in your web application. The code to achieve it is simple and looks like

    TomcatJNDI tomcatJNDI = new TomcatJNDI();
    tomcatJNDI.processContextXml(contextXmlFile);
    tomcatJNDI.start();
    
    DataSource ds = (DataSource) InitialContext.doLookup("java:comp/env/path/to/datasource")
    

    Go here to read more about it.

提交回复
热议问题