﻿<?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-sichuanxiaohuo-文章分类-spring</title><link>http://www.blogjava.net/sichuanxiaohuo/category/49553.html</link><description /><language>zh-cn</language><lastBuildDate>Sat, 10 Sep 2011 02:57:42 GMT</lastBuildDate><pubDate>Sat, 10 Sep 2011 02:57:42 GMT</pubDate><ttl>60</ttl><item><title>spring security 配置文件小结(逐步深化到url级别) </title><link>http://www.blogjava.net/sichuanxiaohuo/articles/358103.html</link><dc:creator>sichuanxiaohuo</dc:creator><author>sichuanxiaohuo</author><pubDate>Tue, 06 Sep 2011 06:22:00 GMT</pubDate><guid>http://www.blogjava.net/sichuanxiaohuo/articles/358103.html</guid><wfw:comment>http://www.blogjava.net/sichuanxiaohuo/comments/358103.html</wfw:comment><comments>http://www.blogjava.net/sichuanxiaohuo/articles/358103.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sichuanxiaohuo/comments/commentRss/358103.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sichuanxiaohuo/services/trackbacks/358103.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Java代码 &nbsp;一&nbsp;、方式一：用户名密码都在配置文件中。 &nbsp;&nbsp;&lt;?xml&nbsp;version="1.0"&nbsp;encoding="UTF-8"?&gt; &nbsp;&nbsp;&lt;beans:beans&nbsp;xmlns="http://www.springframework.org/schema/security"&nbsp...&nbsp;&nbsp;<a href='http://www.blogjava.net/sichuanxiaohuo/articles/358103.html'>阅读全文</a><img src ="http://www.blogjava.net/sichuanxiaohuo/aggbug/358103.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sichuanxiaohuo/" target="_blank">sichuanxiaohuo</a> 2011-09-06 14:22 <a href="http://www.blogjava.net/sichuanxiaohuo/articles/358103.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> spring组件扫描&lt;context:component-scan/&gt;使用详解</title><link>http://www.blogjava.net/sichuanxiaohuo/articles/358083.html</link><dc:creator>sichuanxiaohuo</dc:creator><author>sichuanxiaohuo</author><pubDate>Tue, 06 Sep 2011 05:26:00 GMT</pubDate><guid>http://www.blogjava.net/sichuanxiaohuo/articles/358083.html</guid><wfw:comment>http://www.blogjava.net/sichuanxiaohuo/comments/358083.html</wfw:comment><comments>http://www.blogjava.net/sichuanxiaohuo/articles/358083.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sichuanxiaohuo/comments/commentRss/358083.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sichuanxiaohuo/services/trackbacks/358083.html</trackback:ping><description><![CDATA[<div><strong><strong></strong>下面是引用spring framework开发手册中的一段话</strong>&#8220;</div>
<p>Spring 2.5引入了更多典型化注解(stereotype annotations)： <code>@Component</code>、<code>@Service</code>和 <code>@Controller</code>。 <code>@Component</code>是所有受Spring管理组件的通用形式； 而<code>@Repository</code>、<code>@Service</code>和 <code>@Controller</code>则是<code>@Component</code>的细化，用来表示更具体的用例(例如，分别对应了持久化层、服务层和表现层)。也就是说， 你能用<code>@Component</code>来注解你的组件类， 但如果用<code>@Repository</code>、<code>@Service</code> 或<code>@Controller</code>来注解它们，你的类也许能更好地被工具处理，或与切面进行关联。例如，这些典型化注解可以成为理想的切入点目标。当然，在Spring Framework以后的版本中， <code>@Repository</code>、<code>@Service</code>和 <code>@Controller</code>也许还能携带更多语义。如此一来，如果你正在考虑服务层中是该用 <code>@Component</code>还是<code>@Service</code>， 那<code>@Service</code>显然是更好的选择。同样的，就像前面说的那样， <code>@Repository</code>已经能在持久化层中进行异常转换时被作为标记使用了。&#8221;</p>
<p>&nbsp;<wbr></p>
<p><span style="color: #000000"><span><strong>下面是网上目前关于组件扫描最详细的介绍</strong></span></span></p>
<p style="line-height: normal">Spring applicationContext.xml的&lt;context:component-scan&gt;標籤用途比我想像的還要實用。而且後來才知道，有了&lt;context:component-scan&gt;，另一個&lt;context:annotation-config/&gt;標籤根本可以移除掉，因為被包含進去了。原本我survery Spring3通常只配置成&lt;context:component-scan base-package="com.foo.bar"/&gt;，意即在base-package下尋找有@Component和@Configuration的target Class。而現在如下的飯粒：</p>
<table style="line-height: normal" border="2" cellspacing="0" cellpadding="2" width="623">
<tbody style="line-height: normal">
<tr style="line-height: normal">
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="619">
<p style="line-height: normal"><span style="line-height: normal"><font face="Arial">&lt;context:component-scan base-package="com.foo" <span style="line-height: normal; color: #ff00ff">use-default-filters</span>="false"&gt;<br style="line-height: normal" />&lt;<span style="line-height: normal; color: #ff0000">context:include-filter</span> type="regex" expression="com.foo.bar.*Config"/&gt;<br style="line-height: normal" />&lt;context:include-filter type="regex" expression="com.foo.config.*"/&gt;<br style="line-height: normal" />&lt;/context:component-scan&gt;</font></span></p></td></tr></tbody></table>
<p style="line-height: normal">　　&lt;context:component-scan&gt;提供兩個子標籤：&lt;context:include-filter&gt;和&lt;context:exclude-filter&gt;各代表引入和排除的過濾。而上例把use-default-filters屬性設為false，意即在base-package所有被宣告為@Component和@Configuration等target Class不予註冊為bean，由filter子標籤代勞。</p>
<p style="line-height: normal">　　filter標籤在Spring3有五個type，如下：</p>
<table style="line-height: normal" border="2" cellspacing="0" cellpadding="2" width="674">
<tbody style="line-height: normal">
<tr style="line-height: normal">
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="83"><strong style="line-height: normal">Filter Type</strong></td>
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="170"><strong style="line-height: normal">Examples Expression</strong></td>
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="417"><strong style="line-height: normal">Description</strong></td></tr>
<tr style="line-height: normal">
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="83">annotation</td>
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="170">org.example.SomeAnnotation</td>
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="417">符合SomeAnnoation的target class</td></tr>
<tr style="line-height: normal">
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="83">assignable</td>
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="170">org.example.SomeClass</td>
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="417">指定class或interface的全名</td></tr>
<tr style="line-height: normal">
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="83">aspectj</td>
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="170">org.example..*Service+</td>
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="417">AspectJ語法</td></tr>
<tr style="line-height: normal">
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="83">regex</td>
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="170">org\.example\.Default.*</td>
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="417">Regelar Expression</td></tr>
<tr style="line-height: normal">
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="83">custom</td>
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="170">org.example.MyTypeFilter</td>
<td style="line-height: normal; font-family: Arial; font-size: 12px" valign="top" width="417">Spring3新增自訂Type，實作org.springframework.core.type.TypeFilter</td></tr></tbody></table>
<p style="line-height: normal">　　所以上例用的regex就有個語病，<span style="line-height: normal">com.foo.config.*</span> 可以找到com.foo.config.WebLogger，但也可以找到com1fool2config3abcde，因為小數點在Regex是任意字元，是故要用\.把小數點跳脫為佳。(2010/3/15補充：但要使用\.方式，其use-default-filters不能為false，否則抓不到，感覺是Bug)</p>
<p style="line-height: normal">　　Spring3提供豐富的Filter支援，有益配置策略，不需面臨Configuration Hell，比如Regex的com\.foo\.*\.action\.*Config，這樣就可以找到com.foo package下所有action子package的*Config的target class。</p>
<p style="line-height: normal">&nbsp;<wbr></p>
<p style="line-height: normal">我按他的例子，配置了我自己的如下：</p>
<div>&lt;context:component-scan base-package="com.xhlx.finance.budget"&nbsp;<wbr> &gt;<br />&nbsp;<wbr>&nbsp;<wbr>&lt;context:include-filter type="regex" expression="com.lee.finance.budget.service.*"/&gt;<br />&nbsp;<wbr>&nbsp;<wbr>&lt;context:include-filter type="regex" expression="com.lee.finance.budget.security.*"/&gt;&nbsp;<wbr><br />&lt;/context:component-scan&gt;</div>
<div>但是死活扫描不到，网上又没有更好的讲解，没办法只好自己试，改成</div>
<div>&lt;context:component-scan base-package="com.xhlx.finance.budget" &gt;<br />&nbsp;<wbr> &lt;context:include-filter type="regex" expression="com.lee.finance.budget.*"/&gt;<br />&lt;/context:component-scan&gt;</div>
<div>这样连没有加注解的类也扫描并实例化，结果报错，因为有的类根本就没有默认的构造函数不能实例化，能不报错吗</div>
<div>改成</div>
<div>&lt;context:component-scan base-package="com.xhlx.finance.budget"&nbsp;<wbr> &gt;<br />&nbsp;<wbr>&nbsp;<wbr>&lt;context:include-filter type="regex" expression="com\.lee\.finance\.budget\.service.*"/&gt;&nbsp;<wbr>&nbsp;<wbr><br />&lt;/context:component-scan&gt;问题依旧</div>
<div>
<div>&lt;context:component-scan base-package="com.xhlx.finance.budget" &gt;<br />&nbsp;<wbr> &lt;context:include-filter type="regex" expression="com.lee.finance.budget.service.TestService"/&gt;<br />&lt;/context:component-scan&gt;</div>
<div>嘿，这次可以了，写全限定名就可以，表达式却不行，但是如果我有成千上百个还得一个一个这样配置啊？不行，继续研究，我想有个base-package的配置，从表面意思来看这是个&#8220;基本包&#8221;，是否表示下面的过滤路径是基于这个包的呢？于是试着改成</div>
<div>&lt;context:component-scan base-package="com.xhlx.finance.budget" &gt;<br />&nbsp;<wbr> &lt;context:include-filter type="regex" expression=".service.*"/&gt;<br />&lt;/context:component-scan&gt;</div></div><img src ="http://www.blogjava.net/sichuanxiaohuo/aggbug/358083.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sichuanxiaohuo/" target="_blank">sichuanxiaohuo</a> 2011-09-06 13:26 <a href="http://www.blogjava.net/sichuanxiaohuo/articles/358083.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>