[Spring实战系列] - No.1 使用Spring 写一个 hello world

2024-04-12 14:48

本文主要是介绍[Spring实战系列] - No.1 使用Spring 写一个 hello world,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

写在前面 :这是我 [Spring 实战] 系列文章的第一篇,[Spring 实战]将是本博客第一个可能会长期写下去的专栏,主要就是分享我在学习Spring 实战这本书以及Spring相关资料的感受,经历,以及Code。我的想法是我会尽可能的展示代码和截图以及很少的文字 提高文章可阅读行。


 接下来我会展示如何使用Spring 写一个很简单的HelloWorld程序
      1. 使用intellij 创建一个spring项目(你也可以使用其他的任意java IDE 然后引入spring 相关的一些jar包,在这里我们使用intellij 推荐!)


2.这个HelloWorld需要什么?
Interface :GreetingService
Class:GreetingServiceImpl
MainClass:HelloApp

配置Bean:hello.xml

package main;/*** Created by YanMing on 2017/2/19.*/
public interface GreetingService {void sayGreeting();
}

package main;/*** Created by YanMing on 2017/2/19.*/
public class GreetingServiceImpl implements GreetingService{private String greeting;public GreetingServiceImpl() {}public GreetingServiceImpl(String greeting) {this.greeting = greeting;}@Overridepublic void sayGreeting() {System.out.println(greeting);}public void setGreeting(String greeting) {this.greeting = greeting;}
}
package main;/*** Created by YanMing on 2017/2/19.*/
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloApp {public static void main(String[]args) throws Exception{ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("helloworld.xml");GreetingService greetingService = (GreetingService) context.getBean("greetingService");greetingService.sayGreeting();}
}

3.什么是Bean?为什么要配置Bean呢?

Bean,我们通俗的来解释就是你创建的打算交给Spring进行管理,并且在使用的时候通过注入的方式直接申
的类。在后面的文章中我们会提到Ioc特性以及DI(动态注入),届时我们会详细的讲解Bean。
所以我们只需要按照某种方式,把我们的自己创建的类告诉Spring,让他去接纳我们的类,并且进行管理和
实例化对象。那么我们需要通过什么方式呢?XML!(Spring 同时支持通过java的方式来配置,本文不做讲解)

在你的项目里创建hello.xml文件,在intellij中你会得到如下文档:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"></beans>

这是IDE直接为你添加的Spring 配置使用的xml模板。在beans标签中添加以下新增代码,完成xml。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="greetingService" class="main.GreetingServiceImpl"><property name="greeting" value="Hello World!"></property></bean>
</beans>



至此,我们的HelloWorld已经完成了。右击HelloApp,运行代码


3.刚刚发生了什么? 接口函数以及类实现都很简单。那么在HelloApp 以及 Hello.xml中发生了什么呢? 正如我们前面所说的,我们通过xml文件,以某种方式在Spring中配置了我们的Bean,Spring负责管理和实 例化我们的对象。在HelloApp中,我们有如下代码:
 ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("helloworld.xml");GreetingService greetingService = (GreetingService) context.getBean("greetingService");greetingService.sayGreeting();



这段代码,就是我们从Spring的BeanFactory中,获得我们的对象的代码。仿佛是我们找到了Hello.xml文件
然后,我们创建了一个对象并执行对象的sayGreeting()。就好像我们最熟悉的方式:
 GreetingServiceImpl greetingService1 =new GreetingServiceImpl("Hello World!");

所以Spring是让我们变得复杂了么?答案自然是:No! 接下来,我们会讲到,Spring 通过何种方式对我们的代码进行解耦合,使得代码可以复用。


Github源码点击下载

这篇关于[Spring实战系列] - No.1 使用Spring 写一个 hello world的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/897437

相关文章

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解