在Weblogic Server 环境下使用JNDI的名字服务

在Weblogic Server 环境下使用JNDI的名字服务,第1张

在Weblogic Server 环境下使用JNDI的名字服务

我使用的是MyEclipse+Weblogic12c

先了解一下如何建立初始上下文。
InitialContext类实现了Context接口:调用其构造方法便可创建初始上下文(及搜索请求对象的起始点)。创建初始上下文需要设定以下两个环境属性:

  • (1)上下文工厂对象:指定要使用那个服务提供程序spi Context.INITIAL_CONTEXT_FACTORY连接字符串。
  • (2)连接字符串:指定服务的位置和初始上下文的起始点Context.PROVIDER_URL。

在Weblogic 下建立初始上下文:

  • 建立Hashtable变量,将两个环境属性存入其中。
    Hashtable ht = new Hashtable();
    ht.put(Context.INITIAL_CONTEXT_FACTORY, “weblogic.jndi.WLInitialContextFactory”);
    ht.put(Context.PROVIDER_URL, “t3://localhost:7001”);
  • 使用上述HAshtable 变量为InitialContext 构造方法的参数,创建一个Context实例。
    try{ Context etx = InitialContext(ht) ; }

接下来,需要绑定和查找对象:

  • 绑定对象:调用Context接口中的bind()和rebind()方法来实现。 Context.bind(String
    name, Object obj) Context.rebind(String name, Object obj)

  • 查找对象:调用Context接口中的Lookup(String name)方法返回当前上下文中name对应的绑定对象。
    Object Context.lookup(String name)

下面举个例子进行描述吧

对象绑定程序:

package testJNDI;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;

/**
 *@description
 * @author zhen
 * @date 2022年5月7日 下午1:56:19
 */
public class TestBind {
	public static void main(String[] args) {
		Hashtable ht = new Hashtable();
		ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
		ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
		try{
			InitialContext ctx = new InitialContext(ht);
			String str = "Hello world !";
			ctx.bind("hello", str);
			System.out.println("Object Bound !");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

查找对象程序:

package testJNDI;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;

/**
 *@description
 * @author zhen
 * @date 2022年5月7日 下午1:57:19
 */
public class TestLookup {
	public static void main(String[] args) {
		System.out.println("Object Binding...");
		Hashtable ht = new Hashtable();
		ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
		ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
		try{
			InitialContext ctx = new InitialContext(ht);
			String result;
			result = (String)ctx.lookup("hello");
			System.out.println("Result is "+result);
			System.out.println("Lookup end !");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

在这里为了方便改变环境属性,可以把环境属性添加到属性文件里面:
java.naming.factory.initial = weblogic.jndi.WLInitialContextFactory
java.naming.provider.url =t3://localhost:7001

接着就可以通过读取属性文件进行绑定对象:

package testJNDI;

import javax.naming.*;
import java.io.*;
import java.util.*;


public class TextBind_Property {
	public static void main(String[] args) {
		try{
			Properties props = new Properties();
			props.load(new FileInputStream(".\bin\testJNDI\TextBind_Property.properties")); //路径是自动生成class文件下面的路径。
			InitialContext ctx = new InitialContext(props);
			String str = "Hello Enterprise java !";
			ctx.bind("hello", str);
			System.out.println("Object Bound !");
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}

最后还要添加weblogic.jar包支持。

然好启动Weblogic服务,这没启动是不能跑起来的。

运行TextBind_Property.java程序。运行截图如下:

运行TestLookup.java程序,程序运行截图如下:

我的构建路径如图:

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/876294.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存