如何使用 Spring 的 jee 标签使用 JNDI 引用数据源



我在Geronimo服务器中定义了一个名为LocalMysql的数据源。我可以使用Geronimo管理控制台来查询数据库并获取数据,所以我知道它至少在应用程序服务器上配置正确。

我的下一步是在Eclipse中创建一个Spring MVC项目。我只使用模板生成的代码,在servlet-context.xml中添加了以下几行

在"beans"声明中:

<beans ... xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="...
                    http://www.springframework.org/schema/jee 
                    http://www.springframework.org/schema/jee/spring-jee.xsd"

然后,在文件的正文中添加:

<jee:jndi-lookup id="mysqlDatasource" jndi-name="LocalMysql"/>

我还需要什么来建立连接?

编辑并更新

除了下面在web.xml中列出的标签之外,我还在geronimo-web.xml中添加了以下内容:

<dep:dependency>
  <dep:groupId>console.dbpool</dep:groupId>
  <dep:artifactId>LocalMysql</dep:artifactId>
  <dep:version>1.0</dep:version>
  <dep:type>car</dep:type>
</dep:dependency>

以及:

<name:resource-ref>
  <name:ref-name>LocalMysql</name:ref-name>
  <name:resource-link/>
</name:resource-ref>

即使添加了这些内容,我仍然会得到以下异常:

Unable to resolve resource reference 'java:comp/env/LocalMysql' 
(Could not find resource ''.  Perhaps it has not yet been configured, 
or your application does not have a dependency declared for that 
resource module?
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/yourresource"
        expected-type="javax.sql.DataSource" resource-ref="true" />

以及您的web.xml:中的参考

<resource-ref>
    <res-ref-name>jdbc/yourresource</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

最新更新