JNDI就是为JAVA中命名和目录服务定义的JAVA API,是命名服务的抽象机制。在J2EE中,JNDI的目的是用来查找J2EE服务器的注册资源。只要该对象在命名服务器上注册过,且你知道命名服务器的地址和该对象在命名服务器上注册的JNDI名。这样你就可以在无需知道对象位置的情况下获取和使用对象。SUN对JNDI只提供接口,使用JNDI只需要用到JNDI接口而不必关心具体实现。

现在我们就可以在main()中启动基于RMI的JNDI服务并且绑一个对象到JNDI上。注意,我直接把JNDI的相关参数放入了System.properties中,这样,后面的代码如果要查JNDI,直接new InitialContext()就可以了

在RMI中绑JNDI的限制是,绑定的对象必须是Remote类型,所以就需要自己扩展一个。其实JNDI还有两个Context.SECURITY_PRINCIPAL和Context.SECURITY_CREDENTIAL,如果访问JNDI需要用户名和口令,这两个也要提供,不过一般用不上。下面是两个使用JNDI的简单例子的代码,可以直接运行。

使用main方法做JNDI的demo时出现NoInitialContextException是因为无法从System.properties中获得必要的JNDI参数,在服务器环境下,服务器启动时就把这些参数放到System.properties中了,于是直接new InitialContext()就搞定了。但是在单机环境下,可没有JNDI服务在运行,那就需要手动启动一个JNDI服务。在JDK 5的rt.jar中一共找到了4种SUN自带的JNDI实现:LDAP,CORBA,RMI,DNS。 
这4种JNDI要正常运行还需要底层的相应服务。一般我们没有LDAP或CORBA服务器,也就无法启动这两种JNDI服务,DNS用于查域名的,以后再研究,唯一可以在main()中启动的就是基于RMI的JNDI服务。

package com.ellen.jndi;

import java.io.Serializable;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.Date;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException; //在RMI中绑JNDI的限制是,绑定的对象必须是Remote类型
class Person implements Remote, Serializable {
private static final long serialVersionUID = -8592182872966400365L; private String name;
private String pass; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPass() {
return pass;
} public void setPass(String pass) {
this.pass = pass;
} public String toString() {
return "name=" + this.getName() + "&pass=" + this.getPass();
} } // 在RMI中绑JNDI的限制是,绑定的对象必须是Remote类型
// 外部扩展,可以内部扩展也可以外部扩展
class RemoteDate extends Date implements Remote {
}; public class Demo {
public static void initDate() throws NamingException, RemoteException {
// 设置参数
LocateRegistry.createRegistry(1099);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
System.setProperty(Context.PROVIDER_URL, "rmi://localhost:1099");
InitialContext ctx = new InitialContext();
// 在RMI中绑JNDI的限制是,绑定的对象必须是Remote类型
// 内部扩展,可以内部扩展也可以外部扩展
class RemoteDate extends Date implements Remote {
}
;
ctx.bind("java:comp/env/systemStartTime", new RemoteDate());
ctx.close();
} public static void initDate2() throws NamingException, RemoteException {
// 设置参数
LocateRegistry.createRegistry(1099);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
System.setProperty(Context.PROVIDER_URL, "rmi://localhost:1099");
InitialContext ctx = new InitialContext();
// 自己扩展,可以内部扩展也可以外部扩展
// class RemoteDate extends Date implements Remote {
// }
// ;
ctx.bind("java:comp/env/systemStartTime", new RemoteDate());
ctx.close();
} public static void findDate() throws NamingException, RemoteException {
// 直接使用
InitialContext ctx = new InitialContext();
Date startTime = (Date) ctx.lookup("java:comp/env/systemStartTime");
System.out.println("+++++++++++++++++++++++" + startTime.toString());
ctx.close();
} public static void jndiDate() throws NamingException, RemoteException {
// Demo.initDate();
Demo.initDate2();
Demo.findDate();
} public static void initPerson() throws NamingException, RemoteException {
// 设置参数
LocateRegistry.createRegistry(1099);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
System.setProperty(Context.PROVIDER_URL, "rmi://localhost:1099");
InitialContext ctx = new InitialContext();
// Person person = new Person();
// person.setName("ellen");
// person.setPass("000727");
ctx.bind("java:comp/env/person", new Person());
ctx.close();
} public static void initPerson2() throws NamingException, RemoteException {
// 设置参数
LocateRegistry.createRegistry(1099);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
System.setProperty(Context.PROVIDER_URL, "rmi://localhost:1099");
InitialContext ctx = new InitialContext();
Person person = new Person();
person.setName("ellen");
person.setPass("000727");
ctx.bind("java:comp/env/person", person);
ctx.close();
} public static void findPerson() throws NamingException, RemoteException {
// 直接使用
InitialContext ctx = new InitialContext();
Person person = (Person) ctx.lookup("java:comp/env/person");
System.out.println("------" + person.toString());
System.out.println("------" + person.getName());
System.out.println("------" + person.getPass());
ctx.close();
} public static void jndiPerson() throws NamingException, RemoteException {
// Demo.initPerson();
Demo.initPerson2();
Demo.findPerson();
} public static void main(String[] args) throws NamingException, RemoteException {
// 为什么两个jndi的例子不能同时运行
// internal error: ObjID already in use
// Demo.jndiDate();
Demo.jndiPerson();
} }

  

