gpt4 book ai didi

java - tomat 7中JNDI数据源注入(inject)的@Resource注解

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:01:30 27 4
gpt4 key购买 nike

我正在处理一个访问 tomcat 7 中的 JNDI 数据源的项目。这是我的 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 类,数据源已通过上下文查找服务成功注入(inject)到我的类中,如下所示:

  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 注入(inject)。因此,您只能在 Tomcat 中使用上下文查找服务。您还可以使用依赖注入(inject)框架 spring 注入(inject)数据源,使您能够使用 @Resource 注释。

关于java - tomat 7中JNDI数据源注入(inject)的@Resource注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28572012/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com