JNDI 在Tomcat中的访问方式

初始化:

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

java:com/env为Tomcat中的初始化固定名称

 

0. Introduction

This resource factory can be used to create objects of any Java class that conforms to standard JavaBeans naming conventions (i.e. it has a zero-arguments constructor, and has property setters that conform to the setFoo() naming pattern. The resource factory will create a new instance of the appropriate bean class every time a lookup() for this entry is made.

The steps required to use this facility are described below.

1. Create Your JavaBean Class

Create the JavaBean class which will be instantiated each time that the resource factory is looked up. For this example, assume you create a class com.mycompany.MyBean, which looks like this:

package com.mycompany;

public class MyBean {

private String foo = "Default Foo";

public String getFoo() {
return (this.foo);
}

public void setFoo(String foo) {
this.foo = foo;
}

private int bar = 0;

public int getBar() {
return (this.bar);
}

public void setBar(int bar) {
this.bar = bar;
}


}

2. Declare Your Resource Requirements

Next, modify your web application deployment descriptor (/WEB-INF/web.xml) to declare the JNDI name under which you will request new instances of this bean. The simplest approach is to use a <resource-env-ref> element, like this:

1<resource-env-ref>
2 <description>
3 Object factory for MyBean instances.
4 </description>
5 <resource-env-ref-name>
6 bean/MyBeanFactory
7 </resource-env-ref-name>
8 <resource-env-ref-type>
9 com.mycompany.MyBean
10 </resource-env-ref-type>
11</resource-env-ref>
12

WARNING - Be sure you respect the element ordering that is required by the DTD for web application deployment descriptors! See the Servlet Specification for details.

3. Code Your Application's Use Of This Resource

A typical use of this resource environment reference might look like this:

1Context initCtx = new InitialContext();
2Context envCtx = (Context) initCtx.lookup("java:comp/env");
3MyBean bean = (MyBean) envCtx.lookup("bean/MyBeanFactory");
4
5writer.println("foo = " + bean.getFoo() + ", bar = " +
6 bean.getBar());
7

4. Configure Tomcat's Resource Factory

To configure Tomcat's resource factory, add an elements like this to the $CATALINA_HOME/conf/server.xml file, nested inside the Context element for this web application (or nested inside a DefaultContext element for the surrounding <Host> or <Engine> element.

1 <Context ...>
2 ...
3 <Resource name="bean/MyBeanFactory" auth="Container"
4 type="com.mycompany.MyBean"/>
5 <ResourceParams name="bean/MyBeanFactory">
6 <parameter>
7 <name>factory</name>
8 <value>org.apache.naming.factory.BeanFactory</value>
9 </parameter>
10 <parameter>
11 <name>bar</name>
12 <value>23</value>
13 </parameter>
14 </ResourceParams>
15 ...
16  </Context>
17  

 

Note that the resource name (here, bean/MyBeanFactory must match the value specified in the web application deployment descriptor.
We are also initializing the value of the bar property, which will cause setBar(23) to be called before the new bean is returned. Because
 we are not initializing the foo property (although we could have), the bean will contain whatever default value is set up by its constructor.

 

posted on 2010-11-24 20:44  aurawing  阅读(394)  评论(0编辑  收藏  举报