<?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>-Flyぁ梦- &#187; Spring</title>
	<atom:link href="http://blog.11034.org/tag/spring/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.11034.org</link>
	<description></description>
	<lastBuildDate>Sun, 22 Jun 2025 08:59:05 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>Spring-MVC中的一些问题</title>
		<link>http://blog.11034.org/2016-03/spring_mvc_applicationcontext.html</link>
		<comments>http://blog.11034.org/2016-03/spring_mvc_applicationcontext.html#comments</comments>
		<pubDate>Tue, 22 Mar 2016 13:52:10 +0000</pubDate>
		<dc:creator><![CDATA[-Flyぁ梦-]]></dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://blog.11034.org/?p=2805</guid>
		<description><![CDATA[想法：Spring-mvc（Spring v3.2）中，仅对注册过（通过@RequestMapping注解）的 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>想法：Spring-mvc（Spring v3.2）中，仅对注册过（通过<code class="markdown_inline_code">@RequestMapping</code>注解）的url进入到controller层，其他非法url在Filter里就拦截掉，统一处理节省资源。因为在filter的doChain之前，给本次请求从对象池中获取一个操作db的对象，对无效url也分配的话就浪费了资源。</p>
<p>实现：通过Spring-mvc的<code class="markdown_inline_code">RequestMappingHandlerMapping</code>来获取注册过的url path，再根据HttpServletRequest，判断下就可以进行过滤。</p>
<p><span id="more-2805"></span></p>
<h2>自己实现</h2>
<p>一开始，自己新建一个类用一个HashMap去保存所有的url path即可，在controller中的static域或者构造方法里，添加本controller中的所有url path即可。</p>
<p><strong>问题一：spring日志显示已经初始化完毕绑定了url了，但是并没有触发controller中的static域或者构造方法！</strong></p>
<p>不得不使用Class.forName()去强制触发static域。</p>
<p>原因：spring-servlet.xml中&lt;beans&gt;根标签加了default-lazy-init=&#8221;true&#8221;，所有controller在有真正http请求到来才会被访问到！</p>
<p><strong>问题二：spring日志显示明明有url记录，为啥要再自己干？</strong></p>
<p>日志显示一个叫RequestMappingHandlerMapping的东西显然保存了url注册，拿到这个bean的实例应该就大功告成。</p>
<h2>通过RequestMappingHandlerMapping</h2>
<p><strong>问题三：在Filter里用WebApplicationContextUtils.getWebApplicationContext(ServletContext)的方法拿到ApplicationContext后，但是拿不到bean</strong></p>
<p>这个ApplicationContext里面根本没有任何bean！</p>
<p>原因：这个ApplicationContext是<code class="markdown_inline_code">针对applicationContext.xml建出来的Root ApplicationContext</code>，而<code class="markdown_inline_code">Spring MVC根据spring-servlet.xml建立的是一个Child ApplicationContext</code></p>
<p><strong>问题四：在Filter里无法获取到spring-servlet.xml建立的Child ApplicationContext</strong></p>
<p>因为通过Root ApplicationContext拿不到Children，也没有任何API可以直接获取Child ApplicationContext。</p>
<p>原因：Filter是直接注册在web.xml中的，并不归Spring管理，Spring MVC根据spring-servlet.xml建立ApplicationContext只能注入到同一个Spring容器中的bean，比如所有controller中。</p>
<p>于是新建一个类交给Spring管理（在spring-servlet.xml中定义bean），然后<code class="markdown_inline_code">implements ApplicationContextAware</code>，就可以获得Spring MVC的ApplicationContext了，自然就拿到了RequestMappingHandlerMapping的bean，将其保存下来，通过判断<code class="markdown_inline_code">requestMappingHandlerMapping.getHandler(request) == null</code>即可知道是否为有效url path。</p>
<h4  class="related_post_title">看看 Spring</h4><ul class="related_post"><li>2013-08-10 -- <a target="_blank" href="http://blog.11034.org/2013-08/some_skills_in_java.html" title="学到的一些东西">学到的一些东西</a></li><li>2012-05-13 -- <a target="_blank" href="http://blog.11034.org/2012-05/java_play_framework.html" title="Java Play framework">Java Play framework</a></li><li>2011-03-29 -- <a target="_blank" href="http://blog.11034.org/2011-03/java_encoding.html" title="Java编码的那些事儿">Java编码的那些事儿</a></li><li>2011-03-15 -- <a target="_blank" href="http://blog.11034.org/2011-03/spring_hibernate_annotation.html" title="SpringMVC+Hibernate的一些配置">SpringMVC+Hibernate的一些配置</a></li></ul><h4 class="related_post_title">看看 Java </h4><ul class="related_post"><li>2016-09-09 -- <a target="_blank" href="http://blog.11034.org/2016-09/64bits_linux_arena_memory.html" title="64位Linux下Java进程堆外内存迷之64M问题">64位Linux下Java进程堆外内存迷之64M问题</a></li><li>2016-08-18 -- <a target="_blank" href="http://blog.11034.org/2016-08/java_concurrency_in_practice.html" title="读java concurrency in practice">读java concurrency in practice</a></li><li>2016-08-05 -- <a target="_blank" href="http://blog.11034.org/2016-08/thread_stop.html" title="线程清理">线程清理</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/futuretask.html" title="FutureTask简单分析和用法">FutureTask简单分析和用法</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/semaphore.html" title="Semaphore简单分析">Semaphore简单分析</a></li>]]></content:encoded>
			<wfw:commentRss>http://blog.11034.org/2016-03/spring_mvc_applicationcontext.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>学到的一些东西</title>
		<link>http://blog.11034.org/2013-08/some_skills_in_java.html</link>
		<comments>http://blog.11034.org/2013-08/some_skills_in_java.html#comments</comments>
		<pubDate>Sat, 10 Aug 2013 03:11:52 +0000</pubDate>
		<dc:creator><![CDATA[-Flyぁ梦-]]></dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://blog.11034.org/?p=1895</guid>
		<description><![CDATA[暑假在天猫实习，从头开始参与一个项目。项目虽小，五脏俱全，虽然自己实现的是一个十分简单如果完全用自己的环境和习 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>暑假在天猫实习，从头开始参与一个项目。项目虽小，五脏俱全，虽然自己实现的是一个十分简单如果完全用自己的环境和习惯写代码不用一天就能完成的任务，结果就是因为搭上了淘宝整个系统和环境，变得十分复杂，而且还要和其他team约定接口联调测试，还要和测试、DBA、前台、PD（产品经理）等打交道。</p>
<p>相比本科的百度上海那时候的实习经历，最大的区别就是那时候完全就用一套开源的简单的东西，能多简单能跑起来就好，几乎没有和百度已有的平台去接通（除了一块不是我负责的登陆系统），打交道的主要就是一位前端，再后来就是一个测试，没有接触太多的人和其他的技术。</p>
<p><span id="more-1895"></span></p>
<p>下面是项目里碰到几个自己当时解决不了，在景升和楚帅的帮忙下（耗费较长时间debug啊）给解决的，挺有意思的。</p>
<h2>Spring加载properties配置文件</h2>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context:property-placeholder</span> <span style="color: #000066;">location</span>=<span style="color: #ff0000;">&quot;/WEB-INF/jdbc.properties&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;locations&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>classpath:jdbc.properties<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p><context:property-placeholder />标签和PropertyPlaceholderConfigurer这个Bean的实现是一个原理，所以如果同时出现在<Beans>标签中，之后出现的那个会被忽略（Bean已存在），导致后一个的配置文件加载失败。所以应该推荐使用PropertyPlaceholderConfigurer的方式加载多个配置文件，并且将这个Bean放在最顶层的Beans配置文件中，供所以开发者使用。</p>
<h2>特殊化/项目化的配置文件名</h2>
<p>由于习惯，Spring的配置文件applicationContext.xml、ibatis的配置文件sql-map.xml等随处可见，当项目庞大之后，很难保证引入的第三方jar包内没有这些同名文件，弄不好因为classpath加载顺序在加载&#8221;classpath:sql-map.xml&#8221;时加载了错误的xml文件而百思不得其解。</p>
<p>反正这些配置文件再加载时，都在可以自定义文件名的，所以给一个namespace是比较好的习惯，比如appname_sql-map.xml。</p>
<h2>追寻jar包</h2>
<p>Maven只能帮助构建项目依赖，具体运行时，还是依赖classpath，服务器和本地机器环境不可能完全相同，越是复杂的环境越容易加载错jar包，导致版本不一致，而导致错误。<br />
可以通过<strong>class.getProtectionDomain().getCodeSource().getLocation()</strong>获得jar包的URL对象，进行日志打印就可以知道此class所属的jar包的具体路径信息，以此判断运行状况是否符合期望。</p>
<h2>Eclipse远程断点调试</h2>
<p>只要服务器和本地的代码完全一致，在Debug模式下的Open Debug Dialog，左侧菜单最后一个Remote Java Application，填写了项目、IP和端口后，设了断点就可以按右下角Debug进行断点。不过对服务器进行断点容易造成服务器崩溃&#8230;小心使用。</p>
<h4  class="related_post_title">看看 Spring</h4><ul class="related_post"><li>2016-03-22 -- <a target="_blank" href="http://blog.11034.org/2016-03/spring_mvc_applicationcontext.html" title="Spring-MVC中的一些问题">Spring-MVC中的一些问题</a></li><li>2012-05-13 -- <a target="_blank" href="http://blog.11034.org/2012-05/java_play_framework.html" title="Java Play framework">Java Play framework</a></li><li>2011-03-29 -- <a target="_blank" href="http://blog.11034.org/2011-03/java_encoding.html" title="Java编码的那些事儿">Java编码的那些事儿</a></li><li>2011-03-15 -- <a target="_blank" href="http://blog.11034.org/2011-03/spring_hibernate_annotation.html" title="SpringMVC+Hibernate的一些配置">SpringMVC+Hibernate的一些配置</a></li></ul><h4 class="related_post_title">看看 Java </h4><ul class="related_post"><li>2016-09-09 -- <a target="_blank" href="http://blog.11034.org/2016-09/64bits_linux_arena_memory.html" title="64位Linux下Java进程堆外内存迷之64M问题">64位Linux下Java进程堆外内存迷之64M问题</a></li><li>2016-08-18 -- <a target="_blank" href="http://blog.11034.org/2016-08/java_concurrency_in_practice.html" title="读java concurrency in practice">读java concurrency in practice</a></li><li>2016-08-05 -- <a target="_blank" href="http://blog.11034.org/2016-08/thread_stop.html" title="线程清理">线程清理</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/futuretask.html" title="FutureTask简单分析和用法">FutureTask简单分析和用法</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/semaphore.html" title="Semaphore简单分析">Semaphore简单分析</a></li>]]></content:encoded>
			<wfw:commentRss>http://blog.11034.org/2013-08/some_skills_in_java.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Java Play framework</title>
		<link>http://blog.11034.org/2012-05/java_play_framework.html</link>
		<comments>http://blog.11034.org/2012-05/java_play_framework.html#comments</comments>
		<pubDate>Sun, 13 May 2012 05:19:29 +0000</pubDate>
		<dc:creator><![CDATA[-Flyぁ梦-]]></dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Play]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[框架]]></category>

		<guid isPermaLink="false">http://blog.stariy.org/?p=1150</guid>
		<description><![CDATA[因本科几个老同学的邀请参与一个项目的开发，用到了这个全新的Java框架，“Play”。名字很有意思，接触的1. [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>因本科几个老同学的邀请参与一个项目的开发，用到了这个全新的Java框架，“Play”。名字很有意思，接触的1.2.4版本而不是最近的2.o，上手很快，安装包里有自带的Sample程序。框架的易用程度大大超过了自己的预期，果然随着时代的发展Java框架越来越强大并且越来越人性化，最大程度地方便程序员开发应用。</p>
<p>一直使用也看好Spring MVC，但是Spring的日益臃肿让这个框架的优势在不断流失。列举Play一些非常棒的地方，特别是较之Spring MVC的优势。</p>
<p><span id="more-1150"></span></p>
<ul>
<li>Play最大的一个特点就是开发期间每次修改Java代码无需重启Java Web服务器，瞬间感觉和写PHP程序一样方便！这个真的是太爽，之前写Java Web程序调试，每次关闭再重启Tomcat少说也得5秒钟吧。</li>
<li>配置文件齐全，基本上帮你考虑到了所有可能要配置的内容（配置、持久、缓存、日志、国际化等）</li>
<li>URL route很清楚很简单，而且完全自定义URL Pattern（SpringMVC的Route还是不算太好，不能定义/model/action这样的url）</li>
<li>一个Java类作为一个Controller可以每个方法都对应一个HTTP请求的方式，大量减少了类的数量</li>
<li>Controller的方法和Template文件对应很好，默认模式下自动对应，十分方便</li>
<li>根据Action方法的参数类型和名称自动从提交参数中自动封装，包括对JavaBean的封装，对大量属性的表单特别有优势</li>
<li>对传入参数的验证很容易而且基本不用再自己写代码，全部在Model类里用Annotation搞定</li>
<li>Action方法可方便返回Html、Text、Json、Xml等数据，各种方便的API</li>
<li>Model层真的简略到基本不用写任何代码了，利用Hibernate自动建表、插入和更新自不用说，因为所有Model从特定类继承而来，丰富的API基本够平常所需，而且查询接口十分简单和强大。再也不用像SpringMVC那样每一个Model都要建一个Dao，每一个查询都要写一个十分复杂的Hibernate方法。</li>
</ul>
<p>Play框架已经不仅仅是个框架的概念了，而是一个开发环境了，因为要利用它帮你生成Project，eclipsify进入开发模式，run进入debug模式，war进入发布模式，而且Play自定义了一套Project下的目录结构和文件名称，这些一经修改很有可能运行就会出错。这是一把双刃剑，虽然方便了开发者和初学者，但是对于比较喜欢自己完全掌控代码和框架运作的开发人员来说并不是一件好的事情。</p>
<p>当然，也感觉Play有些还不太习惯的地方，较之其他框架另类之处，还有一些有待改善的地方。</p>
<ul>
<li>包结构名默认被限定，不符合大众模式（域名倒置的形式）</li>
<li>一个Controller不能自定义对应的Template的目录名，而默认采用与Controller名一致的目录名（强烈建议应该在Controller上加一个Annotation来自定义Template的目录名）</li>
<li>View模式下的模板语法（Groovy）据说效率比较低</li>
<li>开发模式下，因为靠Play自带程序运行，脱离IDE环境，较难实现单步跟踪</li>
<li>相比Spring，加入其它第三方组件不太方便，比如更换模板引擎等</li>
</ul>
<div>总的来说，Play让人耳目一新的感觉，第一感觉是非常棒的，真的大大加快了用Java开发Web的速度和效率，而且Play作为框架的理念也理解的很好很懂得开发人员的心思来尽量满足需求，当然个人觉得这甚至有点过了因为什么都帮你干完了开发人员的价值很多就体现不出来了。作为一个demo或者小型应用，Play绝对适合，但是要开发一个企业级大项目，就至今对Play的了解来看，也许Spring这一套路更稳重一些。</div>
<h4  class="related_post_title">看看 Play , Spring , 框架</h4><ul class="related_post"><li>2012-05-15 -- <a target="_blank" href="http://blog.11034.org/2012-05/param_binding_in_play_framework.html" title="Play框架中Action的参数绑定和验证">Play框架中Action的参数绑定和验证</a></li><li>2012-05-15 -- <a target="_blank" href="http://blog.11034.org/2012-05/router_in_play_framework.html" title="Play框架的Router机制和jregex包">Play框架的Router机制和jregex包</a></li><li>2016-03-22 -- <a target="_blank" href="http://blog.11034.org/2016-03/spring_mvc_applicationcontext.html" title="Spring-MVC中的一些问题">Spring-MVC中的一些问题</a></li><li>2013-08-10 -- <a target="_blank" href="http://blog.11034.org/2013-08/some_skills_in_java.html" title="学到的一些东西">学到的一些东西</a></li><li>2011-03-29 -- <a target="_blank" href="http://blog.11034.org/2011-03/java_encoding.html" title="Java编码的那些事儿">Java编码的那些事儿</a></li></ul><h4 class="related_post_title">看看 Java </h4><ul class="related_post"><li>2016-09-09 -- <a target="_blank" href="http://blog.11034.org/2016-09/64bits_linux_arena_memory.html" title="64位Linux下Java进程堆外内存迷之64M问题">64位Linux下Java进程堆外内存迷之64M问题</a></li><li>2016-08-18 -- <a target="_blank" href="http://blog.11034.org/2016-08/java_concurrency_in_practice.html" title="读java concurrency in practice">读java concurrency in practice</a></li><li>2016-08-05 -- <a target="_blank" href="http://blog.11034.org/2016-08/thread_stop.html" title="线程清理">线程清理</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/futuretask.html" title="FutureTask简单分析和用法">FutureTask简单分析和用法</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/semaphore.html" title="Semaphore简单分析">Semaphore简单分析</a></li>]]></content:encoded>
			<wfw:commentRss>http://blog.11034.org/2012-05/java_play_framework.html/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Java编码的那些事儿</title>
		<link>http://blog.11034.org/2011-03/java_encoding.html</link>
		<comments>http://blog.11034.org/2011-03/java_encoding.html#comments</comments>
		<pubDate>Tue, 29 Mar 2011 13:15:20 +0000</pubDate>
		<dc:creator><![CDATA[-Flyぁ梦-]]></dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[编码]]></category>

		<guid isPermaLink="false">http://blog.stariy.org/?p=824</guid>
		<description><![CDATA[来上海百度实习也才没多久，这是第三个礼拜吧，接手的项目相当紧迫，而且作为一个实习生我是main coder，不 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>来上海百度实习也才没多久，这是第三个礼拜吧，接手的项目相当紧迫，而且作为一个实习生我是main coder，不由地带动了极大的工作积极性。这个一开始认为很简单很明了的“小”项目，没想到让我学到了很多东西，今天就讲讲关于Java编码的那些事儿。</p>
<p><span id="more-824"></span></p>
<h3>Java编码</h3>
<p>Unicode是全球标准字符集，是Java所为String采用的编码方式，任何字符用2个字节表示。String实例中保存有一个char[]字符数组。string.getByte()方法可以获得到这个字符串实例在指定编码下的字节数组，<strong>注意</strong>的是不带参数的getByte方法使用OS默认的字符集，比如GB2312（简体中文）。所以要得到Unicode下的字节数组，需要这样：string.getBytes(&#8220;unicode&#8221;)（<strong>此处注意见下文</strong>）。如果使用new String(byte[], Charset)构造，可以将已知编码的字节数组重新拼成一个String实例，即用指定的Charset去组合字节为Unicode字符罢了。同理，不带Charset的String构造使用OS默认字符集。</p>
<p>因此，得到UTF-8的字节数组，按以下步骤：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> str <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;梦&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> bytes <span style="color: #339933;">=</span> str.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #0000ff;">&quot;使用UTF-8解码字符串得到的UTF-8字节数组&quot;</span>
<span style="color: #003399;">String</span> str2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span>bytes, <span style="color: #0000ff;">&quot;utf8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">&quot;按照当初被解码的方式（utf8）重新组成Java String类&quot;</span>
str.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>str2<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>       <span style="color: #0000ff;">&quot;使用大小写不同的编码写法，来区别不同API中参数代表的意义&quot;</span></pre></td></tr></table></div>

<p>所以，str = new String(str.getBytes(Charset), Charset) 什么都没有做，除了新建了个String对象。</p>
<p>但是如果你要获取unicode的字节数组，却有非常多的选择，而且很容易出错。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">void</span> echoBytesTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">String</span> s <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;梦&quot;</span><span style="color: #339933;">;</span>
	echoBytes<span style="color: #009900;">&#40;</span>s, <span style="color: #0000ff;">&quot;Unicode&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	echoBytes<span style="color: #009900;">&#40;</span>s, <span style="color: #0000ff;">&quot;UnicodeBig&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	echoBytes<span style="color: #009900;">&#40;</span>s, <span style="color: #0000ff;">&quot;UnicodeLittle&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	echoBytes<span style="color: #009900;">&#40;</span>s, <span style="color: #0000ff;">&quot;UnicodeBigUnmarked&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	echoBytes<span style="color: #009900;">&#40;</span>s, <span style="color: #0000ff;">&quot;UnicodeLittleUnmarked&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	echoBytes<span style="color: #009900;">&#40;</span>s, <span style="color: #0000ff;">&quot;UTF-16&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	echoBytes<span style="color: #009900;">&#40;</span>s, <span style="color: #0000ff;">&quot;UTF-16BE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	echoBytes<span style="color: #009900;">&#40;</span>s, <span style="color: #0000ff;">&quot;UTF-16LE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	echoBytes<span style="color: #009900;">&#40;</span>s, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> echoBytes<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> s, <span style="color: #003399;">String</span> encoding<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> bytes <span style="color: #339933;">=</span> s.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span>encoding<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span> b <span style="color: #339933;">:</span> bytes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> b <span style="color: #339933;">&amp;</span> 0xff<span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>.<span style="color: #006633;">toHexString</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>JDK6.0环境下的输出结果如下所示：</p>
<table>
<tbody>
<tr>
<td>Unicode</td>
<td><font color="red">fe ff</font> 68 a6</td>
<td>BE顺序，带BOM</td>
<td>强烈申明不要使用，<strong>JDK相关</strong></td>
</tr>
<tr>
<td>UnicodeBig</td>
<td><font color="red">fe ff</font> 68 a6</td>
<td>BE顺序，带BOM</td>
<td><font color="blue">推荐使用</font></td>
</tr>
<tr>
<td>UnicodeLittle</td>
<td><font color="red">ff fe</font> a6 68</td>
<td>LE顺序，带BOM</td>
<td><font color="blue">推荐使用</font></td>
</tr>
<tr>
<td>UnicodeBigUnmarked</td>
<td>68 a6</td>
<td>BE顺序，无BOM</td>
<td><font color="blue">推荐使用</font></td>
</tr>
<tr>
<td>UnicodeLittleUnmarked</td>
<td>a6 68</td>
<td>LE顺序，无BOM</td>
<td><font color="blue">推荐使用</font></td>
</tr>
<tr>
<td>UTF-16</td>
<td><font color="red">fe ff</font> 68 a6</td>
<td>BE顺序，带BOM</td>
<td>不推荐使用，可读性不够高</td>
</tr>
<tr>
<td>UTF-16BE</td>
<td>68 a6</td>
<td>BE顺序，无BOM</td>
<td>不推荐使用，可读性不够高</td>
</tr>
<tr>
<td>UTF-16LE</td>
<td>a6 68</td>
<td>LE顺序，无BOM</td>
<td>不推荐使用，可读性不够高</td>
</tr>
<tr>
<td>UTF-8</td>
<td>e6 a2 a6</td>
<td>无BOM</td>
<td>此为UTF-8字节串，特以此区别</td>
</tr>
</tbody>
</table>
<p><strong>然后要特别说明的是，getBytes(&#8220;Unicode&#8221;)这种方式是什么样的结果，却是与JDK相关的</strong>，JDK5.0使用LE顺序，JDK6.0使用BE顺序！</p>
<p>有点复杂，看官方解释。</p>
<blockquote><p>
为了在读取字节时能知道所采用的字节序，在传输时采用了一个名为 “Zero Width No-Break Space” 的字符（中文译名作“零宽无间断间隔”）用于限定字节序，开头两个字节为 FE FF（即-2 -1） 时为 Big-Endian，为 FF FE（即-1 -2） 时为 Little-Endian。 详见 RFC2781 3.2 节。这个就是传说中的BOM头。<br />
UTF-8不需要 BOM 来表明字节顺序，但可以用 BOM 来表明编码方式。字符 &#8220;Zero Width No-Break Space&#8221; 的 UTF-8 编码是 EF BB BF。<br />
详见：<a href="http://baike.baidu.com/view/126558.htm#sub5073178" target="_blank">BOM_百度百科</a>
</p></blockquote>
<p>自我理解很像体系结构中的大端编址和小端编址对吧。不知道Unicode为什么要存在Big-Endian和Little-Endian两种顺序，反正就是存在着呗。</p>
<h3>说说项目</h3>
<p>就是因为string.getBytes(&#8220;Unicode&#8221;)的JDK相关性，然后我因为使用了旧版本的JDK导致和大家同样从SVN签出的代码的运行结果却完全不一致，由于完全信任Java的平台无关，没有一点怀疑是JDK源码的原因，整整花了一天的时候排查bug。</p>
<p>第二个是Spring MVC的@ResponseBody默认返回的字符集是ISO-8859-1的西文字符，导致返回客户端为乱码，修改Spring配置的见<a href="http://forum.springsource.org/showthread.php?t=81858" target="_blank">Spring官方论坛的解决方案</a>，不过貌似试了几种方法都没有起效&#8230;其实最简单的方式就是放弃使用Spring提供的Ajax方式，直接使用HttpServletResponse的getWriter().write()方法写字符串或getOutputStream.write()方法直接写字节。</p>
<h3>参考引用</h3>
<p>百度百科：<a href="http://baike.baidu.com/view/126558.htm#sub5073178">http://baike.baidu.com/view/126558.htm#sub5073178</a></p>
<p>CSDN论坛：<a href="http://topic.csdn.net/u/20081009/09/e899898c-591f-4985-ae88-5972475708fb.html" target="_blank">http://topic.csdn.net/u/20081009/09/e899898c-591f-4985-ae88-5972475708fb.html</a></p>
<p>Spring官方论坛：<a href="http://forum.springsource.org/showthread.php?t=81858">http://forum.springsource.org/showthread.php?t=81858</a></p>
<h4  class="related_post_title">看看 Ajax , Spring , 编码</h4><ul class="related_post"><li>2016-03-22 -- <a target="_blank" href="http://blog.11034.org/2016-03/spring_mvc_applicationcontext.html" title="Spring-MVC中的一些问题">Spring-MVC中的一些问题</a></li><li>2014-06-17 -- <a target="_blank" href="http://blog.11034.org/2014-06/onchange_of_input.html" title="小心使用input的onchange事件">小心使用input的onchange事件</a></li><li>2013-08-10 -- <a target="_blank" href="http://blog.11034.org/2013-08/some_skills_in_java.html" title="学到的一些东西">学到的一些东西</a></li><li>2012-05-13 -- <a target="_blank" href="http://blog.11034.org/2012-05/java_play_framework.html" title="Java Play framework">Java Play framework</a></li><li>2011-03-15 -- <a target="_blank" href="http://blog.11034.org/2011-03/spring_hibernate_annotation.html" title="SpringMVC+Hibernate的一些配置">SpringMVC+Hibernate的一些配置</a></li></ul><h4 class="related_post_title">看看 Java </h4><ul class="related_post"><li>2016-09-09 -- <a target="_blank" href="http://blog.11034.org/2016-09/64bits_linux_arena_memory.html" title="64位Linux下Java进程堆外内存迷之64M问题">64位Linux下Java进程堆外内存迷之64M问题</a></li><li>2016-08-18 -- <a target="_blank" href="http://blog.11034.org/2016-08/java_concurrency_in_practice.html" title="读java concurrency in practice">读java concurrency in practice</a></li><li>2016-08-05 -- <a target="_blank" href="http://blog.11034.org/2016-08/thread_stop.html" title="线程清理">线程清理</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/futuretask.html" title="FutureTask简单分析和用法">FutureTask简单分析和用法</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/semaphore.html" title="Semaphore简单分析">Semaphore简单分析</a></li>]]></content:encoded>
			<wfw:commentRss>http://blog.11034.org/2011-03/java_encoding.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>SpringMVC+Hibernate的一些配置</title>
		<link>http://blog.11034.org/2011-03/spring_hibernate_annotation.html</link>
		<comments>http://blog.11034.org/2011-03/spring_hibernate_annotation.html#comments</comments>
		<pubDate>Tue, 15 Mar 2011 15:53:54 +0000</pubDate>
		<dc:creator><![CDATA[-Flyぁ梦-]]></dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://blog.stariy.org/?p=815</guid>
		<description><![CDATA[近期项目又用到了Spring MVC+Hibernate的框架结构和全Annotation（注解）的配置，自然 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>近期项目又用到了Spring MVC+Hibernate的框架结构和全Annotation（注解）的配置，自然是再熟悉不过的了，从Trilogy的达西那儿实习开始，到学院的J2EE、软件工程的大程，一路相同的架构配置，轻车熟路，不过这次可谓变化不小，配置项因此有不小的改变，也学到一些东西。</p>
<p><span id="more-815"></span></p>
<h3>Spring MVC的XML</h3>
<p>Spring MVC的&lt;servlet-name&gt;-servlet.xml（以下用spring-servlet.xml表示）和普通Spring结构的applicationContext.xml，两者有比较微妙的关系。</p>
<p>Spring MVC项目中，web.xml的servle项按如下配置好，Spring会自动加载applicationContext.xml之后再加载spring-servlet.xml（推测原因，因为后者能引用前者的bean，反之则不行），其中关于Controller的<context:component-scan/>项必须放在spring-servlet.xml中，而其他比如datasource、dao、service等bean则建议放在applicationContext.xml中。</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.web.servlet.DispatcherServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #808080; font-style: italic;">&lt;!-- </span>
<span style="color: #808080; font-style: italic;">	这里如果填了下面的项，会导致仅加载applicationContext.xml而不加载spring-servlet.xml</span>
<span style="color: #808080; font-style: italic;">	这样就只能在applicationContext.xml里手动去加载其他xml，&lt;import resource=&quot;spring-servlet.xml&quot;/&gt;</span>
<span style="color: #808080; font-style: italic;">	&lt;init-param&gt;</span>
<span style="color: #808080; font-style: italic;">		&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;</span>
<span style="color: #808080; font-style: italic;">		&lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;</span>
<span style="color: #808080; font-style: italic;">	&lt;/init-param&gt;</span>
<span style="color: #808080; font-style: italic;">	--&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<h3>Spring MVC的模板层配置</h3>
<p>先说Spring MVC的模板层，一直以来习惯使用Freemarker，这就需要配置Freemarker的bean和Spring连接Freemarker的ViewResolver，FreeMarkerViewResolver。一般方法就是在spring-servlet.xml中，将这几个bean的信息注入。如下所示：</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;fmXmlEscape&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;freemarker.template.utility.XmlEscape&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;freemarkerConfig&quot;</span> </span>
<span style="color: #009900;">	<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;templateLoaderPath&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/views/&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;freemarkerSettings&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;props<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;defaultEncoding&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>UTF-8<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;datetime_format&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>yyyy-MM-dd<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;number_format&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>0.######<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/props<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;freemarkerVariables&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;map<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;xml_escape&quot;</span> <span style="color: #000066;">value-ref</span>=<span style="color: #ff0000;">&quot;fmXmlEscape&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;defaultEncoding&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;viewResolver&quot;</span> </span>
<span style="color: #009900;">	<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;prefix&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;suffix&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;.ftl&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;exposeSpringMacroHelpers&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;contentType&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;text/html;charset=UTF-8&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>这次项目比较正规，我负责后端Java开发，前端页面另有人负责，而且前端程序员希望用JSP页面来完成，于是正好换一个新的模板引擎试试。其实自己一直很想用JSP的，这样又可以省去Freemarker这一第三方类库，少一点总是好的。换成JSP，当然就要换成JSP的ViewResolver，叫InternalResourceViewResolver，而JSP因为是J2EE集成的模板，所以不需要像Freemarker还要配置，的确是方便了！bean如下：</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;prefix&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/views/&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;suffix&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;.jsp&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<h3>Hibernate Annotation的外键配置</h3>
<p>前几个项目中虽然一直都有用@ManyToOne、@OneToOne和@JoinColumn这种Annotation来配置对应关系，但一直都是代码逻辑来控制的，并没有将外键实施于数据库中。说了这次项目比较正规嘛，要求数据库中也是有外键约束的，于是又要重新动动脑筋。说来也奇怪的，搞了好久的Annotation，也百度了好久才找到：<strong>MySQL中MyISAM类型的数据表是不支持外键的，InnoDB类型才支持！</strong>说来关于MySQL的数据表类型是MyISAM还是InnoDB曾经也遇到过类似问题，不过是不是外键就不记得了。</p>
<p>找到了问题所在，剩下的就是如何将Hibernate的自动建表机制（hibernate.hbm2ddl.auto）将数据表建为InnoDB了。居然没想到百度还搜不到，最后在Google的英文页面中找到了配置方法：</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;">hibernate.<span style="color: #006633;">dialect</span> <span style="color: #339933;">=</span> org.<span style="color: #006633;">hibernate</span>.<span style="color: #006633;">dialect</span>.<span style="color: #006633;">MySQLDialect</span>	<span style="color: #339933;">=&gt;</span>	MyISAM表
hibernate.<span style="color: #006633;">dialect</span> <span style="color: #339933;">=</span> org.<span style="color: #006633;">hibernate</span>.<span style="color: #006633;">dialect</span>.<span style="color: #006633;">MySQLInnoDBDialect</span>	<span style="color: #339933;">=&gt;</span>	InnoDB表</pre></td></tr></table></div>

<h3>深爱的Spring</h3>
<p>说起著名的SSH框架，Spring是我第二个学的，初学的时候感觉非常难，《Pro Spring》中的反转控制和依赖注入实在是太抽象的概念。幸运的是，通过实习项目中的coding，立马就深深地理解了Spring的各种编程理念和思维方式。从而也接触了Spring MVC，接触了全Annotation的配置，立马就让我放弃了Structs。至于Hibernate，其实也不是很喜欢，又复杂又臃肿，不如JDBC来的简洁方便！所以，当“SSH你最喜欢哪个框架”这种问题摆在我面前，毫无疑问的！难道不觉得Spring的名字也是最美的么？ <img src="http://blog.11034.org/wp-includes/images/smilies/icon_mrgreen.gif" alt=":mrgreen:" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h4  class="related_post_title">看看 Hibernate , MySQL , Spring</h4><ul class="related_post"><li>2016-03-22 -- <a target="_blank" href="http://blog.11034.org/2016-03/spring_mvc_applicationcontext.html" title="Spring-MVC中的一些问题">Spring-MVC中的一些问题</a></li><li>2015-08-12 -- <a target="_blank" href="http://blog.11034.org/2015-08/rails_nginx.html" title="Liunx下搭建Rails和Nginx环境">Liunx下搭建Rails和Nginx环境</a></li><li>2013-08-10 -- <a target="_blank" href="http://blog.11034.org/2013-08/some_skills_in_java.html" title="学到的一些东西">学到的一些东西</a></li><li>2013-06-23 -- <a target="_blank" href="http://blog.11034.org/2013-06/shida.html" title="院版十大了，那就XYT一下~">院版十大了，那就XYT一下~</a></li><li>2012-05-13 -- <a target="_blank" href="http://blog.11034.org/2012-05/java_play_framework.html" title="Java Play framework">Java Play framework</a></li></ul><h4 class="related_post_title">看看 Java </h4><ul class="related_post"><li>2016-09-09 -- <a target="_blank" href="http://blog.11034.org/2016-09/64bits_linux_arena_memory.html" title="64位Linux下Java进程堆外内存迷之64M问题">64位Linux下Java进程堆外内存迷之64M问题</a></li><li>2016-08-18 -- <a target="_blank" href="http://blog.11034.org/2016-08/java_concurrency_in_practice.html" title="读java concurrency in practice">读java concurrency in practice</a></li><li>2016-08-05 -- <a target="_blank" href="http://blog.11034.org/2016-08/thread_stop.html" title="线程清理">线程清理</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/futuretask.html" title="FutureTask简单分析和用法">FutureTask简单分析和用法</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/semaphore.html" title="Semaphore简单分析">Semaphore简单分析</a></li>]]></content:encoded>
			<wfw:commentRss>http://blog.11034.org/2011-03/spring_hibernate_annotation.html/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
