Spring 框架支持多种方式的依赖注入,其中注解方式是现代 Spring 开发中非常常用和推荐的方式。注解不仅简化了配置,还使得代码更清晰,更易于理解和维护。本文将详细介绍如何使用注解实现 Spring 依赖注入。
一、依赖注入概述
依赖注入(Dependency Injection, DI)是一种设计模式,它将对象的依赖交给外部容器管理,而不是在对象内部自行创建这些依赖。Spring 框架通过 IoC(控制反转)容器实现了依赖注入,允许开发者轻松地管理对象及其依赖关系。
二、注解注入概述
Spring 提供了一系列注解,用于实现依赖注入。常用的注解包括:
@Autowired
@Resource
@Inject
1. @Autowired
@Autowired
是 Spring 提供的注解,用于自动装配 Bean。它可以用在构造函数、Setter 方法和字段上。
2. @Resource
@Resource
是 JSR-250 提供的注解,Spring 对其进行了支持。它可以通过名称或类型注入 Bean。
3. @Inject
@Inject
是 JSR-330 提供的注解,与 @Autowired
类似,但更标准化。
三、使用注解实现依赖注入
1. 项目结构
一个典型的 Spring 项目结构如下:
my-spring-annotation-injection-project/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ └── project/
│ │ │ ├── config/
│ │ │ │ └── AppConfig.java
│ │ │ ├── service/
│ │ │ │ ├── ExampleService.java
│ │ │ │ └── AnotherService.java
│ │ │ └── controller/
│ │ │ └── ExampleController.java
│ └── resources/
│ └── application.properties
└── pom.xml
2. Java 类定义
// ExampleService.java
package com.example.project.service;
import org.springframework.stereotype.Service;
@Service
public class ExampleService {
public String serve() {
return "Service is serving";
}
}
// AnotherService.java
package com.example.project.service;
import org.springframework.stereotype.Service;
@Service
public class AnotherService {
public String assist() {
return "Another service is assisting";
}
}
// ExampleController.java
package com.example.project.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.example.project.service.ExampleService;
import com.example.project.service.AnotherService;
@Controller
public class ExampleController {
private final ExampleService exampleService;
private final AnotherService anotherService;
@Autowired
public ExampleController(ExampleService exampleService, AnotherService anotherService) {
this.exampleService = exampleService;
this.anotherService = anotherService;
}
public void doSomething() {
System.out.println(exampleService.serve());
System.out.println(anotherService.assist());
}
}
3. 配置类
使用 @Configuration
和 @ComponentScan
注解定义配置类和组件扫描。
// AppConfig.java
package com.example.project.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.example.project")
public class AppConfig {
}
4. 启动类
Spring Boot 项目通常包含一个启动类,用于启动 Spring 应用。
// Application.java
package com.example.project;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
5. application.properties
配置文件用于配置应用程序参数,如服务器端口、数据库连接等。
server.port=8080
6. 测试类
// ApplicationTests.java
package com.example.project;
import com.example.project.controller.ExampleController;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class ApplicationTests {
@Autowired
private ExampleController exampleController;
@Test
public void testDoSomething() {
exampleController.doSomething();
}
}
四、总结
使用注解实现 Spring 依赖注入是一种简洁、高效的方法。通过 @Autowired
、@Resource
和 @Inject
注解,可以轻松地在构造函数、Setter 方法和字段上注入依赖。注解注入不仅简化了配置,还使代码更清晰,更易于理解和维护。