[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

相关文章

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录