atitit.jndi的架构与原理以及资源配置and单元测试实践

1. jndi架构 1

2. jndi实现原理
3

3. jndi资源配置 3

3.1. resin  <database>  节点 3

3.2. tomcat    <resource 标签 4

3.3. 自定义资源 5

4. JNDI测试支持: 5

4.1. D:\workspace\wxb\src\jndi4t.xml 6

4.2. applicationContext.xml 7

4.3. jdbc.properties 7

4.4. other----maybe help ful code 8

5. 参考 8

1. jndi架构

为什么会有jndi

jndi诞生的理由似乎很简单。随着分布式应用的发展,远程访问对象访问成为常用的方法。虽然说通过Socket等编程手段仍然可实现远程通信,但按照模式的理论来说,仍是有其局限性的。RMI技术,RMI-IIOP技术的产生,使远程对象的查找成为了技术焦点。JNDI技术就应运而生。JNDI技术产生后,就可方便的查找远程或是本地对象。

作者:: 老哇的爪子 Attilax 艾龙,  EMAIL:1466519819@qq.com

转载请注明来源: http://blog.csdn.net/attilax

1. try{

2.     Context initCtx = new javax.naming.InitialContext();

3.     Context envCtx = (Context)initCtx.lookup("java:comp/env");    //官方的example写法

4.     DataSource ds = (DataSource)envCtx.lookup("jdbc/YourDataBase"); // 或下面这样写:

5.     // DataSource ds = (DataSource)intCtx.lookup("java:comp/env/jdbc/YourDataBase");

6.     return ds.getConnection();

7. }catch(Exception e){

8.     throw e;

9. }

2. jndi实现原理

hgjg0k0n1m 10级  分类: JAVA语言  被浏览42次  2013.07.09

yingwang2011

采纳率:51% 10级 2013.07.09

jndi类似一个大map 将一些资源以key-value的形式放到jndi。 需要的时候再用key查询出来。 value,可能是一个字符串也可能是一个对象。

3. jndi资源配置

3.1. resin  <database>  节点

<database>

<jndi-name>jndi/wxb_site_new</jndi-name>

<driver type="com.mysql.jdbc.Driver">

