atitit.jndi的架构与原理以及资源配置and单元測试实践
atitit.jndi的架构与原理以及资源配置and单元測试实践
4.1. D:\workspace\wxb\src\jndi4t.xml 6
4.4. other----maybe help ful code 8
1. jndi架构
为什么会有jndi
jndi诞生的理由似乎非常easy。
随着分布式应用的发展,远程訪问对象訪问成为经常使用的方法。尽管说通过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
採纳率: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"/>
<!-- 数据源配置,在生产环境使用应用server的数据库连接池 -->
<!--<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单元測试实践的更多相关文章
- atitit.jndi的架构与原理以及资源配置and单元测试实践
atitit.jndi的架构与原理以及资源配置and单元测试实践 1. jndi架构 1 2. jndi实现原理 3 3. jndi资源配置 3 3.1. resin <database> ...
- atitit.软件与sql设计模式原理与本质 大总结attialx总结v6 qc26.docx
atitit.软件与sql设计模式原理与本质 大总结attialx总结v6 qc26.docx 1.1. 版本历史2 2. 设计模式是什么2 2.1. 模式就是在一种场合下对某个问题的一个解决方案.& ...
- Atitit 在线支付系统功能设计原理与解决方案 与目录
Atitit 在线支付系统功能设计原理与解决方案 与目录 1.1. 支付系统1 1.2. 独立的支付子体系..微服务架构..1 1.3. 参考书籍1 支付战争 [The PayPal Wars:Bat ...
- Atitit.数据库存储引擎的原理与attilax 总结
Atitit.数据库存储引擎的原理与attilax 总结 1. 存储引擎是什么1 2. 其它数据库系统(包括大多数商业选择)仅支持一种类型的数据存储2 3. 表的存储有三个文件:结构+数据+索引2 4 ...
- Atitit数据库层次架构表与知识点 attilax 总结
Atitit数据库层次架构表与知识点 attilax 总结 第一阶段,大概理论(三五天 数据库的类型,网状,层次,树形数据库,kv数据库.Oodb Er模型 sql 并发控制与lock Acid ...
- atitit.木马病毒webshell的原理and设计 java c# .net php.
atitit.木马病毒webshell的原理and设计 java c# .net php. 1. 隐蔽性 编辑 WebShell后门具有隐蔽性,一般有隐藏在正常文件中并修改文件时间达到隐蔽的,还有利用 ...
- Atitit.数据库存储引擎的原理与attilax 总结
Atitit.数据库存储引擎的原理与attilax 总结 1. 存储引擎是什么1 2. 其它数据库系统(包括大多数商业选择)仅支持一种类型的数据存储2 3. 表的存储有三个文件:结构+数据+索引2 4 ...
- Atitit 延迟绑定架构法attilax总结
Atitit 延迟绑定架构法attilax总结 配置文件的延迟绑定1 Api属性与方法的回调延迟绑定1 后期绑定和前期绑定2 延迟调用2 用 Java 语言延迟绑定2 什么是推迟绑定 C++3 配置文 ...
- Atitit 图像处理 深刻理解梯度原理计算.v1 qc8
Atitit 图像处理 深刻理解梯度原理计算.v1 qc8 1.1. 图像处理 梯度计算 基本梯度 内部梯度 外部梯度 方向梯度1 2. 图像梯度就是图像边缘吗?2 1.1. 图像处理 梯度计算 ...
随机推荐
- hdoj--2282--Chocolate(最小费用)
Chocolate Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 7.stack
#include <iostream> #include <stack> #include <algorithm> #include <list> #i ...
- 6.CPU调度
总论:所有的程序都是CPU和I/O等待交替执行 CPU调度器的操作时机 调用CPU调度器的时机,通常发生在 某一进程从执行状态转化为等待状态 某一进程从执行状态转化为就绪状态 某一进程从等待状态转为就 ...
- Functor& Monad解读
整体上代表封装的概念,重点是函数的封装,及函数运行的上下文环境.trait Functor[F[_]] Functor:代表整体封装: F[_]:代表封装后的目标域. A.B:代表普通的对象:f:代表 ...
- Vue总结(二)
原始引用:开发时使用开发版本,线上使用生产版本. 原始引用到html中,在浏览器中控制台输入Vue,输出一个函数就可以. defineProperties实现的数据绑定. //defineProper ...
- mongodb 的索引
索引加快了查询速度,但是降低了写入速度.所以不要在没必要的属性上加索引. 在 mongodb 中索引可以按倒序/正序创建,便于排序. ...
- iOS下调用元素的focus方法,input元素不聚焦,键盘不弹起的问题
页面元素 <input type="text" ref="elInput"/> <div style="margin-top:20p ...
- HDU 4889 Scary Path Finding Algorithm
其实这个题是抄的题解啦…… 题解给了一个图,按照那个图模拟一遍大概就能理解了. 题意: 有一段程序,给你一个C值(程序中某常量),让你构造一组数据,使程序输出"doge" 那段代码 ...
- 中断函数中不能使用printf的原因
vxworks 中断处理程序之所以不用printf,本质在于printf是将信息输出到标准输出设备(STDOUT)中, 整个标准输出设备是一个全局变量,由于有semTake操作,那么就会发生阻塞,vx ...
- 关于Windows7下创建Cocos2D-X项目的小问题
"新版的Cocos2D-X"已经不支持用上述脚本来创建工程了,而是改为用create-project.py来创建...命令格式: python create-project.py ...