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

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:workspacewxbsrcjndi4t.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:workspaceBookWebRootWEB-INFliborg.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[] argsthrows 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:workspacewxbsrcjndi4t.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

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

原文地址:https://www.cnblogs.com/attilax/p/5963659.html