﻿<?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-kitten-随笔分类-spring</title><link>http://www.blogjava.net/kitten-tiger/category/50582.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 25 Aug 2014 14:26:56 GMT</lastBuildDate><pubDate>Mon, 25 Aug 2014 14:26:56 GMT</pubDate><ttl>60</ttl><item><title>org.springframework.util.Assert</title><link>http://www.blogjava.net/kitten-tiger/archive/2014/08/25/417298.html</link><dc:creator>云③+_+输</dc:creator><author>云③+_+输</author><pubDate>Mon, 25 Aug 2014 02:53:00 GMT</pubDate><guid>http://www.blogjava.net/kitten-tiger/archive/2014/08/25/417298.html</guid><wfw:comment>http://www.blogjava.net/kitten-tiger/comments/417298.html</wfw:comment><comments>http://www.blogjava.net/kitten-tiger/archive/2014/08/25/417298.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kitten-tiger/comments/commentRss/417298.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kitten-tiger/services/trackbacks/417298.html</trackback:ping><description><![CDATA[<h2><p style="margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding: 6px 0px; border: 0px; outline: 0px; vertical-align: baseline; font-family: Arial, sans-serif; color: #222222; line-height: 1.5em; font-weight: normal; background-color: #ffffff; font-size: 1.166em !important;"><span style="font-size: 10pt;">Web 应用在接受表单提交的数据后都需要对其进行合法性检查，如果表单数据不合法，请求将被驳回。类似的，当我们在编写类的方法时，也常常需要对方法入参进行合法性检查，如果入参不符合要求，方法将通过抛出异常的方式拒绝后续处理。举一个例子：有一个根据文件名获取输入流的方法：InputStream getData(String file)，为了使方法能够成功执行，必须保证 file 入参不能为 null 或空白字符，否则根本无须进行后继的处理。这时方法的编写者通常会在方法体的最前面编写一段对入参进行检测的代码，如下所示：</span></p><div style="margin: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Arial, sans-serif; font-weight: normal; line-height: 15.360000610351563px; background-color: #ffffff;"><pre style="margin-top: 0px; border: 1px solid #cccccc; outline: 0px; font-size: 11px; vertical-align: baseline; width: 780px; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; overflow: auto; clear: right; margin-bottom: 6px !important; padding: 5px 10px 5px 3px !important; background-color: #f7f7f7 !important;"><span style="font-size: 10pt;">public InputStream getData(String file) {     if (file == null || file.length() == 0|| file.replaceAll("\\s", "").length() == 0) {         throw new IllegalArgumentException("file入参不是有效的文件地址");     } &#8230; }</span></pre></div><p style="margin: 0px; padding: 6px 0px; border: 0px; outline: 0px; vertical-align: baseline; font-family: Arial, sans-serif; color: #222222; line-height: 1.5em; font-weight: normal; background-color: #ffffff; font-size: 1.166em !important;"><span style="font-size: 10pt;">类似以上检测方法入参的代码是非常常见，但是在每个方法中都使用手工编写检测逻辑的方式并不是一个好主意。阅读 Spring 源码，您会发现 Spring 采用一个 org.springframework.util.Assert 通用类完成这一任务。</span></p><p style="margin: 0px; padding: 6px 0px; border: 0px; outline: 0px; vertical-align: baseline; font-family: Arial, sans-serif; color: #222222; line-height: 1.5em; font-weight: normal; background-color: #ffffff; font-size: 1.166em !important;"><span style="font-size: 10pt;">Assert 翻译为中文为&#8220;断言&#8221;，使用过 JUnit 的读者都熟知这个概念，它断定某一个实际的运行值和预期想一样，否则就抛出异常。Spring 对方法入参的检测借用了这个概念，其提供的 Assert 类拥有众多按规则对方法入参进行断言的方法，可以满足大部分方法入参检测的要求。这些断言方法在入参不满足要求时就会抛出 IllegalArgumentException。</span></p></h2><span style="font-size: 10pt;">&nbsp;转自：</span><span style="font-size: 10pt;">http://www.ibm.com/developerworks/cn/java/j-lo-spring-utils2/</span><img src ="http://www.blogjava.net/kitten-tiger/aggbug/417298.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kitten-tiger/" target="_blank">云③+_+输</a> 2014-08-25 10:53 <a href="http://www.blogjava.net/kitten-tiger/archive/2014/08/25/417298.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>