本文作为SpringIOC的入门指南,详细介绍基础概念、环境准备、工具安装等入门知识。从零开始,手把手教您掌握SpringIOC的基础操作,为后续深入学习打下坚实基础。
一、基础概念
1.1 什么是SpringIOC
1
| IOC(Inversion of Control)控制反转:
|
IOC是Spring框架的核心概念之一,是一种设计思想。传统方式中,对象之间的依赖关系由程序代码直接控制,而IOC将对象的创建和依赖关系的管理交给Spring容器来处理。
- 控制反转:将对象的控制权从应用程序代码转移到Spring容器
- 依赖注入(DI):IOC的实现方式,通过容器注入对象所需的依赖
- 解耦:降低对象之间的耦合度,提高代码的可维护性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class UserService { private UserRepository userRepository; public UserService() { this.userRepository = new UserRepositoryImpl(); } }
public class UserService { private UserRepository userRepository; public UserService(UserRepository userRepository) { this.userRepository = userRepository; } }
|
1.2 核心特性
- Bean管理:Spring容器负责创建、配置和管理Bean
- 依赖注入:自动注入对象所需的依赖
- 生命周期管理:管理Bean的创建、初始化和销毁
- 作用域管理:支持单例、原型等多种作用域
- 配置灵活:支持XML、注解、Java配置等多种方式
- 构造函数注入:通过构造函数注入依赖
- Setter注入:通过Setter方法注入依赖
- 字段注入:通过@Autowired注解直接注入字段
二、环境准备
2.1 系统要求
- JDK: JDK 8或更高版本
- IDE: IntelliJ IDEA / Eclipse
- 构建工具: Maven / Gradle
- Spring Framework 5.3+
- 或 Spring Boot 2.7+(内置Spring)
2.2 工具安装
1 2 3 4 5 6 7 8 9
| <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.21</version> </dependency> </dependencies>
|
1 2 3 4
| dependencies { implementation 'org.springframework:spring-context:5.3.21' }
|
三、快速开始
3.1 创建第一个IOC项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public class User { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
public class UserService { private UserRepository userRepository; public UserService(UserRepository userRepository) { this.userRepository = userRepository; } public User getUserById(Long id) { return userRepository.findById(id); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?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="userRepository" class="com.example.repository.UserRepositoryImpl"/> <bean id="userService" class="com.example.service.UserService"> <constructor-arg ref="userRepository"/> </bean> </beans>
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Configuration public class AppConfig { @Bean public UserRepository userRepository() { return new UserRepositoryImpl(); } @Bean public UserService userService(UserRepository userRepository) { return new UserService(userRepository); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @Configuration @ComponentScan(basePackages = "com.example") public class AppConfig { }
@Repository public class UserRepositoryImpl implements UserRepository { }
@Service public class UserService { @Autowired private UserRepository userRepository; }
|
3.2 运行示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = context.getBean(UserService.class); User user = userService.getUserById(1L); System.out.println(user); } }
|
四、常见问题
4.1 安装问题
- 检查类路径是否正确
- 检查Bean的构造函数参数
- 检查依赖的Bean是否存在
4.2 配置问题
1 2 3 4 5 6
| @Lazy @Autowired private ServiceB serviceB;
|
本文标题: SpringIOC入门篇
发布时间: 2022年01月23日 00:00
最后更新: 2025年12月30日 08:54
原始链接: https://haoxiang.eu.org/23c0ae81/
版权声明: 本文著作权归作者所有,均采用CC BY-NC-SA 4.0许可协议,转载请注明出处!