「Spring认证」Spring Hello World 项目示例

分享 0 518
IT胶囊
IT胶囊 LV1 2021年4月23日 19:45 发表
点击群号免费加入社区交流群: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;">让我们开始使用 Spring Framework 进行实际编程。在开始使用 Spring 框架编写第一个示例之前,您必须确保已按照Spring - 环境设置章节中的说明正确设置了 Spring 环境。我们还假设您对 Eclipse IDE 有一定的了解。</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 应用程序,它将打印“Hello World!” 或基于在 Spring Beans 配置文件中完成的配置的任何其他消息。</p><h2 id="%E6%AD%A5%E9%AA%A4-1---%E5%88%9B%E5%BB%BA-Java-%E9%A1%B9%E7%9B%AE" name="%E6%AD%A5%E9%AA%A4-1---%E5%88%9B%E5%BB%BA-Java-%E9%A1%B9%E7%9B%AE" 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;">步骤 1 - 创建 Java 项目</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;">第一步是使用 Eclipse IDE 创建一个简单的 Java 项目。按照选项<strong style="list-style: inherit;">文件 → 新建 → 项目</strong>,最后从向导列表中选择<strong style="list-style: inherit;">Java 项目</strong>向导。现在使用向导窗口将您的项目命名为<strong style="list-style: inherit;">HelloSpring</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;"></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/kzj9jlj911.jpg?q-sign-algorithm=sha1&amp;q-ak=AKID2uZ1FGBdx1pNgjE3KK4YliPpzyjLZvug&amp;q-sign-time=1629177150;1629184350&amp;q-key-time=1629177150;1629184350&amp;q-header-list=&amp;q-url-param-list=&amp;q-signature=46f52cd066ba9b847101a1bad43575149ee7f242" 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;">项目资源管理器</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;"></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/lmp56q1led.jpg?q-sign-algorithm=sha1&amp;q-ak=AKID2uZ1FGBdx1pNgjE3KK4YliPpzyjLZvug&amp;q-sign-time=1629177150;1629184350&amp;q-key-time=1629177150;1629184350&amp;q-header-list=&amp;q-url-param-list=&amp;q-signature=547056e9f4323cc9788f46093cbe6797ad1cd015" 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><h2 id="%E7%AC%AC-2-%E6%AD%A5---%E6%B7%BB%E5%8A%A0%E6%89%80%E9%9C%80%E7%9A%84%E5%BA%93" name="%E7%AC%AC-2-%E6%AD%A5---%E6%B7%BB%E5%8A%A0%E6%89%80%E9%9C%80%E7%9A%84%E5%BA%93" 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;">第 2 步 - 添加所需的库</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 Framework 和通用日志 API 库。为此,请右键单击您的项目名称<strong style="list-style: inherit;">HelloSpring</strong> ,然后按照上下文菜单中提供的以下选项进行操作 - <strong style="list-style: inherit;">Build Path → Configure Build Path</strong>以显示 Java Build Path 窗口,如下所示 -</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/inhdkwbyk3.jpg?q-sign-algorithm=sha1&amp;q-ak=AKID2uZ1FGBdx1pNgjE3KK4YliPpzyjLZvug&amp;q-sign-time=1629177150;1629184350&amp;q-key-time=1629177150;1629184350&amp;q-header-list=&amp;q-url-param-list=&amp;q-signature=dfe48c31e5bd7fb3c1ed0af8117a84434569e1fa" 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;">库”</strong>选项卡下可用的“<strong style="list-style: inherit;">添加外部 JAR”</strong>按钮从 Spring Framework 和 Common Logging 安装目录中添加以下核心 JAR -</p><ul style="margin-top: 0px; margin-bottom: 16px; margin-left: 0px; list-style: none; 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;"><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">commons-logging-1.1.1</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-aop-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-aspects-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-beans-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-context-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-context-support-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-core-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-expression-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-instrument-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-instrument-tomcat-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-jdbc-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-jms-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-messaging-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-orm-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-oxm-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-test-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-tx-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-web-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-webmvc-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-webmvc-portlet-4.1.6.RELEASE</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">spring-websocket-4.1.6.RELEASE</li></ul><h2 id="%E7%AC%AC-3-%E6%AD%A5---%E5%88%9B%E5%BB%BA%E6%BA%90%E6%96%87%E4%BB%B6" name="%E7%AC%AC-3-%E6%AD%A5---%E5%88%9B%E5%BB%BA%E6%BA%90%E6%96%87%E4%BB%B6" 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;">第 3 步 - 创建源文件</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;">现在让我们在<strong style="list-style: inherit;">HelloSpring</strong>项目下创建实际的源文件。首先,我们需要创建一个名为<strong style="list-style: inherit;">com.tutorialspoint</strong>的包。为此,请右键单击包资源管理器部分中的<strong style="list-style: inherit;">src</strong>并按照选项 -<strong style="list-style: inherit;">新建 → 包</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;">接下来我们将在 com.tutorialspoint 包下创建<strong style="list-style: inherit;">HelloWorld.java</strong>和<strong style="list-style: inherit;">MainApp.java</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;"></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/g3t4yfkprp.jpg?q-sign-algorithm=sha1&amp;q-ak=AKID2uZ1FGBdx1pNgjE3KK4YliPpzyjLZvug&amp;q-sign-time=1629177150;1629184350&amp;q-key-time=1629177150;1629184350&amp;q-header-list=&amp;q-url-param-list=&amp;q-signature=9d3ee55a894c0184ddf9c2eb859c57dfc032f0aa" 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 obj <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> obj<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;">关于主程序,需要注意以下两个要点 -</p><ul style="margin-top: 0px; margin-bottom: 16px; margin-left: 0px; list-style: none; 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;"><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">第一步是创建一个应用程序上下文,我们在其中使用了框架 API <strong style="list-style: inherit;">ClassPathXmlApplicationContext()</strong>。该 API 加载 bean 配置文件,并最终基于提供的 API,负责创建和初始化所有对象,即配置文件中提到的 bean。</li><li style="margin-bottom: 4px; padding-left: 18px; list-style-type: none; position: relative;">第二步用于使用创建的上下文的<strong style="list-style: inherit;">getBean()</strong>方法获取所需的 bean 。该方法使用 bean ID 返回一个通用对象,最终可以将其强制转换为实际对象。一旦你有了一个对象,你就可以使用这个对象来调用任何类方法。</li></ul><h2 id="%E7%AC%AC-4-%E6%AD%A5---%E5%88%9B%E5%BB%BA-Bean-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6" name="%E7%AC%AC-4-%E6%AD%A5---%E5%88%9B%E5%BB%BA-Bean-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6" 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;">第 4 步 - 创建 Bean 配置文件</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 配置文件,该文件是一个 XML 文件并充当将 Bean(即类)粘合在一起的粘合剂。该文件需要在<strong style="list-style: inherit;">src</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;"></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/s07vw32815.jpg?q-sign-algorithm=sha1&amp;q-ak=AKID2uZ1FGBdx1pNgjE3KK4YliPpzyjLZvug&amp;q-sign-time=1629177150;1629184350&amp;q-key-time=1629177150;1629184350&amp;q-header-list=&amp;q-url-param-list=&amp;q-signature=77252277c679c6a6848990aeaa21e8578859bf79" 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;">Beans.xml</strong>,但您可以独立选择您喜欢的任何名称。您必须确保此文件在 CLASSPATH 中可用,并在主应用程序中使用相同的名称,同时创建 MainApp.java 文件中所示的应用程序上下文。</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;">Beans.xml 用于为不同的 bean 分配唯一的 ID,并控制具有不同值的对象的创建,而不会影响任何 Spring 源文件。例如,使用以下文件,您可以为“message”变量传递任何值,并且可以打印不同的消息值,而不会影响 HelloWorld.java 和 MainApp.java 文件。让我们看看它是如何工作的 -</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><span style="list-style: inherit; color: rgb(103, 205, 204);">&gt;</span> <span style="list-style: inherit; color: rgb(103, 205, 204);">&lt;</span>property name <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"message"</span> value <span style="list-style: inherit; color: rgb(103, 205, 204);">=</span> <span style="list-style: inherit; color: rgb(126, 198, 153);">"Hello World!"</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><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;">当 Spring 应用程序被加载到内存中时,Framework 使用上面的配置文件来创建所有定义的 bean,并为它们分配一个在<strong style="list-style: inherit;">&lt;bean&gt;</strong>标签中定义的唯一 ID 。您可以使用<strong style="list-style: inherit;">&lt;property&gt;</strong>标签来传递创建对象时使用的不同变量的值。</p><h2 id="%E7%AC%AC-5-%E6%AD%A5---%E8%BF%90%E8%A1%8C%E7%A8%8B%E5%BA%8F" name="%E7%AC%AC-5-%E6%AD%A5---%E8%BF%90%E8%A1%8C%E7%A8%8B%E5%BA%8F" 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;">第 5 步 - 运行程序</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 配置文件的创建,您就可以开始这一步了,即编译和运行您的程序。为此,请保持 MainApp.Java 文件选项卡处于活动状态,并使用Eclipse IDE 中提供的<strong style="list-style: inherit;">运行</strong>选项或使用<strong style="list-style: inherit;">Ctrl + F11</strong>来编译和运行<strong style="list-style: inherit;">MainApp</strong>应用程序。如果您的应用程序一切正常,这将在 Eclipse IDE 的控制台中打印以下消息 -</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> Hello World<span style="list-style: inherit; color: rgb(103, 205, 204);">!</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;">恭喜,您已经成功创建了您的第一个 Spring 应用程序。通过更改“message”属性的值并保持两个源文件不变,您可以看到上述 Spring 应用程序的灵活性。</p>
收藏(0)  分享
相关标签: java
0个回复
  • 消灭零回复