diff --git a/README.en.md b/README.en.md new file mode 100644 index 0000000000000000000000000000000000000000..3a74545fd089c250b2cfdd2fe56ded20441dd70f --- /dev/null +++ b/README.en.md @@ -0,0 +1,100 @@ +# spring-ioc + +Example project demonstrating Spring IoC container implementation + +## Project Overview + +This project demonstrates two configuration approaches for Spring Framework's IoC (Inversion of Control) and DI (Dependency Injection): + +1. **Annotation-based** - Configuration using annotations such as `@Configuration`, `@ComponentScan`, `@Autowired`, etc. +2. **Declarative** - Configuration using an XML file (`applicationContext.xml`) for Bean management + +## Project Structure + +``` +src/main/java/com/example/ioc/ +├── annotation/ # Annotation-based configuration +│ ├── config/ +│ │ └── AppConfig.java +│ ├── controller/ +│ │ └── UserController.java +│ ├── dao/ +│ │ └── UserDao.java +│ ├── entity/ +│ │ └── User.java +│ └── service/ +│ └── UserService.java +│ +└── declarative/ # Declarative (XML) configuration + ├── controller/ + │ └── UserController.java + ├── dao/ + │ └── UserDao.java + ├── entity/ + │ └── User.java + └── service/ + └── UserService.java +``` + +## Technology Stack + +- Java +- Spring Framework + +## Quick Start + +### Compile the Project + +```bash +mvn clean compile +``` + +### Run the Examples + +**Annotation-based approach:** +```bash +mvn exec:java -Dexec.mainClass="com.example.ioc.annotation.Application" +``` + +**Declarative approach:** +```bash +mvn exec:java -Dexec.mainClass="com.example.ioc.declarative.Application" +``` + +## Feature Overview + +The project implements basic user management functionality: + +| Operation | Description | +|----------|-------------| +| Register User | Create and save a new user | +| Delete User | Delete a user by ID | +| Update User | Modify user information | +| Get Single User | Retrieve a user by ID | +| Get User List | Retrieve all users | + +## Dependency Injection Methods + +### Annotation-based Approach + +Dependencies are automatically injected using the `@Autowired` annotation: + +```java +@Autowired +private UserService userService; +``` + +### Declarative Approach + +Dependencies are injected via setter methods: + +```java +public void setUserService(UserService userService) { + this.userService = userService; +} +``` + +And configured in `applicationContext.xml`: + +```xml +``` \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..1dd24af65d8a8ec1ca3998ccd4fdbf808722b121 --- /dev/null +++ b/README.md @@ -0,0 +1,100 @@ + + +# spring-ioc + +Spring IoC 容器实现示例项目 + +## 项目简介 + +本项目演示了 Spring 框架 IoC(控制反转)和 DI(依赖注入)的两种配置方式: +1. **注解方式** - 使用 `@Configuration`、`@ComponentScan`、`@Autowired` 等注解进行配置 +2. **声明式方式** - 使用 XML 配置文件(`applicationContext.xml`)进行 Bean 管理 + +## 项目结构 + +``` +src/main/java/com/example/ioc/ +├── annotation/ # 注解方式配置 +│ ├── config/ +│ │ └── AppConfig.java +│ ├── controller/ +│ │ └── UserController.java +│ ├── dao/ +│ │ └── UserDao.java +│ ├── entity/ +│ │ └── User.java +│ └── service/ +│ └── UserService.java +│ +└── declarative/ # 声明式(XML)方式配置 + ├── controller/ + │ └── UserController.java + ├── dao/ + │ └── UserDao.java + ├── entity/ + │ └── User.java + └── service/ + └── UserService.java +``` + +## 技术栈 + +- Java +- Spring Framework + +## 快速开始 + +### 编译项目 + +```bash +mvn clean compile +``` + +### 运行示例 + +**注解方式:** +```bash +mvn exec:java -Dexec.mainClass="com.example.ioc.annotation.Application" +``` + +**声明式方式:** +```bash +mvn exec:java -Dexec.mainClass="com.example.ioc.declarative.Application" +``` + +## 功能说明 + +项目实现了基本的用户管理功能: + +| 操作 | 说明 | +|------|------| +| 注册用户 | 创建新用户并保存 | +| 删除用户 | 根据 ID 删除用户 | +| 更新用户 | 修改用户信息 | +| 查询单个用户 | 根据 ID 获取用户 | +| 用户列表 | 获取所有用户 | + +## 依赖注入方式 + +### 注解方式 + +通过 `@Autowired` 注解自动注入依赖: + +```java +@Autowired +private UserService userService; +``` + +### 声明式方式 + +通过 setter 方法注入依赖: + +```java +public void setUserService(UserService userService) { + this.userService = userService; +} +``` + +并在 `applicationContext.xml` 中配置: + +```xml