JNDI @资源注释 [英] JNDI @Resource annotation
本文介绍了JNDI @资源注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在servlet和过滤器类中,我可以通过注释初始化DataSource变量
In servlet and filter classes i can initialize DataSource variable via annotation
@Resource(name = "jdbc/testDB")
protected DataSource ds;
但是它如何通过注解在基本类中初始化? 通常会抛出 NullPointerException
But how it initialize in basic class via annotation? Usually thorows NullPointerException
public class AddAuto {
@Resource(name = "jdbc/testDB")
private DataSource ds;
}
推荐答案
您的容器是什么?
如果是tomcat,则资源名称应该是这样的
If it's tomcat, the resource name should be something like this
@Resource(name = "java:/comp/env/jdbc/testDB")
protected DataSource ds;
我不知道其他容器,但是JBoss与Tomcat相同,而GlassFish作为您的值.
I don't know about the other container, but JBoss would be same as Tomcat, and GlassFish as your value.
我还建议您使用旧的查找方法,从而对调试有很大帮助
Also I suggest old lookup which help you so much for debugging
void init(){
DataSource ds=(DataSource)InitialContext.doLookup("java:/comp/env/jdbc/testDB");
}
这篇关于JNDI @资源注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文