how use jndi settings in web.xml via hard coding?

岁酱吖の 提交于 2019-12-13 03:41:55

问题


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>

how use jndi settings in web.xml via hard coding by annotation?


回答1:


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 ...
    }
}

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"/>


来源:https://stackoverflow.com/questions/27864385/how-use-jndi-settings-in-web-xml-via-hard-coding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!