用最通俗的方式理解 Spring —— Java 世界的"万能管家"
new 菜()new 刀()new 锅()手动销毁getBean("鱼香肉丝")对象不用自己 new,Spring 帮你创建和管理
类似:你不用自己做饭,餐厅帮你做
日志、安全检查等自动加上,不用每个方法都写
类似:门卫自动登记,不用每个人都手写签到
Spring 是模块化的,像乐高积木一样,需要哪块用哪块。点击卡片查看详情:
本章和后续几章主要学核心容器(IoC 和 DI),这是 Spring 的根基。Web 和数据库部分后面章节会专门讲。
按照下面的步骤,一步一步来。复制粘贴就能运行!
选择 Maven,GroupId 填 com.example,ArtifactId 填 spring-hello,点 Create
打开项目根目录的 pom.xml,在 <project> 标签内添加:
<!-- 📄 pom.xml — Maven 依赖配置 -->
<dependencies>
<!-- Spring 核心容器: 包含 IoC 容器、Bean 管理、注解支持 -->
<!-- 引入这一个就够了,会自动拉取 spring-core、spring-beans 等 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.30</version>
</dependency>
</dependencies>// 📄 HelloWorld.java — 一个普通的 Java 类(Bean)
package com.example.spring;
// 注意: 这里没有任何 Spring 注解!
// 它就是一个普通的 Java 类,由 XML 配置文件告诉 Spring 来管理它
public class HelloWorld {
private String message; // 属性,将由 Spring 通过 setter 赋值
// ⚠️ 必须有 setter!Spring XML 配置的 <property> 标签会调用它
public void setMessage(String message) {
this.message = message;
}
public void showMessage() {
System.out.println("Spring 说: " + message);
}
}applicationContext.xml<!-- 📄 applicationContext.xml — Spring XML 配置文件 -->
<?xml version="1.0" encoding="UTF-8"?>
<!-- 这是 Spring 的“菜单”,告诉容器要创建哪些对象、怎么配置 -->
<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="helloWorld": Bean 的名字,后面用 getBean("helloWorld") 取它 -->
<!-- class: 告诉 Spring 创建哪个类的实例 -->
<bean id="helloWorld" class="com.example.spring.HelloWorld">
<!-- property: 调用 setMessage() 方法,把值设置进去 -->
<property name="message" value="Hello Spring!"/>
</bean>
</beans>// 📄 MainApp.java — 启动入口(XML 方式)
package com.example.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
// 第1步: 启动 Spring 容器
// 读取 classpath 下的 XML 配置文件,创建并管理所有 Bean
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
// 第2步: 从容器中获取对象
// 注意: 我们没有写 new HelloWorld()!对象是容器创建的
// "helloWorld" 对应 XML 中 <bean id="helloWorld">
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
// 第3步: 直接使用
// message 已经被 Spring 通过 <property> 设置好了!
obj.showMessage(); // 输出: Spring 说: Hello Spring!
}
}右键 MainApp.java → Run 'MainApp.main()',你会看到:
Spring 说: Hello Spring!new HelloWorld()!上面的 XML 方式太啰嗦了!现代开发中 99% 用注解方式,更简洁、更直观。下面同样是复制粘贴就能跑!
<bean id="helloWorld"
class="com.example.HelloWorld">
<property name="message"
value="Hello!"/>
</bean>@Component // 告诉Spring:管理我!
public class HelloWorld {
private String message
= "Hello Spring!";
}如果新建:IDEA → New Project → Maven,GroupId: com.example,ArtifactId: spring-annotation。pom.xml 依赖与上面一样。
// 📄 AppConfig.java — 配置类(替代 XML 配置文件)
package com.example.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
// @Configuration: 标记为配置类,等价于一个 XML 配置文件
// @ComponentScan: Spring 启动时会扫描这个包下所有带 @Component 的类
@Configuration
@ComponentScan(basePackages = "com.example.spring")
public class AppConfig {
// 不需要写任何代码!
// Spring 自动扫描包下所有 @Component 标记的类,创建并管理它们
}// 📄 HelloWorld.java — 注解方式的 Bean
package com.example.spring;
import org.springframework.stereotype.Component;
// @Component: 告诉 Spring“请管理我”
// Spring 扫描时看到这个注解,就会自动创建这个类的实例放入容器
// 不需要写 XML,一个注解搞定!
@Component
public class HelloWorld {
// 直接赋初始值(不再需要 XML 的 <property>)
private String message = "Hello Spring with Annotations!";
public void showMessage() {
System.out.println("Spring 说: " + message);
}
}// 📄 MainApp.java — 启动入口(注解方式)
package com.example.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainApp {
public static void main(String[] args) {
// 用 AnnotationConfigApplicationContext 替代 ClassPathXmlApplicationContext
// 传入配置类 → Spring 读取 @ComponentScan → 自动扫描并创建 Bean
ApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
// 从容器获取 Bean(按类型获取,不需要强转)
// 没有 new!Spring 自动创建的
HelloWorld obj = context.getBean(HelloWorld.class);
obj.showMessage(); // 输出: Spring 说: Hello Spring with Annotations!
}
}右键 MainApp.java → Run 'MainApp.main()',你会看到:
Spring 说: Hello Spring with Annotations!applicationContext.xml 配置文件了@Component 就能让 Spring 管理,不用写 <bean> 标签@Configuration + @ComponentScan 替代整个 XML| 注解 | 含义 | 白话翻译 |
|---|---|---|
@Component | 通用组件 | "Spring 你来管我" |
@Service | 业务逻辑层 | "我是干活的" |
@Repository | 数据访问层 | "我负责存数据" |
@Controller | 控制器层 | "我接收用户请求" |
@Configuration | 配置类 | "我是菜单/说明书" |
点击按钮模拟 Spring 容器的工作过程,观察对象是如何被创建和管理的
点击按钮,看两种方式写代码的区别
做完这 3 道题,确认你理解了本章核心知识点!
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
UserService service = ctx.getBean(UserService.class); // 没有 new!先点左边的注解,再点右边对应的作用