胶南做网站,在线学习网站模板,网站内页收录,咨询公司税率是多少一、JNDI在Java EE中的应用JNDI 技术是Java EE规范中的一个重要“幕后”角色#xff0c;它为Java EE容器、组件提供者和应用程序之间提供了桥梁作用#xff1a;Java EE容器同时扮演JNDI提供者角色#xff0c;组件提供者将某个服务的具体实现部署到容器上#xff0c;应用程序…一、JNDI在Java EE中的应用JNDI 技术是Java EE规范中的一个重要“幕后”角色它为Java EE容器、组件提供者和应用程序之间提供了桥梁作用Java EE容器同时扮演JNDI提供者角色组件提供者将某个服务的具体实现部署到容器上应用程序通过标准的JNDI接口就可以从容器上发现并使用服务而不用关心服务的具体实现是什么它的具体位置在哪里。下面以一个常见的J2EE应用场景来看四种角色组件接口、容器、组件提供者、应用程序是如何围绕JNDI来发挥作用的组件接口数据源DataSource是一种很常见的服务。我们通常将组件接口绑定到容器的Context上供客户调用。Java EE容器Tomcat是一种常见的Java EE容器其他的还有JBoss,WebLogic它们同时也实现了JNDI提供者规范。容器通常提供一个JNDI注入场所供加入组件的具体实现比如 Tomcat中的Server.xml配置文件。 组件提供者 众多数据库厂商提供了DataSource的实现比如OracleDataSourceMySQLDataSourceXXXDataSource 等。我们将该实现的部署到容器中将一系列jar加入classpath中在Server.xml中配置DataSource实现如 Resource namejdbc/MyDB authContainer typejavax.sql.DataSource ..../
应用程序 一个JSP/Servlet应用程序。通过JNDI接口使用 DataSource服务如 Context initContext new InitialContext(); Context envContext (Context)initContext.lookup(java:/comp/env); DataSource ds (DataSource)envContext.lookup(jdbc/MyDB);
关于在Tomcat中如何配置DataSource可以参考文档http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html。 关于在Tomcat中如何配置其他JNDI服务可以参考文档http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html。 二、 JNDI实例演练在Java SE中使用JNDI 要在Java EE中环境中提供一个独立的实例不太容易。下面以一个独立的Java SE应用来演练JNDI的使用过程。在该应用中我们使用JNDI来使用两种假想的服务数据库服务DBService和日志服务LogService。 1、指定JNDI提供者 要使用JNDI首先要配置JNDI提供者。在我们的 Java SE应用中没有Java EE容器充当JNDI提供者因此我们要指定其他的JNDI提供者。在我们的例子里我们使用SUN的文件系统服务提供者File System Service Provider。由于SUN的文件系统服务提供者并没有包含在JDK中我们需要从SUN网站上下载http://java.sun.com/products/jndi/downloads/index.html。它包含两个jar文件fscontext.jar和providerutil.jar。我们将这两个文件加入项目的类路径中。
2、定义服务接口 首先我们在服务接口package(xyz.service)中定义服务接口DBService和LogService分别表示数据库服务和日志服务。 ♦ 数据库服务接口 DBService.java
package xyz.service;public interface DBService {String getLocation(); //获取数据库位置String getState(); //获取数据库状态void accessDB(); //访问数据库void setProperty(int index,String property); //设置数据库信息
}
♦日志服务接口 LogService.java package xyz.service;public interface LogService {void log(String message); //记录日志
}
3、组件提供者实现服务接口接下来我们在组件提供者package(xyz.serviceprovider)中提供DBService和LogService的实现SimpleDBService和SimpleLogService。为了让服务能够在JNDI环境中使用根据JNDI规范我们同时定义两个对象工厂类SimpleDBServiceFactory和SimpleLogServiceFactory分别用来创建服务实例。♦ 数据库服务接口实现 SimpleDBService.javapackage xyz.serviceprovider;import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.Referenceable;
import javax.naming.StringRefAddr;import xyz.service.DBService;// 为了将数据库服务实例加入JNDI的Context中我们需要实现Referenceable接口并实现RetReference方法。
// 关于Reference和Referenceable请参考上一篇Java技术回顾之JNDIJNDI API
public class SimpleDBService implements Referenceable, DBService {private String location mydb//local:8421/defaultdb;// 数据库服务属性之一数据库位置private String state start; // 数据库服务属性之二数据库状态public Reference getReference() throws NamingException {// Reference是对象的引用Context中存放的是Reference为了从Reference中还原出对象实例// 我们需要添加RefAddr它们是创建对象实例的线索。在我们的// SimpleDBService中location和state是这样两个线索。Reference ref new Reference(getClass().getName(),SimpleDBServiceFactory.class.getName(), null);ref.add(new StringRefAddr(location, location));ref.add(new StringRefAddr(state, state));return ref;}public void accessDB() {if (state.equals(start))System.out.println(we are accessing DB.);elseSystem.out.println(DB is not start.);}public String getLocation() {return location;}public String getState() {return state;}public void setProperty(int index, String property) {if (index 0)location property;elsestate property;}
}
♦数据库服务对象工厂类 SimpleDBServiceFactory.java
package xyz.serviceprovider;import java.util.Hashtable;import javax.naming.Context;
import javax.naming.Name;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;// 数据库服务对象工厂类被JNDI提供者调用来创建数据库服务实例对使用JNDI的客户不可见。
public class SimpleDBServiceFactory implements ObjectFactory {// 根据Reference中存储的信息创建出SimpleDBService实例public Object getObjectInstance(Object obj, Name name, Context ctx, Hashtable?, ? env) throws Exception {if (obj instanceof Reference) {Reference ref (Reference) obj;String location (String) ref.get(location).getContent();String state (String) ref.get(state).getContent();SimpleDBService db new SimpleDBService();db.setProperty(0, location);db.setProperty(1, state);return db;}return null;}
}
♦ 日志服务接口实现 SimpleLogService.java package xyz.serviceprovider;import java.text.SimpleDateFormat;
import java.util.Date;import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.Referenceable;import xyz.service.LogService;public class SimpleLogService implements Referenceable, LogService {private SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);// SimpleLogService没有任何属性通过 SimpleLogService类名创建出来的SimpleLogService实例都是一样的// 因此这里无需添加RefAddr了。public Reference getReference() throws NamingException {return new Reference(getClass().getName(),SimpleLogServiceFactory.class.getName(), null);}public void log(String message) {String date sdf.format(new Date());System.out.println(date : message);}
}
♦日志服务对象工厂类 SimpleLogServiceFactory.javapackage xyz.serviceprovider;import java.util.Hashtable;import javax.naming.Context;
import javax.naming.Name;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;public class SimpleLogServiceFactory implements ObjectFactory {public Object getObjectInstance(Object obj, Name name, Context ctx,Hashtable?, ? env) throws Exception {if (obj instanceof Reference) {return new SimpleLogService();}return null;}
}4、 JNDI容器和JNDI客户端 最后我们在JNDI应用package(xyz.jndi)中实现一个JNDI容器 JNDIContainer和一个JNDI客户端应用JNDIClient。JNDIContainer在内部使用文件系统服务提供者 fscontext来提供命名和目录服务配置文件JNDIContainer.properties是服务注入场所供配置DBService和 LogService实现。♦JNDI容器类 JNDIContainer.java
package xyz.jndi;import java.io.InputStream;
import java.util.Hashtable;
import java.util.Properties;import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;import xyz.service.DBService;
import xyz.service.LogService;public class JNDIContainer {private Context ctx null;public void init() throws Exception {// 初始化JNDI提供者。HashtableObject,Object env new HashtableObject,Object();env.put(Context.INITIAL_CONTEXT_FACTORY, com.sun.jndi.fscontext.RefFSContextFactory);env.put(Context.PROVIDER_URL, file:/d:/works); // fscontext的初始目录我们需要在d:\下创建works目录。ctx new InitialContext(env);loadServices();}// 从配置文件 JNDIContainer.properties中读取DBService和LogService实现绑定到Context中。private void loadServices() throws Exception {InputStream in getClass().getResourceAsStream(JNDIContainer.properties);Properties props new Properties();props.load(in);// inject dbserviceString s props.getProperty(DBServiceClass);Object obj Class.forName(s).newInstance();if (obj instanceof DBService) {DBService db (DBService) obj;String[] ss props.getProperty(DBServiceProperty).split(;);for (int i 0; i ss.length; i)db.setProperty(i, ss[i]);ctx.rebind(props.getProperty(DBServiceName), db);}// inject logservices props.getProperty(LogServiceClass);obj Class.forName(s).newInstance();if (obj instanceof LogService) {LogService log (LogService) obj;ctx.rebind(props.getProperty(LogServiceName), log);}}public void close() throws NamingException {ctx.close();}public Context getContext() {return ctx;}
}
♦JNDI 容器配置文件 JNDIContainer.properties
//和 JNDIContainer.java文件位于同一目录
DBServiceNameDBService
DBServiceClassxyz.serviceprovider.SimpleDBService
DBServicePropertymydb//192.168.1.2:8421/testdb;startLogServiceNameLogService
LogServiceClassxyz.serviceprovider.SimpleLogService♦JNDI 客户端 JNDIClient.java
package xyz.jndi;import javax.naming.Context;import xyz.service.DBService;
import xyz.service.LogService;public class JNDIClient {public static void main(String[] args) {try {JNDIContainer container new JNDIContainer();container.init();// JNDI客户端使用标准JNDI接口访问命名服务。Context ctx container.getContext();DBService db (DBService) ctx.lookup(DBService);System.out.println(db location is: db.getLocation() ,state is: db.getState());db.accessDB();LogService ls (LogService) ctx.lookup(LogService);ls.log(this is a log message.);container.close();} catch (Exception e) {e.printStackTrace();}}
}