tomat 7中JNDI数据源注入的@Resource注解

@Resource annotation for JNDI datasource injection in tomat 7

我正在处理一个访问 tomcat 中的 JNDI 数据源的项目 7。这是我的 META-INF/context.xml 文件:

   <?xml version="1.0" encoding="UTF-8"?>
   <Context>

    <Resource name="jdbc/ambes" auth="Container" type="javax.sql.DataSource"
           maxActive="50" maxIdle="30" maxWait="10000"
           username="root" password="" 
           driverClassName="com.mysql.jdbc.Driver"
           url="jdbc:mysql://localhost:3306/ANTTestDb"/>

   </Context>

这是我的 dao class,数据源已通过上下文查找服务成功注入我的 class,如下所示:

  public class CoffeeDataBase {

  private DataSource dataSource;

  public CoffeeDataBase() {

    try {

        Context ctx = new InitialContext();
        dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/ambes");

        } catch (Exception e) {
        e.printStackTrace();
    }

   // other methods definition

}

但是当我像这样用 @Resourse anotaion 替换查找服务时:

  @Resource(lookup="java:comp/env/jdbc/ambes")
  public class CoffeeDataBase {

  private DataSource dataSource;

  public CoffeeDataBase() {
  }

   // other methods definition

}

我得到的 NullPointerExeption 是这样的:

java.lang.NullPointerException
    at com.ambestephen.dao.CoffeeDataBase.getCoffeeList(CoffeeDataBase.java:63)
    at com.ambestephen.controller.ShowCoffees.doGet(ShowCoffees.java:16)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

我也试过 @Resource(name="jdbc/ambes") 但同样的例外!

Tomcat不支持@Resource注入。因此,您只能在 Tomcat 中工作时使用上下文查找服务。您还可以使用依赖注入框架 spring 来注入数据源,使您能够使用 @Resource 注释。