Spring Boot 提供了很多实用的注解,它们能极大地简化开发流程。以下是一些常用注解及其作用的详细介绍:

启动类相关注解

  • @SpringBootApplication:这是一个组合注解,整合了 @SpringBootConfiguration@EnableAutoConfiguration@ComponentScan 三个注解。
    • @SpringBootConfiguration:表明该类是 Spring Boot 的配置类,本质上等同于 @Configuration
    • @EnableAutoConfiguration:开启 Spring Boot 的自动配置功能,Spring Boot 会依据类路径中的依赖自动配置应用。
    • @ComponentScan:指定 Spring 扫描组件的路径,像 @Component@Service@Repository 等注解标注的类都会被扫描到。
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}


### 组件注解
- **@Component**:这是一个通用的组件注解,用于将一个类标记为 Spring 组件,这样 Spring 就会自动扫描并将其纳入到应用上下文中。
- **@Service**:通常用于标记服务层的类,本质上是 @Component 的特殊化,目的是让代码的语义更清晰。
```java
import org.springframework.stereotype.Service;

@Service
public class UserService {
    public String getUser() {
        return "User info";
    }
}</code></pre>
<ul>
<li><strong><code>@Repository</code></strong>:主要用于标记数据访问层的类,也是 <code>@Component</code> 的特殊化,可让 Spring 自动处理数据访问异常。
<pre><code class="language-java">
import org.springframework.stereotype.Repository;</code></pre></li>
</ul>
<p>@Repository
public class UserRepository {
public void saveUser() {
// 保存用户逻辑
}
}</p>
<pre><code>- **<code>@Controller</code>**:用于标记控制器类,表明该类会处理 HTTP 请求,同样是 <code>@Component</code> 的特殊化。
```java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
    @GetMapping("/user")
    @ResponseBody
    public String getUser() {
        return "User info";
    }
}

配置类注解

  • @Configuration:标记一个类为配置类,等同于传统的 XML 配置文件,类中可以定义多个 @Bean 方法。
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
@Bean
public UserService userService() {
return new UserService();
}
}

- **@Bean**:用于在配置类中定义一个 Bean,方法的返回值会被注册到 Spring 容器中。

### 依赖注入注解
- **@Autowired**:用于自动注入依赖的 Bean,可作用于构造函数、字段、方法等。
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    private UserRepository userRepository;

    @Autowired
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
}</code></pre>
<ul>
<li><strong><code>@Qualifier</code></strong>:当有多个相同类型的 Bean 时,可使用 <code>@Qualifier</code> 指定要注入的 Bean 的名称。
<pre><code class="language-java">
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;</code></pre></li>
</ul>
<p>@Service
public class UserService {
private UserRepository userRepository;</p>
<pre><code>@Autowired
public UserService(@Qualifier("userRepositoryImpl") UserRepository userRepository) {
    this.userRepository = userRepository;
}</code></pre>
<p>}</p>
<pre><code>- **<code>@Resource</code>**:这是 JSR - 250 规范的注解,也用于依赖注入,可通过 <code>name</code> 属性指定要注入的 Bean 的名称。

### 切面编程注解
- **<code>@Aspect</code>**:用于标记一个类为切面类,该类中可以定义切点和通知。
```java
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {
    @Pointcut("execution(* com.example.service.*.*(..))")
    public void serviceMethods() {}

    @Before("serviceMethods()")
    public void beforeServiceMethod() {
        System.out.println("Before service method");
    }
}
  • @Pointcut:用于定义切点,指定哪些方法会被增强。
  • @Before@After@Around 等:这些是通知注解,分别表示在目标方法执行前、执行后、环绕执行时进行增强。

配置属性注解

  • @ConfigurationProperties:用于将配置文件中的属性绑定到一个 Java Bean 上。
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "myapp")
public class MyAppProperties {
private String name;
private int age;

// Getters and Setters
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

}

- **@EnableConfigurationProperties**:用于启用 @ConfigurationProperties 注解的类。

### 测试注解
- **@SpringBootTest**:用于在 Spring Boot 应用中进行集成测试,会加载完整的应用上下文。
```java
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class UserServiceTest {
    @Autowired
    private UserService userService;

    @Test
    public void testGetUser() {
        String user = userService.getUser();
        // 断言逻辑
    }
}
  • @MockBean:在测试中用于创建一个 Mock 对象来替换 Spring 容器中的真实 Bean。