jndi 小案例的更多相关文章

  1. 机械表小案例之transform的应用

    这个小案例主要是对transform的应用. 时钟的3个表针分别是3个png图片,通过setInterval来让图片转动.时,分,秒的转动角度分别是30,6,6度. 首先,通过new Date函数获取 ...

  2. shell讲解-小案例

    shell讲解-小案例 一.文件拷贝输出检查 下面测试文件拷贝是否正常,如果cp命令并没有拷贝文件myfile到myfile.bak,则打印错误信息.注意错误信息中basename $0打印脚本名.如 ...

  3. [jQuery学习系列六]6-jQuery实际操作小案例

    前言最后在这里po上jQuery的几个小案例. Jquery例子1_占位符使用需求: 点击第一个按钮后 自动去check 后面是否有按钮没有选中, 如有则提示错误消息. <html> &l ...

  4. 02SpringMvc_springmvc快速入门小案例(XML版本)

    这篇文章中,我们要写一个入门案例,去整体了解整个SpringMVC. 先给出整个项目的结构图:

  5. React.js入门小案例

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...

  6. SqlDependency缓存数据库表小案例

    SqlDependency的简介: SqlDependency是outputcache网页缓存的一个参数,它的作用是指定缓存失效的数据库依赖项,可以具体到数据库和表. SqlDependency能解决 ...

  7. JavaScript apply函数小案例

    //回调函数1 function callback(a,b,c) { alert(a+b+c); } //回调函数2 function callback2(a,b) { alert(a+b); } / ...

  8. Session小案例------完成用户登录

    Session小案例------完成用户登录     在项目开发中,用户登陆功能再平常只是啦,当用户完毕username和password校验后.进入主界面,须要在主界面中显示用户的信息,此时用ses ...

  9. ch1-vuejs基础入门(hw v-bind v-if v-for v-on v-model 应用组件简介 小案例)

    1 hello world 引入vue.min.js 代码: ----2.0+版本 <div id="test"> {{str}} </div> <s ...

随机推荐

  1. Office365开发者计划——账户信息上显示已是订阅用户,点击其他授权信息,仍然需要激活

    一.申请过程跳过. 二.安装之后,点击账号,查看授权状态,如题所述. 解决办法: 链接地址:https://answers.microsoft.com/zh-hans/msoffice/forum/a ...

  2. java使用jedis访问CentOS中的redis

    pom.xml <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</ ...

  3. C# 中使用锁防止多线程冲突

    在编程的时候经常会用到多线程,有时候如果多线程操作同一个资源就会导致冲突,.NET提供了多种方法来防止冲突发生,这里讲下Mutex 该类位于System.Threading命名空间,常用的方式是这样: ...

  4. 一篇文章,教你学会Git

    在日常工作中,经常会用到Git操作.但是对于新人来讲,刚上来对Git很陌生,操作起来也很懵逼.本篇文章主要针对刚开始接触Git的新人,理解Git的基本原理,掌握常用的一些命令. 一.Git工作流程 以 ...

  5. CS229 6.16 Neurons Networks linear decoders and its implements

    Sparse AutoEncoder是一个三层结构的网络,分别为输入输出与隐层,前边自编码器的描述可知,神经网络中的神经元都采用相同的激励函数,Linear Decoders 修改了自编码器的定义,对 ...

  6. 【死磕 Spring】—— IoC 之 Spring 统一资源加载策略

    本文主要基于 Spring 5.0.6.RELEASE 摘要: 原创出处 http://svip.iocoder.cn/Spring/IoC-load-Resource/ 在学 Java SE 的时候 ...

  7. Java平台编写运行Ruby和Python

    Java不仅是一门编程语言,还是一个平台,通过JRuby和Jython,我们可以在Java平台上编写和运行Ruby和Python程序.

  8. 前端笔记二:CSS盒模型

    1.标准模型和IE模型 2.标准模型和IE模型的区别 标准模型的height和width只是content的: IE模型的height和width是包含padding和border的 3.CSS如何设 ...

  9. 零基础学习python_列表和元组(10-13课)

    一时兴起今天又回过头来补一下列表和元组,先来说说列表哈,列表其实是python最经常用到的数据类型了,不仅经常用还很强大呢,这个跟C语言里面的数组是类似的,列表当然也可以增删改查,不过我可没打算用之前 ...

  10. [Unity算法]斜抛运动

    斜抛运动: 1.物体以一定的初速度斜向射出去,物体所做的这类运动叫做斜抛运动. 2.斜抛运动看成是作水平方向的匀速直线运动和竖直方向的竖直上抛运动的合运动. 3.它的运动轨迹是抛物线. Oblique ...