1

我需要为使用嵌入式 Tomcat 8 应用程序服务器的应用程序设置连接池。通常,我会在 context.xml 文件中配置一个新资源。但是当然,在使用嵌入式版本时,这样的文件是不存在的。资源的定义如下所示:

<Context>
    <Resource name="jdbc/dbname" auth="Container" type="javax.sql.DataSource" username="username" password="password" driverClassName="org.postgresql.Driver" description="Database" url="jdbc:postgresql://localhost:5432/dbname" maxActive="20" maxIdle="3" />
</Context> 

因此,必须有另一种解决方案来将资源添加到上下文中。是否可以将数据源资源直接添加到代码中的 Standardcontext 中?如果是,如何?或者在使用嵌入式版本时还能如何做到这一点?

4

2 回答 2

1

您可以编写自己的工厂并将其集成到 Tomcat 中,然后在 web 应用程序的元素中配置该工厂的使用。

1.编写资源工厂类

您必须编写一个实现 JNDI 服务提供者 javax.naming.spi.ObjectFactory 接口的类。每次您的 Web 应用程序在绑定到此工厂的上下文条目上调用 lookup() 时(假设工厂配置为 singleton="false"),都会调用 getObjectInstance() 方法。

要创建一个知道如何生成 MyBean 实例的资源工厂,您可以创建一个像这样的类:

package com.mycompany;

import java.util.Enumeration;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;

public class MyBeanFactory implements ObjectFactory {

  public Object getObjectInstance(Object obj,
      Name name2, Context nameCtx, Hashtable environment)
      throws NamingException {

      // Acquire an instance of our specified bean class
      MyBean bean = new MyBean();

      // Customize the bean properties from our attributes
      Reference ref = (Reference) obj;
      Enumeration addrs = ref.getAll();
      while (addrs.hasMoreElements()) {
          RefAddr addr = (RefAddr) addrs.nextElement();
          String name = addr.getType();
          String value = (String) addr.getContent();
          if (name.equals("foo")) {
              bean.setFoo(value);
          } else if (name.equals("bar")) {
              try {
                  bean.setBar(Integer.parseInt(value));
              } catch (NumberFormatException e) {
                  throw new NamingException("Invalid 'bar' value " + value);
              }
          }
      }

      // Return the customized instance
      return (bean);

  }

}

在此示例中,我们无条件地创建 com.mycompany.MyBean 类的新实例,并根据配置此工厂的元素中包含的参数填充其属性(见下文)。您应该注意,应该跳过任何名为 factory 的参数 - 该参数用于指定工厂类本身的名称(在本例中为 com.mycompany.MyBeanFactory),而不是正在配置的 bean 的属性。

2. 声明你的资源需求

接下来,修改您的 Web 应用程序部署描述符 (/WEB-INF/web.xml) 以声明 JNDI 名称,您将在该名称下请求此 bean 的新实例。最简单的方法是使用元素,如下所示:

<resource-env-ref>
  <description>
    Object factory for MyBean instances.
  </description>
  <resource-env-ref-name>
    bean/MyBeanFactory
  </resource-env-ref-name>
  <resource-env-ref-type>
    com.mycompany.MyBean
  </resource-env-ref-type>
</resource-env-ref>

警告 - 确保遵守 DTD 要求的 Web 应用程序部署描述符的元素顺序!有关详细信息,请参阅 Servlet 规范。

3. 编码您的应用程序对该资源的使用

此资源环境参考的典型用法可能如下所示:

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
MyBean bean = (MyBean) envCtx.lookup("bean/MyBeanFactory");

writer.println("foo = " + bean.getFoo() + ", bar = " +
               bean.getBar());

4.配置Tomcat的资源工厂

要配置 Tomcat 的资源工厂,请将类似这样的元素添加到此 Web 应用程序的元素中。

<Context ...>
  ...
  <Resource name="bean/MyBeanFactory" auth="Container"
            type="com.mycompany.MyBean"
            factory="com.mycompany.MyBeanFactory"
            singleton="false"
            bar="23"/>
  ...
</Context>

资源链接:

  1. 添加自定义资源工厂
  2. 如何使用 Java 配置在 Tomcat 8 中配置 JNDI 数据源:

要在 tomcat 8 中添加外部资源,您可以点击以下链接:在 Tomcat 8 中将外部资源添加到类路径

于 2016-08-10T14:31:13.150 回答
0

问题是关于嵌入式tomcat。

于 2020-09-28T19:15:25.660 回答