【问题标题】:how use jndi settings in web.xml via hard coding?如何通过硬编码在 web.xml 中使用 jndi 设置?
【发布时间】:2015-01-09 15:56:37
【问题描述】:

这个属性必须在 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设置?

【问题讨论】:

    标签: java spring tomcat jndi


    【解决方案1】:

    我在这里假设您已经在 context.xml 中配置了 JNDI 连接。假设您有,那么答案很简单,只需声明一个 DataSource 字段并使用引用您的 jdbc/cms JNDI 名称的 @Resource 注释对其进行注释。像这样:

    public class DataSourceFoobar {
        @Resource(name = "jdbc/cms")
        DataSource ds;
    
        public void doStuff() {
            ds.getConnection(); //etc
            // etc ...
        }
    }
    

    如果您还没有在 context.xml 中配置它,则需要声明一个带有所有必要参数的&lt;Resource /&gt;。可能是这样的:

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

    【讨论】:

      猜你喜欢
      • 2018-02-07
      • 1970-01-01
      • 2020-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多