﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-asdtiang-java study-文章分类-grails 学习笔记</title><link>http://www.blogjava.net/asdtiang/category/44425.html</link><description>交流学习JAVA </description><language>zh-cn</language><lastBuildDate>Sat, 27 Mar 2010 09:23:11 GMT</lastBuildDate><pubDate>Sat, 27 Mar 2010 09:23:11 GMT</pubDate><ttl>60</ttl><item><title>Grails自定义配置文件读取</title><link>http://www.blogjava.net/asdtiang/articles/316692.html</link><dc:creator>asdtiang</dc:creator><author>asdtiang</author><pubDate>Sat, 27 Mar 2010 06:50:00 GMT</pubDate><guid>http://www.blogjava.net/asdtiang/articles/316692.html</guid><wfw:comment>http://www.blogjava.net/asdtiang/comments/316692.html</wfw:comment><comments>http://www.blogjava.net/asdtiang/articles/316692.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/asdtiang/comments/commentRss/316692.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/asdtiang/services/trackbacks/316692.html</trackback:ping><description><![CDATA[<div style="text-indent: 17.95pt;">标题为《Grails探
索之资源文件的读取》，但其实是Groovy语言的资源
文件的读取，任何使用Groovy语言的地方都可以使用这种方法读取资源文件。因为我们使用Groovy语言的地方，大多要体现在使用Grails上，而
且作为一个Grails项目，使用资源文件是必不可少的。因此，我还是把标题定为&#8220;Grails探索之资源文件的读取&#8221;。</div>
<div style="text-indent: 17.95pt;">首先，我们来看看Grails的
资源文件，下面的例子是Grails的数据源配置文件的一部分：</div>
<div style="text-indent: 17.95pt;">dataSource {</div>
<div style="text-indent: 17.95pt;">pooled = false</div>
<div style="text-indent: 17.95pt;">
&nbsp;&nbsp;&nbsp; driverClassName = "oracle.jdbc.driver.OracleDriver"
</div>
<div style="text-indent: 17.95pt;">
&nbsp;&nbsp;&nbsp; username = "test"
</div>
<div style="text-indent: 17.95pt;">
&nbsp;&nbsp;&nbsp; password = "test"
</div>
<div style="text-indent: 17.95pt;">
&nbsp;&nbsp;&nbsp;
</div>
<div style="text-indent: 17.95pt;">
&nbsp;&nbsp;&nbsp; dialect = org.hibernate.dialect.Oracle9Dialect
</div>
<div style="text-indent: 17.95pt;">}</div>
<div style="text-indent: 17.95pt;">&nbsp;</div>
<div style="text-indent: 17.95pt;">&nbsp;</div>
<div style="text-indent: 17.95pt;">它就是一种所谓的&#8220;DSL&#8221;编码，的确比我们常规的properties文件要
来的简单，如是properties文件的话，上面的代码就会变成如下的样子：</div>
<div style="text-indent: 17.95pt;">dataSource. pooled = false</div>
<div style="text-indent: 17.95pt;">dataSource. driverClassName =
"oracle.jdbc.driver.OracleDriver"</div>
<div style="text-indent: 17.95pt;">dataSource. username = "test"</div>
<div style="text-indent: 17.95pt;">dataSource. password = "test"</div>
<div style="text-indent: 17.95pt;">dataSource. dialect =
org.hibernate.dialect.Oracle9Dialect</div>
<div style="text-indent: 17.95pt;">&nbsp;</div>
<div style="text-indent: 17.95pt;">&nbsp;</div>
<div style="text-indent: 17.95pt;">对比上面的两个例子，可以看到，Groovy语言的DSL配置文件的确是要简单
多了。但是，我们自己要如何才能读取形如dataSource.groovy这样的配置文件呢？</div>
<div style="text-indent: 17.95pt;">答案是<span style="font-size: 10pt; color: black;">ConfigSlurper</span>
<span style="color: black;">类。是的，在</span>
<span style="color: black;">Groovy</span>
<span style="color: black;">语言中，我们都通过</span>
<span style="font-size: 10pt; color: black;">ConfigSlurper</span>
<span style="color: black;">类来操作配置文件，在</span>
<span style="color: black;">Grails</span>
<span style="color: black;">里也不例外。</span>
</div>
<div style="text-indent: 17.95pt;">
<span style="color: black;">下面来看一个简单的例子，假如我们有一个</span>
<span style="color: black;">test.groovy</span>
<span style="color: black;">的文件，内容如下：</span>
</div>
<div style="text-indent: 17.95pt;">
<strong><span style="font-size: 10pt; color: rgb(0, 153, 102);">test</span>
</strong>
</div>
<div style="text-indent: 17.95pt;">
<span style="font-size: 10pt; color: black;">{</span>
</div>
<div>
<span style="font-size: 10pt; color: black;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; name = </span>
<span style="font-size: 10pt; color: rgb(255, 0, 204);">'Tom'</span>
</div>
<div>
<span style="font-size: 10pt; color: black;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; age = </span>
<span style="font-size: 10pt; color: red;">33</span>
</div>
<div>
<span style="font-size: 10pt; color: black;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sex = </span>
<span style="font-size: 10pt; color: rgb(255, 0, 204);">'male'</span>
</div>
<div style="text-indent: 17.1pt;">
<span style="font-size: 10pt; color: black;">}</span>
</div>
<div style="text-indent: 17.1pt;">&nbsp;</div>
<div style="text-indent: 17.95pt;">&nbsp;</div>
<div style="text-indent: 17.95pt;">
<span style="color: black;">首先，我们需要读入&#8220;</span>
<span style="color: black;">test.groovy</span>
<span style="color: black;">&#8221;文件：</span>
</div>
<div style="text-indent: 17.15pt;">
<strong><span style="font-size: 10pt; color: rgb(0, 153, 102);">def</span>
</strong>
<span style="font-size: 10pt; color: black;"> config =</span>
<strong><span style="font-size: 10pt; color: rgb(0, 102, 153);">new</span>
</strong>
<span style="font-size: 10pt; color: black;"> ConfigSlurper().parse(</span>
<strong><span style="font-size: 10pt; color: rgb(0, 102, 153);">new</span>
</strong>
<span style="font-size: 10pt; color: black;"> File(</span>
<span style="font-size: 10pt; color: rgb(255, 0, 204);">"${System.properties['user.dir']}/configSlurper/test.groovy"</span>
<span style="font-size: 10pt; color: black;">).toURL())</span>
</div>
<div style="text-indent: 17.1pt;">&nbsp;</div>
<div style="text-indent: 17.95pt;">&nbsp;</div>
<div style="text-indent: 17.95pt;">注意，<span style="font-size: 10pt; color: rgb(255, 0, 204);">System.properties['user.dir']</span>
获取的是当前项目的更目录，其他就没什么好说的了。</div>
<div style="text-indent: 17.95pt;">然后来读取配置文件的内容：</div>
<div>
<span style="font-size: 10pt; color: black;">&nbsp;&nbsp;&nbsp; &nbsp;</span>
<strong><span style="font-size: 10pt; color: rgb(102, 204, 255);">println</span>
</strong>
<span style="font-size: 10pt; color: black;"> config.</span>
<strong><span style="font-size: 10pt; color: rgb(0, 153, 102);">test</span>
</strong>
<span style="font-size: 10pt; color: black;">.name</span>
</div>
<div>
<span style="font-size: 10pt; color: black;">&nbsp;&nbsp;&nbsp; &nbsp;</span>
</div>
<div>
<span style="font-size: 10pt; color: black;">&nbsp;&nbsp;&nbsp; &nbsp;</span>
<strong><span style="font-size: 10pt; color: rgb(102, 204, 255);">println</span>
</strong>
<span style="font-size: 10pt; color: black;"> config.</span>
<strong><span style="font-size: 10pt; color: rgb(0, 153, 102);">test</span>
</strong>
<span style="font-size: 10pt; color: black;">.age</span>
</div>
<div>
<span style="font-size: 10pt; color: black;">&nbsp;&nbsp;&nbsp; &nbsp;</span>
</div>
<div style="text-indent: 17.1pt;">
&nbsp;
<strong><span style="font-size: 10pt; color: rgb(102, 204, 255);">println</span>
</strong>
<span style="font-size: 10pt; color: black;"> config.</span>
<strong><span style="font-size: 10pt; color: rgb(0, 153, 102);">test</span>
</strong>
<span style="font-size: 10pt; color: black;">.sex</span>
</div>
<div style="text-indent: 17.1pt;">&nbsp;</div>
<div style="text-indent: 17.1pt;">&nbsp;</div>
<div style="text-indent: 17.95pt;">
<span style="color: black;">注意到，我们读取配置文件的内容采取的是一种类型如&#8220;</span>
<span style="color: black;">Xpath</span>
<span style="color: black;">&#8221;的方式，在</span>
<span style="color: black;">Groovy</span>
<span style="color: black;">语言里被成为&#8220;</span>
<span style="color: black;">Gpath</span>
<span style="color: black;">&#8221;。</span>
</div>
<div style="text-indent: 17.95pt;">
<span style="color: black;">即&#8220;</span>
<span style="color: black;">config</span>
<span style="color: black;">&#8221;代表的是配置文件，即根节点，然后下索到一级节点，即&#8220;</span>
<span style="color: black;">test</span>
<span style="color: black;">&#8221;，最后是二级节点，即&#8220;</span>
<span style="color: black;">name</span>
<span style="color: black;">&#8221;、&#8220;</span>
<span style="color: black;">age</span>
<span style="color: black;">&#8221;等。</span>
</div>
<div style="text-indent: 17.95pt;">
<span style="color: black;">上面的代码的运行结果为：</span>
</div>
<div style="text-indent: 17.95pt;">
<span style="font-size: 10pt; color: black;">Tom</span>
</div>
<div style="text-indent: 17.1pt;">
<span style="font-size: 10pt; color: black;">33</span>
</div>
<div style="text-indent: 17.1pt;">
<span style="font-size: 10pt; color: black;">Male</span>
</div>
<div style="text-indent: 17.1pt;">&nbsp;</div>
<div style="text-indent: 17.95pt;">&nbsp;</div>
<div style="text-indent: 17.95pt;">果然把上面的配置文件读取出来，如此类推，我们可以用这样的方法来做一些比较复
杂的配置文件。在这里就不再说明了。</div>
<div style="text-indent: 17.95pt;">在Grails项
目中，我们有一个统一的放置配置文件的地方，即&#8220;../grails-app/conf&#8221;，
它是在项目的&#8220;grails-app/conf&#8221;目录里。我们可以在该目录下再建一个
&#8220;user&#8221;目录，供与项目业务相关的配置文件使用。</div>
<div style="text-indent: 17.95pt;">比如，在&#8220;grails-app/conf
/user&#8221;目录里有一个测试配置文件&#8220;Test.groovy&#8221;，内容如下：</div>
<div style="text-indent: 17.95pt;">
<strong><span style="font-size: 10pt; color: rgb(0, 102, 153);">package</span>
</strong>
<span style="font-size: 10pt; color: black;"> user;</span>
</div>
<div>
<span style="font-size: 10pt;">&nbsp;&nbsp;&nbsp; </span>
</div>
<div>
<span style="font-size: 10pt;">&nbsp;&nbsp;&nbsp; </span>
</div>
<div style="text-indent: 17.95pt;">
<strong><span style="font-size: 10pt; color: rgb(0, 153, 102);">test</span>
</strong>
<span style="font-size: 10pt; color: black;">{</span>
</div>
<div>
<span style="font-size: 10pt; color: black;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; name = </span>
<span style="font-size: 10pt; color: rgb(255, 0, 204);">'Tom'</span>
</div>
<div>
<span style="font-size: 10pt; color: black;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; age = </span>
<span style="font-size: 10pt; color: rgb(255, 0, 204);">'30'</span>
</div>
<div style="text-indent: 17.1pt;">
<span style="font-size: 10pt; color: black;">}</span>
</div>
<div style="text-indent: 17.1pt;">&nbsp;</div>
<div style="text-indent: 17.95pt;">
<span style="color: black;">然后，我们就可以使用如下代码对它进行读取了：</span>
</div>
<div>
<strong><span style="font-size: 10pt; color: rgb(0, 153, 102);">def</span>
</strong>
<span style="font-size: 10pt; color: black;"> config = </span>
<strong><span style="font-size: 10pt; color: rgb(0, 102, 153);">new</span>
</strong>
<span style="font-size: 10pt; color: black;"> ConfigSlurper().parse(</span>
<strong><span style="font-size: 10pt; color: rgb(0, 102, 153);">new</span>
</strong>
<span style="font-size: 10pt; color: black;"> File(</span>
</div>
<div style="text-indent: 17.1pt;">
<span style="font-size: 10pt; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;</span>
<span style="font-size: 10pt; color: rgb(255, 0, 204);">"${System.properties['user.dir']}/grails-app/conf/user/Test.groovy"</span>
<span style="font-size: 10pt; color: black;">).toURL())</span>
</div>
<img src ="http://www.blogjava.net/asdtiang/aggbug/316692.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/asdtiang/" target="_blank">asdtiang</a> 2010-03-27 14:50 <a href="http://www.blogjava.net/asdtiang/articles/316692.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>