<url><![CDATA[jdbc:mysql://localhost:3306/wxb_site_new?useUnicode=true&characterEncoding=utf-8]]></url>

<user>root</user>

<password>root</password>

</driver>

<prepared-statement-cache-size>8</prepared-statement-cache-size>

<max-connections>20</max-connections>

<max-idle-time>30s</max-idle-time>

</database>

<!--

<resource>

<max-connections>20</max-connections>

</resource>

-->

</resin>

3.2. tomcat    <resource 标签

resin  的resource 语法

<resource> syntax: (( (@jndi-name | <jndi-name>)?

& (@name | <name>)?

& (@var | <var>)?

& (@mbean-name | <mbean-name>)?

& (@mbean-interface | <mbean-interface>)?),

((@type | <type> | @class | <class>),

<arg>*)?,

(<init>*

& (@local-transaction-optimization | <local-transaction-opt

mization>)?

& <mbean-listener>*

& (@shareable | <shareable>)?))

3.3. 自定义资源

tomcat默认提供的jndi配置支持对象有限,比较常用的有datasource,javabean等,

有时无法满足用户的需求 。比如需要在构建对象的构造函数中传递参数等情况。

4. JNDI测试支持:

import org.springframework.mock.jndi.SimpleNamingContextBuilder;

D:\workspace\Book\WebRoot\WEB-INF\lib\org.springframework.test-3.1.0.RELEASE.jar

JNDI测试支持:在org.springframework.mock.jndi包下通过了SimpleNamingContextBuilder来来创建JNDI上下文Mock对象,从而无需依赖特定Java EE容器即可完成JNDI测试。

参考Junit+spring创建JNDI运行测试用例

public static void main(String[] args) throws IllegalStateException,

NamingException {

// System.setProperty("java.naming.factory.initial", "");

String p = pathx.classPath() + "/";

SpringUtil.locations = new String[] { p + "applicationContext.xml",

p + "jndi4t.xml" };

DataSource ds = (DataSource) SpringUtil.getBean("dataSource");

SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();

builder.bind("java:/comp/env/jndi/wxb_site_new", ds);

builder.activate();

UserInfoService userInfoService = (UserInfoService) SpringUtil

.getBean("userInfoService");

Dto pDto = new BaseDto();

pDto.put("user_id", "23946449");

UserInfo   userInfo = userInfoService.checkLogin(pDto);

System.out.println(userInfo);

}

有俩个dataSource一样了三ok这了,改成不一样走错蓝。。

4.1. D:\workspace\wxb\src\jndi4t.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />

<beans:property name="url" value="jdbc:mysql://localhost:3306/wxb_site_new?useUnicode=true&characterEncoding=utf-8" />

<beans:property name="username" value="root" />

<beans:property name="password" value="root" />

</beans:bean>

</beans:beans>

4.2. applicationContext.xml

<!-- 定义受环境影响易变的变量 -->

<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 数据源配置,在生产环境使用应用服务器的数据库连接池 -->

<!--<jee:jndi-lookup id="dataSource" jndi-name="${ds}" ></jee:jndi-lookup> -->

<bean id="dataSource"

class="org.springframework.jndi.JndiObjectFactoryBean">

<property name="jndiName">

<!-- <value>java:comp/env/jdbc/my_center</value> -->

<value>${ds}</value>

</property>

</bean>

4.3.  jdbc.properties

# Properties file for JDBC configuration

#

# Place this file in the root CLASSPATH

#MySQL Connection Configuration

ct.jdbc.driverClassName=com.mysql.jdbc.Driver

ct.jdbc.url=jdbc:mysql://localhost:3306/wxb_site_new?useUnicode=true&characterEncoding=utf-8

ct.jdbc.username=root

ct.jdbc.password=

ds =java\:/comp/env/jndi/wxb_site_new

4.4. other----maybe help ful code

0

String url="localhost:8080";

这是在Weblogic中的写法:

      properties = new Properties();

      properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");

      properties.put(Context.PROVIDER_URL, url);

      //用户是否为空

      if (user != null) {

        properties.put(Context.SECURITY_PRINCIPAL, user);

        properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);

      }

      Context ctx= new InitialContext(properties);



      DataSource ds =(DataSource) ctx.lookup("java:comp/env/jdbc/OracleDB");

      Connection conn = ds.getConnection();



查找相关资料,把

 properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");

换成相应的tomcat对应的上下文工厂,应该就可以了。

5. 参考

Resin中jndi资源配置 - howareyouo的专栏 - 博客频道 - CSDN.NET.htm

JNDI全攻略(一) - chinaifne - 博客园.htm

TOMCAT自定义JNDI - 我的java 博客,嘿嘿 - ITeye技术网站.htm

各位谁知道怎样在一个测试类的main()函数里访问tomcat的JNDI数据源 -CSDN论坛-CSDN.NET-中国最大的IT技术社区.htm

高分请教Spring做单元测试的时候如果通过mock虚拟jndi数据源呢_IT知识问答_希赛网.htm

Junit+spring创建JNDI运行测试用例 - CSDN博客.htm

atitit.jndi的架构与原理以及资源配置and单元测试实践的更多相关文章

  1. atitit.jndi的架构与原理以及资源配置and单元測试实践

    atitit.jndi的架构与原理以及资源配置and单元測试实践 1. jndi架构 1 2. jndi实现原理 3 3. jndi资源配置 3 3.1. resin  <database> ...

  2. atitit.软件与sql设计模式原理与本质 大总结attialx总结v6 qc26.docx

    atitit.软件与sql设计模式原理与本质 大总结attialx总结v6 qc26.docx 1.1. 版本历史2 2. 设计模式是什么2 2.1. 模式就是在一种场合下对某个问题的一个解决方案.& ...

  3. Atitit 在线支付系统功能设计原理与解决方案 与目录

    Atitit 在线支付系统功能设计原理与解决方案 与目录 1.1. 支付系统1 1.2. 独立的支付子体系..微服务架构..1 1.3. 参考书籍1 支付战争 [The PayPal Wars:Bat ...

  4. Atitit.数据库存储引擎的原理与attilax 总结

    Atitit.数据库存储引擎的原理与attilax 总结 1. 存储引擎是什么1 2. 其它数据库系统(包括大多数商业选择)仅支持一种类型的数据存储2 3. 表的存储有三个文件:结构+数据+索引2 4 ...

  5. Atitit数据库层次架构表与知识点 attilax 总结

    Atitit数据库层次架构表与知识点 attilax 总结 第一阶段,大概理论(三五天 数据库的类型,网状,层次,树形数据库,kv数据库.Oodb Er模型   sql 并发控制与lock  Acid ...

  6. atitit.木马病毒webshell的原理and设计 java c# .net php.

    atitit.木马病毒webshell的原理and设计 java c# .net php. 1. 隐蔽性 编辑 WebShell后门具有隐蔽性,一般有隐藏在正常文件中并修改文件时间达到隐蔽的,还有利用 ...

  7. Atitit.数据库存储引擎的原理与attilax 总结

    Atitit.数据库存储引擎的原理与attilax 总结 1. 存储引擎是什么1 2. 其它数据库系统(包括大多数商业选择)仅支持一种类型的数据存储2 3. 表的存储有三个文件:结构+数据+索引2 4 ...

  8. Atitit 延迟绑定架构法attilax总结

    Atitit 延迟绑定架构法attilax总结 配置文件的延迟绑定1 Api属性与方法的回调延迟绑定1 后期绑定和前期绑定2 延迟调用2 用 Java 语言延迟绑定2 什么是推迟绑定 C++3 配置文 ...

  9. Atitit 图像处理 深刻理解梯度原理计算.v1 qc8

    Atitit 图像处理 深刻理解梯度原理计算.v1 qc8 1.1. 图像处理  梯度计算  基本梯度 内部梯度 外部梯度 方向梯度1 2. 图像梯度就是图像边缘吗?2 1.1. 图像处理  梯度计算 ...

随机推荐

  1. 【spring colud】spring cloud微服务项目搭建【spring boot2.0】

    spring cloud微服务项目搭建 =================================== 示例版本: 1.spring boot 2.0版本 2.开发工具 IntellJ IDE ...

  2. jquery缩写,显示隐藏

    $(".a").css("display")=="none" ?$(".a").css("display&qu ...

  3. Oracle 11gR2 RAC的两个bug

      Oracle 11.2.0.2 bug还是不少的.很多库迁到Oracle 11.2.0.2后都遇到了问题.现在正在跟的两个Oracle 11.2.0.2上的问题:1.ORA-00600: inte ...

  4. 项目中集成Easyui-Tree,可拖拽更新节点

    由于公司架构较旧,使用的jdk版本为1.4,页面上也没有el表达式. Html <ul class="easyui-tree" id="data"> ...

  5. 如何解决weblogic server启动中在IIOP后运行缓慢

    WebLogic Server在Linux环境中,有时因为linux OS的安全包没有安装,导致weblogic server 在启动的时候会在长时间的停留在 <2/07/2009 08:54: ...

  6. Javascript中的对象(二)

    Javascript是一种基于原型的对象语言,而不是我们比较熟悉的,像C#语言基于类的面向对象的语言.在前一篇文章中,我们已经介绍了Javascript中对象定义的创建.接下来我们来介绍一下Javas ...

  7. Codeforces 570D TREE REQUESTS dfs序+树状数组 异或

    http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...

  8. Strategy Pattern(策略模式)

    Head First定义: 策略模式定义了算法族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化独立于使用算法的客户. 策略模式的设计原则主要有三个: 找出应用中可能需要变化的部分,把它们独 ...

  9. Hive不等值连接

    select * from ( select t1.instalment_id as r_id , t2.instalment_id as p_id from (select instalment_i ...

  10. 【云计算】Docker build解决父镜像层级关系过多问题:Cannot create container with more than 127 parents

    docker export 8a6e92c71a77 > malakas.tzr.gz cat malakas.tzr.gz|docker import - /nscloud/malakas:1 ...