如何通过硬编码在web.xml中使用jndi设置? [英] how use jndi settings in web.xml via hard coding?
问题描述
有此属性,该属性必须在web.xml中
there is this property which must be in web.xml
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/cms</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
如何通过注解的硬编码在web.xml中使用jndi设置?
how use jndi settings in web.xml via hard coding by annotation?
推荐答案
在这里,我假设您已经在context.xml中配置了JNDI连接.假设您有,那么答案就很简单,只需声明一个DataSource字段并用一个引用您的jdbc/cms
JNDI名称的@Resource
注释对其进行注释.像这样:
I'm assuming here that you have configured the JNDI connection in context.xml. Assuming you have, then the answer is as simple as declaring a DataSource field and annotating it with an @Resource
annotation that refers to your jdbc/cms
JNDI name. Like so:
public class DataSourceFoobar {
@Resource(name = "jdbc/cms")
DataSource ds;
public void doStuff() {
ds.getConnection(); //etc
// etc ...
}
}
如果尚未在context.xml中进行配置,则需要声明带有所有必要参数的<Resource />
.也许是这样的:
Configuring it in the context.xml, if you haven't already, involves declaring a <Resource />
with all the necessary parameters. Maybe like this:
<Resource name="jdbc/cms" auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="<YOUR_DATABASE_USERNAME>"
password="<YOUR_DATABASE_PASSWORD>"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/cmsDB"/>
这篇关于如何通过硬编码在web.xml中使用jndi设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!