<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>涂0实验室 &#187; jndi</title>
	<atom:link href="http://www.sulong.info/archives/tag/jndi/feed" rel="self" type="application/rss+xml" />
	<link>http://www.sulong.info</link>
	<description>一个程序员的成长之路</description>
	<lastBuildDate>Fri, 27 Aug 2010 01:54:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>通过Annotation和Struts2的interceptor实现向Action中注入jndi资源</title>
		<link>http://www.sulong.info/archives/37</link>
		<comments>http://www.sulong.info/archives/37#comments</comments>
		<pubDate>Wed, 14 Nov 2007 09:19:54 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[jndi]]></category>
		<category><![CDATA[struts2]]></category>

		<guid isPermaLink="false">http://www.sulong.info/archives/37</guid>
		<description><![CDATA[为了实现向struts2的Action中注入ejb3，我写了一个Annotation用来定义要注入哪个ejb3，又写了一个struts2的interceptor用来实施注入。Annotation代码如下： @Retention&#40;value=RetentionPolicy.RUNTIME&#41; @Target&#40;value=&#123;ElementType.METHOD&#125;&#41; @Inherited public @interface JndiLookup &#123; &#160; &#160; String jndi&#40;&#41;; &#160; &#160; Class type&#40;&#41;; &#125; Interceptor代码如下： public class JndiLookupInterceptor implements Interceptor &#123; &#160; &#160; private InitialContext contaxt; &#160; &#160; public void destroy&#40;&#41; &#123; &#160; &#160; &#160; &#160; ; &#160; &#160; &#125; &#160; &#160; public void init&#40;&#41; &#123; &#160; &#160; &#160; &#160; try &#123; [...]]]></description>
			<content:encoded><![CDATA[<p>为了实现向struts2的Action中注入ejb3，我写了一个Annotation用来定义要注入哪个ejb3，又写了一个struts2的interceptor用来实施注入。Annotation代码如下：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@Retention<span style="color: #009900;">&#40;</span>value<span style="color: #339933;">=</span>RetentionPolicy.<span style="color: #006633;">RUNTIME</span><span style="color: #009900;">&#41;</span><br />
@Target<span style="color: #009900;">&#40;</span>value<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span>ElementType.<span style="color: #006633;">METHOD</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
@Inherited<br />
<span style="color: #000000; font-weight: bold;">public</span> @<span style="color: #000000; font-weight: bold;">interface</span> JndiLookup <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> jndi<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">Class</span> type<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Interceptor代码如下：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JndiLookupInterceptor <span style="color: #000000; font-weight: bold;">implements</span> Interceptor <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainitialcontext+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InitialContext</span></a> contaxt<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> destroy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contaxt <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainitialcontext+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InitialContext</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Anamingexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">NamingException</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> intercept<span style="color: #009900;">&#40;</span>ActionInvocation invocation<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amethod+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Method</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> methods <span style="color: #339933;">=</span> invocation.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMethods</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amethod+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Method</span></a> method <span style="color: #339933;">:</span> methods<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> parameterTypes<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> method.<span style="color: #006633;">getParameterTypes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>parameterTypes.<span style="color: #006633;">length</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JndiLookup jndiLookup<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> method.<span style="color: #006633;">getAnnotation</span><span style="color: #009900;">&#40;</span>JndiLookup.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>jndiLookup <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>jndiLookup.<span style="color: #006633;">jndi</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">||</span> jndiLookup.<span style="color: #006633;">jndi</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>jndiLookup.<span style="color: #006633;">type</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> object <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">contaxt</span>.<span style="color: #006633;">lookup</span><span style="color: #009900;">&#40;</span>jndiLookup.<span style="color: #006633;">jndi</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>invocation.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>jndiLookup.<span style="color: #006633;">type</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">cast</span><span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> invocation.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>需要被注入的Action代码如下：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeAction <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> DataSource ds<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @JndiLookup<span style="color: #009900;">&#40;</span>jndi<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;jdbc/mysqlDS&quot;</span>, type<span style="color: #339933;">=</span>DataSource.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDs<span style="color: #009900;">&#40;</span>DataSource ds<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">ds</span> <span style="color: #339933;">=</span> ds<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//do something about the ds</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>经测试，在tomcat下注入DataSource成功了。看来Annotation和Struts2真的是个好东西呀！</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/37/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>JNDI中的组件私有环境，公共环境和部署时映射</title>
		<link>http://www.sulong.info/archives/31</link>
		<comments>http://www.sulong.info/archives/31#comments</comments>
		<pubDate>Wed, 07 Nov 2007 06:32:48 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[jndi]]></category>
		<category><![CDATA[JPA]]></category>

		<guid isPermaLink="false">http://sulong.info/archives/31</guid>
		<description><![CDATA[j2ee难用，其中一个原因是jndi不好用，明明已经配置好名字为dataSource的数据源，但是在部署JPA时，老是抛异常说找不到数据源。今天在用jboss时，终于又明白了一些。 原来j2ee中的组件是有它自己独自的环境的，位于java:/comp/env这个jndi目录下。每个组件不能访问另外一个组件的私有环境，实现组件各自独立。同时，又有一些jndi目录和名字是公共的，如java:/下的其它一些名字，还有一些是只能给本地访问的，还有一些是能够给远程访问的，这个就不说了。 如果一个组件想要获得另外一个组件，那么需要在它的部署描述符里说明，比如: &#60;resource-ref&#62; &#160; &#60;description&#62;Dollys DataSource&#60;/description&#62; &#160; &#60;res-ref-name&#62;jdbc/mydatasource&#60;/res-ref-name&#62; &#160; &#60;res-ref-type&#62;javax.sql.DataSource&#60;/res-ref-type&#62; &#160; &#60;res-auth&#62;Container&#60;/res-auth&#62; &#60;/resource-ref&#62; 这里说这个组件需要依赖于一个叫java:/comp/env/jdbc/mydatasource的数据源。但是这个数据源又从哪里来呢？？！！注意了！原来是要在部署时写另外一个特定于服务器种类的数据源映射文件，如下面的jboss示例： &#60;resource-ref&#62; &#160; &#160;&#60;res-ref-name&#62;jdbc/mydatasource&#60;/res-ref-name&#62; &#160; &#160;&#60;jndi-name&#62;java:DefaultDS&#60;/jndi-name&#62; &#60;/resource-ref&#62; 上面这一段可能位于jboss-web.xml等jboss特定的部署描述符里。目的就是告诉jboss，把全局的java:DefaultDS映射到这个组件的java:/comp/env/jdbc/mydatasource。就是这一步，很时候之所以部署失败，找不到jndi都是因为少了这样一个映射导致的。 如果不想写上面的特定于服务器的部署描述符，我估计可以通过指定一个公共的jndi来解决，如下： &#60;resource-ref&#62; &#160; &#60;description&#62;Dollys DataSource&#60;/description&#62; &#160; &#60;res-ref-name&#62;java:DefaultDS&#60;/res-ref-name&#62; &#160; &#60;res-ref-type&#62;javax.sql.DataSource&#60;/res-ref-type&#62; &#160; &#60;res-auth&#62;Container&#60;/res-auth&#62; &#60;/resource-ref&#62; 问题虽然解决了，可是突然觉得好恶心，搞得这么复杂有必要吗？！！j2ee果然不是为了做小项目来的。]]></description>
			<content:encoded><![CDATA[<p>j2ee难用，其中一个原因是jndi不好用，明明已经配置好名字为dataSource的数据源，但是在部署JPA时，老是抛异常说找不到数据源。今天在用jboss时，终于又明白了一些。</p>
<p>原来j2ee中的组件是有它自己独自的环境的，位于java:/comp/env这个jndi目录下。每个组件不能访问另外一个组件的私有环境，实现组件各自独立。同时，又有一些jndi目录和名字是公共的，如java:/下的其它一些名字，还有一些是只能给本地访问的，还有一些是能够给远程访问的，这个就不说了。</p>
<p>如果一个组件想要获得另外一个组件，那么需要在它的部署描述符里说明，比如:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resource-ref<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Dollys DataSource<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;res-ref-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>jdbc/mydatasource<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/res-ref-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;res-ref-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.sql.DataSource<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/res-ref-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;res-auth<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Container<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/res-auth<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resource-ref<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>这里说这个组件需要依赖于一个叫java:/comp/env/jdbc/mydatasource的数据源。但是这个数据源又从哪里来呢？？！！注意了！原来是要在部署时写另外一个特定于服务器种类的数据源映射文件，如下面的jboss示例：</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resource-ref<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;res-ref-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>jdbc/mydatasource<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/res-ref-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jndi-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>java:DefaultDS<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jndi-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resource-ref<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>上面这一段可能位于jboss-web.xml等jboss特定的部署描述符里。目的就是告诉jboss，把全局的java:DefaultDS映射到这个组件的java:/comp/env/jdbc/mydatasource。就是这一步，很时候之所以部署失败，找不到jndi都是因为少了这样一个映射导致的。</p>
<p>如果不想写上面的特定于服务器的部署描述符，我估计可以通过指定一个公共的jndi来解决，如下：</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resource-ref<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Dollys DataSource<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;res-ref-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>java:DefaultDS<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/res-ref-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;res-ref-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.sql.DataSource<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/res-ref-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;res-auth<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Container<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/res-auth<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resource-ref<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>问题虽然解决了，可是突然觉得好恶心，搞得这么复杂有必要吗？！！j2ee果然不是为了做小项目来的。</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/31/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
