Spring认证 - Bean 范围教程

分享 0 481
IT胶囊
IT胶囊 LV1 2021年4月26日 23:35 发表
点击群号免费加入社区交流群:367346704
<p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">定义 &lt;bean&gt; 时,您可以选择声明该 bean 的作用域。例如,要强制 Spring 在每次需要时生成一个新的 bean 实例,您应该将 bean 的 scope 属性声明为<strong style="list-style: inherit;">prototype</strong>。类似地,如果您希望 Spring 在每次需要时返回相同的 bean 实例,您应该将 bean 的 scope 属性声明为<strong style="list-style: inherit;">singleton</strong>。</p><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">Spring Framework 支持以下五个范围,其中三个仅在您使用 web-aware ApplicationContext 时可用。</p><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;"></p><figure style="list-style: inherit; margin-top: 16px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;"><div style="list-style: inherit;"><span style="list-style: inherit;"><img src="https://ask8088-private-1251520898.cn-south.myqcloud.com/developer-images/article/6461686/orw45uvd27.png?q-sign-algorithm=sha1&amp;q-ak=AKID2uZ1FGBdx1pNgjE3KK4YliPpzyjLZvug&amp;q-sign-time=1629450030;1629457230&amp;q-key-time=1629450030;1629457230&amp;q-header-list=&amp;q-url-param-list=&amp;q-signature=f97a8ce484d8f3badbe702a487925364447e9309" style="border-width: 0px; border-style: initial; list-style: inherit; display: block; margin-right: auto; margin-left: auto; max-width: 100%; max-height: 100%; box-shadow: rgba(3, 27, 78, 0.12) 0px 4px 8px; border-radius: 3px; cursor: zoom-in;"></span></div></figure><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">在本章中,我们将讨论前两个范围,其余三个范围将在讨论 Web 感知 Spring ApplicationContext 时讨论。</p><h2 id="%E5%8D%95%E4%BE%8B%E8%8C%83%E5%9B%B4" name="%E5%8D%95%E4%BE%8B%E8%8C%83%E5%9B%B4" style="margin-top: 16px; margin-bottom: 8px; font-size: 16px; list-style: inherit; font-weight: bold; line-height: 26px; font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">单例范围</h2><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">如果范围设置为单例,则 Spring IoC 容器将创建该 bean 定义定义的对象的一个实例。该单个实例存储在此类单例 bean 的缓存中,并且对该命名 bean 的所有后续请求和引用都返回缓存对象。</p><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">默认范围始终是单例。但是,当您只需要一个 bean 实例时,您可以在 bean 配置文件中将<strong style="list-style: inherit;">scope</strong>属性设置为<strong style="list-style: inherit;">singleton</strong>,如下面的代码片段所示 -</p><pre style="list-style: inherit; margin-top: 0.5em; margin-bottom: 0.5em; padding: 1em; color: rgb(204, 204, 204); background: rgb(80, 85, 107); border-radius: 3px; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace; overflow-wrap: normal; white-space: pre; word-break: normal; tab-size: 4; hyphens: none; font-size: 14px;"><span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span><span style="list-style: inherit; color: rgb(103, 205, 204);">!</span><span style="list-style: inherit; color: rgb(103, 205, 204);">--</span> A bean definition <span style="list-style: inherit; color: rgb(204, 153, 205);">with</span> singleton scope <span style="list-style: inherit; color: rgb(103, 205, 204);">--</span><span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span>bean id <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"..."</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">class</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"..."</span> scope <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"singleton"</span><span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span><span style="list-style: inherit; color: rgb(103, 205, 204);">!</span><span style="list-style: inherit; color: rgb(103, 205, 204);">--</span> collaborators and configuration <span style="list-style: inherit; color: rgb(204, 153, 205);">for</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">this</span> bean go here <span style="list-style: inherit; color: rgb(103, 205, 204);">--</span><span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span><span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>bean<span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span></pre><h3 id="%E7%A4%BA%E4%BE%8B" name="%E7%A4%BA%E4%BE%8B" style="margin-top: 16px; margin-bottom: 8px; font-size: 14px; list-style: inherit; font-weight: bold; line-height: 24px; font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">示例</h3><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">让我们有一个工作的 Eclipse IDE 并采取以下步骤来创建一个 Spring 应用程序 -</p><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;"></p><figure style="list-style: inherit; margin-top: 16px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;"><div style="list-style: inherit;"><span style="list-style: inherit;"><img src="https://ask8088-private-1251520898.cn-south.myqcloud.com/developer-images/article/6461686/6tisv0yjf8.png?q-sign-algorithm=sha1&amp;q-ak=AKID2uZ1FGBdx1pNgjE3KK4YliPpzyjLZvug&amp;q-sign-time=1629450030;1629457230&amp;q-key-time=1629450030;1629457230&amp;q-header-list=&amp;q-url-param-list=&amp;q-signature=f2871fcfeb6f10e6b80262d679eebbf6c5876db0" style="border-width: 0px; border-style: initial; list-style: inherit; display: block; margin-right: auto; margin-left: auto; max-width: 100%; max-height: 100%; box-shadow: rgba(3, 27, 78, 0.12) 0px 4px 8px; border-radius: 3px; cursor: zoom-in;"></span></div></figure><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">这是<strong style="list-style: inherit;">HelloWorld.java</strong>文件的内容-</p><pre style="list-style: inherit; margin-top: 0.5em; margin-bottom: 0.5em; padding: 1em; color: rgb(204, 204, 204); background: rgb(80, 85, 107); border-radius: 3px; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace; overflow-wrap: normal; white-space: pre; word-break: normal; tab-size: 4; hyphens: none; font-size: 14px;"><span style="list-style: inherit; color: rgb(204, 153, 205);">package</span> com<span style="list-style: inherit;">.</span>tutorialspoint<span style="list-style: inherit;">;</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">public</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">class</span> <span style="list-style: inherit; color: rgb(248, 197, 85);">HelloWorld</span> <span style="list-style: inherit;">{</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">private</span> String message<span style="list-style: inherit;">;</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">public</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">void</span> <span style="list-style: inherit; color: rgb(240, 141, 73);">setMessage</span><span style="list-style: inherit;">(</span>String message<span style="list-style: inherit;">)</span><span style="list-style: inherit;">{</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">this</span><span style="list-style: inherit;">.</span>message <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> message<span style="list-style: inherit;">;</span> <span style="list-style: inherit;">}</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">public</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">void</span> <span style="list-style: inherit; color: rgb(240, 141, 73);">getMessage</span><span style="list-style: inherit;">(</span><span style="list-style: inherit;">)</span><span style="list-style: inherit;">{</span> System<span style="list-style: inherit;">.</span>out<span style="list-style: inherit;">.</span><span style="list-style: inherit; color: rgb(240, 141, 73);">println</span><span style="list-style: inherit;">(</span><span style="list-style: inherit; color: rgb(126, 198, 153);">"Your Message : "</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">+</span> message<span style="list-style: inherit;">)</span><span style="list-style: inherit;">;</span> <span style="list-style: inherit;">}</span> <span style="list-style: inherit;">}</span></pre><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">以下是<strong style="list-style: inherit;">MainApp.java</strong>文件的内容-</p><pre style="list-style: inherit; margin-top: 0.5em; margin-bottom: 0.5em; padding: 1em; color: rgb(204, 204, 204); background: rgb(80, 85, 107); border-radius: 3px; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace; overflow-wrap: normal; white-space: pre; word-break: normal; tab-size: 4; hyphens: none; font-size: 14px;"><span style="list-style: inherit; color: rgb(204, 153, 205);">package</span> com<span style="list-style: inherit;">.</span>tutorialspoint<span style="list-style: inherit;">;</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">import</span> org<span style="list-style: inherit;">.</span>springframework<span style="list-style: inherit;">.</span>context<span style="list-style: inherit;">.</span>ApplicationContext<span style="list-style: inherit;">;</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">import</span> org<span style="list-style: inherit;">.</span>springframework<span style="list-style: inherit;">.</span>context<span style="list-style: inherit;">.</span>support<span style="list-style: inherit;">.</span>ClassPathXmlApplicationContext<span style="list-style: inherit;">;</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">public</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">class</span> <span style="list-style: inherit; color: rgb(248, 197, 85);">MainApp</span> <span style="list-style: inherit;">{</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">public</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">static</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">void</span> <span style="list-style: inherit; color: rgb(240, 141, 73);">main</span><span style="list-style: inherit;">(</span>String<span style="list-style: inherit;">[</span><span style="list-style: inherit;">]</span> args<span style="list-style: inherit;">)</span> <span style="list-style: inherit;">{</span> ApplicationContext context <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">new</span> <span style="list-style: inherit; color: rgb(248, 197, 85);">ClassPathXmlApplicationContext</span><span style="list-style: inherit;">(</span><span style="list-style: inherit; color: rgb(126, 198, 153);">"Beans.xml"</span><span style="list-style: inherit;">)</span><span style="list-style: inherit;">;</span> HelloWorld objA <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit;">(</span>HelloWorld<span style="list-style: inherit;">)</span> context<span style="list-style: inherit;">.</span><span style="list-style: inherit; color: rgb(240, 141, 73);">getBean</span><span style="list-style: inherit;">(</span><span style="list-style: inherit; color: rgb(126, 198, 153);">"helloWorld"</span><span style="list-style: inherit;">)</span><span style="list-style: inherit;">;</span> objA<span style="list-style: inherit;">.</span><span style="list-style: inherit; color: rgb(240, 141, 73);">setMessage</span><span style="list-style: inherit;">(</span><span style="list-style: inherit; color: rgb(126, 198, 153);">"I'm object A"</span><span style="list-style: inherit;">)</span><span style="list-style: inherit;">;</span> objA<span style="list-style: inherit;">.</span><span style="list-style: inherit; color: rgb(240, 141, 73);">getMessage</span><span style="list-style: inherit;">(</span><span style="list-style: inherit;">)</span><span style="list-style: inherit;">;</span> HelloWorld objB <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit;">(</span>HelloWorld<span style="list-style: inherit;">)</span> context<span style="list-style: inherit;">.</span><span style="list-style: inherit; color: rgb(240, 141, 73);">getBean</span><span style="list-style: inherit;">(</span><span style="list-style: inherit; color: rgb(126, 198, 153);">"helloWorld"</span><span style="list-style: inherit;">)</span><span style="list-style: inherit;">;</span> objB<span style="list-style: inherit;">.</span><span style="list-style: inherit; color: rgb(240, 141, 73);">getMessage</span><span style="list-style: inherit;">(</span><span style="list-style: inherit;">)</span><span style="list-style: inherit;">;</span> <span style="list-style: inherit;">}</span> <span style="list-style: inherit;">}</span></pre><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">以下是单例范围所需的配置文件<strong style="list-style: inherit;">Beans.xml</strong> -</p><pre style="list-style: inherit; margin-top: 0.5em; margin-bottom: 0.5em; padding: 1em; color: rgb(204, 204, 204); background: rgb(80, 85, 107); border-radius: 3px; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace; overflow-wrap: normal; white-space: pre; word-break: normal; tab-size: 4; hyphens: none; font-size: 14px;"><span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span><span style="list-style: inherit; color: rgb(103, 205, 204);">?</span>xml version <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"1.0"</span> encoding <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"UTF-8"</span><span style="list-style: inherit; color: rgb(103, 205, 204);">?</span><span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span>beans xmlns <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"http://www.springframework.org/schema/beans"</span> xmlns<span style="list-style: inherit;">:</span>xsi <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"http://www.w3.org/2001/XMLSchema-instance"</span> xsi<span style="list-style: inherit;">:</span>schemaLocation <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> "http<span style="list-style: inherit;">:</span><span style="list-style: inherit; color: rgb(103, 205, 204);">/</span><span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>www<span style="list-style: inherit;">.</span>springframework<span style="list-style: inherit;">.</span>org<span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>schema<span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>beans http<span style="list-style: inherit;">:</span><span style="list-style: inherit; color: rgb(103, 205, 204);">/</span><span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>www<span style="list-style: inherit;">.</span>springframework<span style="list-style: inherit;">.</span>org<span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>schema<span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>beans<span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>spring<span style="list-style: inherit; color: rgb(103, 205, 204);">-</span>beans<span style="list-style: inherit; color: rgb(240, 141, 73);">-3.0</span><span style="list-style: inherit;">.</span>xsd"<span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span>bean id <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"helloWorld"</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">class</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"com.tutorialspoint.HelloWorld"</span> scope <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"singleton"</span><span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span><span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>bean<span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span><span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>beans<span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span></pre><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">完成源文件和 bean 配置文件的创建后,让我们运行应用程序。如果您的应用程序一切正常,它将打印以下消息 -</p><pre style="list-style: inherit; margin-top: 0.5em; margin-bottom: 0.5em; padding: 1em; color: rgb(204, 204, 204); background: rgb(80, 85, 107); border-radius: 3px; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace; overflow-wrap: normal; white-space: pre; word-break: normal; tab-size: 4; hyphens: none; font-size: 14px;">Your Message <span style="list-style: inherit;">:</span> I'm object A Your Message <span style="list-style: inherit;">:</span> I'm object A</pre><h2 id="%E5%8E%9F%E5%9E%8B%E8%8C%83%E5%9B%B4" name="%E5%8E%9F%E5%9E%8B%E8%8C%83%E5%9B%B4" style="margin-top: 16px; margin-bottom: 8px; font-size: 16px; list-style: inherit; font-weight: bold; line-height: 26px; font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">原型范围</h2><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">如果范围设置为原型,则每次发出对该特定 bean 的请求时,Spring IoC 容器都会创建该对象的一个新 bean 实例。通常,对所有有状态 bean 使用原型作用域,对无状态 bean 使用单例作用域。</p><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">要定义原型范围,您可以在 bean 配置文件中将<strong style="list-style: inherit;">范围</strong>属性设置为<strong style="list-style: inherit;">原型</strong>,如以下代码片段所示 -</p><pre style="list-style: inherit; margin-top: 0.5em; margin-bottom: 0.5em; padding: 1em; color: rgb(204, 204, 204); background: rgb(80, 85, 107); border-radius: 3px; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace; overflow-wrap: normal; white-space: pre; word-break: normal; tab-size: 4; hyphens: none; font-size: 14px;"><span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span><span style="list-style: inherit; color: rgb(103, 205, 204);">!</span><span style="list-style: inherit; color: rgb(103, 205, 204);">--</span> A bean definition <span style="list-style: inherit; color: rgb(204, 153, 205);">with</span> prototype scope <span style="list-style: inherit; color: rgb(103, 205, 204);">--</span><span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span>bean id <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"..."</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">class</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"..."</span> scope <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"prototype"</span><span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span><span style="list-style: inherit; color: rgb(103, 205, 204);">!</span><span style="list-style: inherit; color: rgb(103, 205, 204);">--</span> collaborators and configuration <span style="list-style: inherit; color: rgb(204, 153, 205);">for</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">this</span> bean go here <span style="list-style: inherit; color: rgb(103, 205, 204);">--</span><span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span><span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>bean<span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span></pre><h3 id="%E7%A4%BA%E4%BE%8B" name="%E7%A4%BA%E4%BE%8B" style="margin-top: 16px; margin-bottom: 8px; font-size: 14px; list-style: inherit; font-weight: bold; line-height: 24px; font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">示例</h3><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">让我们使用 Eclipse IDE 并按照以下步骤创建一个 Spring 应用程序 -</p><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;"></p><figure style="list-style: inherit; margin-top: 16px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;"><div style="list-style: inherit;"><span style="list-style: inherit;"><img src="https://ask8088-private-1251520898.cn-south.myqcloud.com/developer-images/article/6461686/zc4sjm3bpw.png?q-sign-algorithm=sha1&amp;q-ak=AKID2uZ1FGBdx1pNgjE3KK4YliPpzyjLZvug&amp;q-sign-time=1629450030;1629457230&amp;q-key-time=1629450030;1629457230&amp;q-header-list=&amp;q-url-param-list=&amp;q-signature=cf84522c5fde9d2b481d8891e9a06e9f5303d500" style="border-width: 0px; border-style: initial; list-style: inherit; display: block; margin-right: auto; margin-left: auto; max-width: 100%; max-height: 100%; box-shadow: rgba(3, 27, 78, 0.12) 0px 4px 8px; border-radius: 3px; cursor: zoom-in;"></span></div></figure><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">这是<strong style="list-style: inherit;">HelloWorld.java</strong>文件的内容</p><pre style="list-style: inherit; margin-top: 0.5em; margin-bottom: 0.5em; padding: 1em; color: rgb(204, 204, 204); background: rgb(80, 85, 107); border-radius: 3px; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace; overflow-wrap: normal; white-space: pre; word-break: normal; tab-size: 4; hyphens: none; font-size: 14px;"><span style="list-style: inherit; color: rgb(204, 153, 205);">package</span> com<span style="list-style: inherit;">.</span>tutorialspoint<span style="list-style: inherit;">;</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">public</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">class</span> <span style="list-style: inherit; color: rgb(248, 197, 85);">HelloWorld</span> <span style="list-style: inherit;">{</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">private</span> String message<span style="list-style: inherit;">;</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">public</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">void</span> <span style="list-style: inherit; color: rgb(240, 141, 73);">setMessage</span><span style="list-style: inherit;">(</span>String message<span style="list-style: inherit;">)</span><span style="list-style: inherit;">{</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">this</span><span style="list-style: inherit;">.</span>message <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> message<span style="list-style: inherit;">;</span> <span style="list-style: inherit;">}</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">public</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">void</span> <span style="list-style: inherit; color: rgb(240, 141, 73);">getMessage</span><span style="list-style: inherit;">(</span><span style="list-style: inherit;">)</span><span style="list-style: inherit;">{</span> System<span style="list-style: inherit;">.</span>out<span style="list-style: inherit;">.</span><span style="list-style: inherit; color: rgb(240, 141, 73);">println</span><span style="list-style: inherit;">(</span><span style="list-style: inherit; color: rgb(126, 198, 153);">"Your Message : "</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">+</span> message<span style="list-style: inherit;">)</span><span style="list-style: inherit;">;</span> <span style="list-style: inherit;">}</span> <span style="list-style: inherit;">}</span></pre><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">以下是<strong style="list-style: inherit;">MainApp.java</strong>文件的内容-</p><pre style="list-style: inherit; margin-top: 0.5em; margin-bottom: 0.5em; padding: 1em; color: rgb(204, 204, 204); background: rgb(80, 85, 107); border-radius: 3px; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace; overflow-wrap: normal; white-space: pre; word-break: normal; tab-size: 4; hyphens: none; font-size: 14px;"><span style="list-style: inherit; color: rgb(204, 153, 205);">package</span> com<span style="list-style: inherit;">.</span>tutorialspoint<span style="list-style: inherit;">;</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">import</span> org<span style="list-style: inherit;">.</span>springframework<span style="list-style: inherit;">.</span>context<span style="list-style: inherit;">.</span>ApplicationContext<span style="list-style: inherit;">;</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">import</span> org<span style="list-style: inherit;">.</span>springframework<span style="list-style: inherit;">.</span>context<span style="list-style: inherit;">.</span>support<span style="list-style: inherit;">.</span>ClassPathXmlApplicationContext<span style="list-style: inherit;">;</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">public</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">class</span> <span style="list-style: inherit; color: rgb(248, 197, 85);">MainApp</span> <span style="list-style: inherit;">{</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">public</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">static</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">void</span> <span style="list-style: inherit; color: rgb(240, 141, 73);">main</span><span style="list-style: inherit;">(</span>String<span style="list-style: inherit;">[</span><span style="list-style: inherit;">]</span> args<span style="list-style: inherit;">)</span> <span style="list-style: inherit;">{</span> ApplicationContext context <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">new</span> <span style="list-style: inherit; color: rgb(248, 197, 85);">ClassPathXmlApplicationContext</span><span style="list-style: inherit;">(</span><span style="list-style: inherit; color: rgb(126, 198, 153);">"Beans.xml"</span><span style="list-style: inherit;">)</span><span style="list-style: inherit;">;</span> HelloWorld objA <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit;">(</span>HelloWorld<span style="list-style: inherit;">)</span> context<span style="list-style: inherit;">.</span><span style="list-style: inherit; color: rgb(240, 141, 73);">getBean</span><span style="list-style: inherit;">(</span><span style="list-style: inherit; color: rgb(126, 198, 153);">"helloWorld"</span><span style="list-style: inherit;">)</span><span style="list-style: inherit;">;</span> objA<span style="list-style: inherit;">.</span><span style="list-style: inherit; color: rgb(240, 141, 73);">setMessage</span><span style="list-style: inherit;">(</span><span style="list-style: inherit; color: rgb(126, 198, 153);">"I'm object A"</span><span style="list-style: inherit;">)</span><span style="list-style: inherit;">;</span> objA<span style="list-style: inherit;">.</span><span style="list-style: inherit; color: rgb(240, 141, 73);">getMessage</span><span style="list-style: inherit;">(</span><span style="list-style: inherit;">)</span><span style="list-style: inherit;">;</span> HelloWorld objB <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit;">(</span>HelloWorld<span style="list-style: inherit;">)</span> context<span style="list-style: inherit;">.</span><span style="list-style: inherit; color: rgb(240, 141, 73);">getBean</span><span style="list-style: inherit;">(</span><span style="list-style: inherit; color: rgb(126, 198, 153);">"helloWorld"</span><span style="list-style: inherit;">)</span><span style="list-style: inherit;">;</span> objB<span style="list-style: inherit;">.</span><span style="list-style: inherit; color: rgb(240, 141, 73);">getMessage</span><span style="list-style: inherit;">(</span><span style="list-style: inherit;">)</span><span style="list-style: inherit;">;</span> <span style="list-style: inherit;">}</span> <span style="list-style: inherit;">}</span></pre><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">以下是原型范围所需的配置文件<strong style="list-style: inherit;">Beans.xml</strong> -</p><pre style="list-style: inherit; margin-top: 0.5em; margin-bottom: 0.5em; padding: 1em; color: rgb(204, 204, 204); background: rgb(80, 85, 107); border-radius: 3px; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace; overflow-wrap: normal; white-space: pre; word-break: normal; tab-size: 4; hyphens: none; font-size: 14px;"><span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span><span style="list-style: inherit; color: rgb(103, 205, 204);">?</span>xml version <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"1.0"</span> encoding <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"UTF-8"</span><span style="list-style: inherit; color: rgb(103, 205, 204);">?</span><span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span>beans xmlns <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"http://www.springframework.org/schema/beans"</span> xmlns<span style="list-style: inherit;">:</span>xsi <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"http://www.w3.org/2001/XMLSchema-instance"</span> xsi<span style="list-style: inherit;">:</span>schemaLocation <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> "http<span style="list-style: inherit;">:</span><span style="list-style: inherit; color: rgb(103, 205, 204);">/</span><span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>www<span style="list-style: inherit;">.</span>springframework<span style="list-style: inherit;">.</span>org<span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>schema<span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>beans http<span style="list-style: inherit;">:</span><span style="list-style: inherit; color: rgb(103, 205, 204);">/</span><span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>www<span style="list-style: inherit;">.</span>springframework<span style="list-style: inherit;">.</span>org<span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>schema<span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>beans<span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>spring<span style="list-style: inherit; color: rgb(103, 205, 204);">-</span>beans<span style="list-style: inherit; color: rgb(240, 141, 73);">-3.0</span><span style="list-style: inherit;">.</span>xsd"<span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span>bean id <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"helloWorld"</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">class</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"com.tutorialspoint.HelloWorld"</span> scope <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"prototype"</span><span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span><span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>bean<span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span><span style="list-style: inherit; color: rgb(103, 205, 204);">/</span>beans<span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span></pre><p style="margin-top: 0px; margin-bottom: 8px; list-style: inherit; min-height: 24px; color: rgb(51, 51, 51); font-family: &quot;pingfang SC&quot;, &quot;helvetica neue&quot;, arial, &quot;hiragino sans gb&quot;, &quot;microsoft yahei ui&quot;, &quot;microsoft yahei&quot;, simsun, sans-serif; white-space: pre-wrap;">完成源文件和 bean 配置文件的创建后,让我们运行应用程序。如果您的应用程序一切正常,它将打印以下消息 -</p><pre style="list-style: inherit; margin-top: 0.5em; margin-bottom: 0.5em; padding: 1em; color: rgb(204, 204, 204); background: rgb(80, 85, 107); border-radius: 3px; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace; overflow-wrap: normal; white-space: pre; word-break: normal; tab-size: 4; hyphens: none; font-size: 14px;">Your Message <span style="list-style: inherit;">:</span> I'm object A Your Message <span style="list-style: inherit;">:</span> <span style="list-style: inherit; color: rgb(204, 153, 205);">null</span></pre>
收藏(0)  分享
相关标签: spring认证 java
0个回复
  • 消灭零回复