﻿<?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-人在江湖-随笔分类-spring</title><link>http://www.blogjava.net/vcycyv/category/47722.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 14 Mar 2011 03:46:27 GMT</lastBuildDate><pubDate>Mon, 14 Mar 2011 03:46:27 GMT</pubDate><ttl>60</ttl><item><title>总结Spring Security之 关于授权，保护web和保护方法</title><link>http://www.blogjava.net/vcycyv/archive/2011/03/14/346178.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Mon, 14 Mar 2011 00:41:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2011/03/14/346178.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/346178.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2011/03/14/346178.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/346178.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/346178.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 接上一篇 总结Spring Security之 关于Authentication * 关于授权 AcessDecisionManager是管授权的。具体授权（authorization）的工作是交给一系列Voter来做的。每个Voter都实现AccessDecisionVoter接口的vote方法，返回 int ACCESS_GRANTED = 1;（投赞成票）  int ACCESS_ABSTA...&nbsp;&nbsp;<a href='http://www.blogjava.net/vcycyv/archive/2011/03/14/346178.html'>阅读全文</a><img src ="http://www.blogjava.net/vcycyv/aggbug/346178.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2011-03-14 08:41 <a href="http://www.blogjava.net/vcycyv/archive/2011/03/14/346178.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>总结Spring Security之 关于Authentication</title><link>http://www.blogjava.net/vcycyv/archive/2011/03/08/345922.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Tue, 08 Mar 2011 03:02:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2011/03/08/345922.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/345922.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2011/03/08/345922.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/345922.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/345922.html</trackback:ping><description><![CDATA[<p>前言<br />
开始花了两三天的时间学Spring Security，还是云山雾罩的，大受打击。于是重新总结一下，飞越迷雾，梳理思路，写这样一篇文字。网上有个雷锋写了Spring Security2 学习精讲：http://www.javaeye.com/topic/319965里面包含可以运行的代码，如果你对spring scurity感兴趣，可以快速浏览一下下面的笔记，然后debug code，然后再看看笔记。Spring Security的内容远比笔记复杂，我只是根据自己的理解挑重要的记录并整理一下。把sample code也当作笔记的一部分，那个code还是比较精简地，更重要的是实用。<br />
官方提供的sample code包居然没有源代码，faint， google半天找到http://grepcode.com/snapshot/repo1.maven.org/maven2/org.springframework.security/spring-security-samples-contacts/2.0.0 当然，如果你会用git的话也可以自己check out code, 不过我没用过git这种高级货。<br />
<br />
正文<br />
跟权限有关的两个概念是 认证 和 授权， 先上个图：</p>
<p><a href="http://www.blogjava.net/images/blogjava_net/vcycyv/WindowsLiveWriter/SpringSecurityAuthentication_9B2A/image_2.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.blogjava.net/images/blogjava_net/vcycyv/WindowsLiveWriter/SpringSecurityAuthentication_9B2A/image_thumb.png" width="627" height="191" /></a> </p>
<p>Run-As Manager 和 After-Invocation Manager不重要</p>
<p>The&nbsp; actual&nbsp; implementation&nbsp; of&nbsp; a&nbsp; security&nbsp; interceptor&nbsp; will&nbsp; depend&nbsp; on&nbsp; what resource is being secured. If you&#8217;re securing a URL in a web application, the security&nbsp; interceptor&nbsp; will&nbsp; be&nbsp; implemented&nbsp; as&nbsp; a&nbsp; servlet&nbsp; filter.&nbsp; But&nbsp; if&nbsp; you&#8217;re&nbsp; securing&nbsp; a method invocation, aspects will be used to enforce security.
<p>这篇只说Authentication Manager:
<p>认证是通过<em>AuthenticationManager</em>来管的，
<p>public interface AuthenticationManager {<br />
&nbsp; public Authentication authenticate(Authentication authentication)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throws AuthenticationException;
<p>}
<p>The&nbsp; authenticate()&nbsp; method&nbsp; will&nbsp; attempt&nbsp; to&nbsp; authenticate&nbsp; the&nbsp; user&nbsp; using&nbsp; the org.acegisecurity.Authentication object (which carries the principal and credentials). If successful, the authenticate() method returns a complete Authentication&nbsp; object,&nbsp; including&nbsp; information&nbsp; about&nbsp; the&nbsp; user&#8217;s&nbsp; granted&nbsp; authorities (which will be considered by the authorization manager).
<p>具体的工作是交给各个 authentication provider来做的：
<p><a href="http://www.blogjava.net/images/blogjava_net/vcycyv/WindowsLiveWriter/SpringSecurityAuthentication_9B2A/image_4.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.blogjava.net/images/blogjava_net/vcycyv/WindowsLiveWriter/SpringSecurityAuthentication_9B2A/image_thumb_1.png" width="498" height="343" /></a>
<p>这里provider manager包含多个具体的providers:
<p>&lt;bean id="authenticationManager" <br />
&nbsp;&nbsp;&nbsp; class="org.acegisecurity.providers.ProviderManager"&gt;<br />
&nbsp; &lt;property name="providers"&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;list&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ref bean="daoAuthenticationProvider"/&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ref bean="ldapAuthenticationProvider"/&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;/list&gt;<br />
&nbsp; &lt;/property&gt;<br />
&lt;/bean&gt;<br />
ProviderManager is given its list of authentication providers through its providers property.
<p>以DaoAuthenticationProvider举例：
<p>&lt;bean id="authenticationProvider" <br />
&nbsp;&nbsp;&nbsp; class="org.acegisecurity.providers.dao.DaoAuthenticationProvider"&gt;<br />
&nbsp; &lt;property name="userDetailsService"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ref="userDetailsService"/&gt;<br />
&lt;/bean&gt;
<p>它会要求一个UserDetailsService, 跟它相关的是UserDetails接口
<p>UserDetailsService接口是个简单的接口
<p>public interface UserDetailsService {<br />
&nbsp;&nbsp;&nbsp; UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException;<br />
}
<p>&nbsp;
<p>UserDetails接口如下：
<p>public interface UserDetails extends Serializable {<br />
&nbsp;&nbsp;&nbsp; GrantedAuthority[] getAuthorities();
<p>&nbsp;&nbsp;&nbsp; String getPassword();
<p>&nbsp;&nbsp;&nbsp; String getUsername();
<p>&nbsp;&nbsp;&nbsp; boolean isAccountNonExpired();
<p>&nbsp;&nbsp;&nbsp; boolean isAccountNonLocked();
<p>&nbsp;&nbsp;&nbsp; boolean isCredentialsNonExpired();
<p>&nbsp;&nbsp;&nbsp; boolean isEnabled();<br />
}
<p>解释一下getAuthorities:该方法返回一个GrantedAuthority[]数组对象，GrantedAuthority是用户权限信息对象，这个对象中定义了一个获取用户权限描述信息的getAuthority()方法。
<p>需要注意<em>Authentication</em>对象才是Spring Security使用的进行安全访问控制用户信息安全对象。实际上，Authentication对象有未认证和已认证两种状态，在作为参数传入认证管理器（AuthenticationManager）的authenticate方法时，是一个未认证的对象，它从客户端获取用户的身份信息（如用户名，密码），可以是从一个登录页面，也可以从Cookie中获取，并由系统自动构造成一个Authentication对象。而这里提到的UserDetails代表一个用户安全信息的源（从数据库，LDAP服务器，CA中心返回），Spring Security要做的就是将这个未认证的Authentication对象和UserDetails进行匹配，成功后将UserDetails中的用户权限信息拷贝到Authentication中组成一个完整的Authentication对象，共其它组件共享。
<p>&nbsp;<br />
下一篇： <span style="color: #0000ff"><a id="homepage1_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl00_TitleUrl" href="http://www.blogjava.net/vcycyv/archive/2011/03/14/346178.html"><u>总结Spring Security之 关于授权，保护web和保护方法</u></a><u>&nbsp;</u></span><br />
&nbsp;
<p>参考：
<p>Spring in Action
<p>Spring Security学习总结： <a title="http://www.blogjava.net/redhatlinux/archive/2008/08/20/223148.html" href="http://www.blogjava.net/redhatlinux/archive/2008/08/20/223148.html">http://www.blogjava.net/redhatlinux/archive/2008/08/20/223148.html</a>
<p>Spring Security2 学习精讲： <a href="http://www.javaeye.com/topic/319965">http://www.javaeye.com/topic/319965</a> </p>
<img src ="http://www.blogjava.net/vcycyv/aggbug/345922.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2011-03-08 11:02 <a href="http://www.blogjava.net/vcycyv/archive/2011/03/08/345922.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>详细讲解在Spring中进行集成测试（转）</title><link>http://www.blogjava.net/vcycyv/archive/2011/02/24/345040.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Thu, 24 Feb 2011 02:12:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2011/02/24/345040.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/345040.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2011/02/24/345040.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/345040.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/345040.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 转载自 http://tech.it168.com/j/2007-10-19/200710190919953.shtml &nbsp; 在单元测试时，我们尽量在屏蔽模块间相互干扰的情况下，重点关注模块内部逻辑的正确性。而集成测试则是在将模块整合在一起后进行的测试，它的目的在于发现一些模块间整合的问题。有些功能很难通过模拟对象进行模拟，相反它们往往只能在真实模块整合后，才能真正运行起来，如事务管理就...&nbsp;&nbsp;<a href='http://www.blogjava.net/vcycyv/archive/2011/02/24/345040.html'>阅读全文</a><img src ="http://www.blogjava.net/vcycyv/aggbug/345040.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2011-02-24 10:12 <a href="http://www.blogjava.net/vcycyv/archive/2011/02/24/345040.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>spring security2 配置精讲（转）</title><link>http://www.blogjava.net/vcycyv/archive/2011/02/13/344196.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Sun, 13 Feb 2011 06:47:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2011/02/13/344196.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/344196.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2011/02/13/344196.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/344196.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/344196.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 转载自：http://www.javaeye.com/topic/319965论坛上看了不少Spring Security的相关文章。这些文章基本上都还是基于Acegi-1.X的配置方式，而主要的配置示例也来自于SpringSide的贡献。 众所周知，Spring Security针对Acegi的一个重大的改进就在于其配置方式大大简化了。所以如果配置还是基于Acegi-1.X这样比较繁琐的...&nbsp;&nbsp;<a href='http://www.blogjava.net/vcycyv/archive/2011/02/13/344196.html'>阅读全文</a><img src ="http://www.blogjava.net/vcycyv/aggbug/344196.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2011-02-13 14:47 <a href="http://www.blogjava.net/vcycyv/archive/2011/02/13/344196.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>spring factory-method 动态传参数（转）</title><link>http://www.blogjava.net/vcycyv/archive/2011/01/30/343778.html</link><dc:creator>人在江湖</dc:creator><author>人在江湖</author><pubDate>Sun, 30 Jan 2011 10:04:00 GMT</pubDate><guid>http://www.blogjava.net/vcycyv/archive/2011/01/30/343778.html</guid><wfw:comment>http://www.blogjava.net/vcycyv/comments/343778.html</wfw:comment><comments>http://www.blogjava.net/vcycyv/archive/2011/01/30/343778.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/vcycyv/comments/commentRss/343778.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/vcycyv/services/trackbacks/343778.html</trackback:ping><description><![CDATA[<p>转载自 <a title="http://forum.springsource.org/showthread.php?t=50934" href="http://forum.springsource.org/showthread.php?t=50934">http://forum.springsource.org/showthread.php?t=50934</a></p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">class</span> ExampleBean {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>   <span style="color: #0000ff">private</span> String string;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>   <span style="color: #0000ff">public</span> ExampleBean(String string) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>     <span style="color: #0000ff">this</span>.string = string;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>   }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>   </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>   <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> write() {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>     System.out.println(<span style="color: #006080">"The text is: "</span> + text);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>   }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span> <span style="color: #0000ff">class</span> ExampleBeanFactory {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>   <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> ExampleBean createExampleBean(String string) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>     <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> ExampleBean(string);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>   }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Main {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>   <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>     ApplicationContext context = <span style="color: #0000ff">new</span> ClassPathXmlApplicationContext(</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>                     <span style="color: #006080">"context.xml"</span>);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>     ExampleBean exampleBean = </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>             ExampleBean)context.getBean(<span style="color: #006080">"exampleBean"</span>, </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>               <span style="color: #0000ff">new</span> Object[]{<span style="color: #006080">"bla bla"</span>});</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>     exampleBean.write();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>   }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span> }</pre><!--CRLF--></div></div>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">bean</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">="exampleBean"</span> <span style="color: #ff0000">class</span><span style="color: #0000ff">="...ExampleBeanFactory"</span>  <span style="color: #ff0000">scope</span><span style="color: #0000ff">="prototype"</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>         <span style="color: #ff0000">factory-method</span><span style="color: #0000ff">="createExampleBean"</span><span style="color: #0000ff">&gt;</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">constructor-arg</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="default value"</span><span style="color: #0000ff">/&gt;</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">bean</span><span style="color: #0000ff">&gt;</span></pre><!--CRLF--></div></div>
<p>&nbsp;</p>
<p>注意scope使用prototype</p>    <img src ="http://www.blogjava.net/vcycyv/aggbug/343778.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/vcycyv/" target="_blank">人在江湖</a> 2011-01-30 18:04 <a href="http://www.blogjava.net/vcycyv/archive/2011/01/30/343778.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>