java
主页 > 软件编程 > java >

Spring Boot 常用注解详解与使用最佳实践建议

2025-05-11 | 佚名 | 点击:

一、核心启动注解

1. @SpringBootApplication

1

2

3

4

5

6

@SpringBootApplication

public class MyApplication {

    public static void main(String[] args) {

        SpringApplication.run(MyApplication.class, args);

    }

}

2. @EnableAutoConfiguration

3. @Configuration

1

2

3

4

5

6

7

@Configuration

public class AppConfig {

    @Bean

    public MyService myService() {

        return new MyServiceImpl();

    }

}

4. @ComponentScan

1

2

3

4

5

@SpringBootApplication

@ComponentScan({"com.example.main", "com.example.controllers"})

public class MyApplication {

    // ...

}

二、Bean定义与管理

1. @Bean

1

2

3

4

5

6

7

@Configuration

public class AppConfig {

    @Bean

    public RestTemplate restTemplate() {

        return new RestTemplate();

    }

}

2. @Component/@Service/@Repository/@Controller

1

2

3

4

5

6

7

8

@Service

public class UserServiceImpl implements UserService {

    // 业务逻辑

}

@Repository

public class UserRepositoryImpl implements UserRepository {

    // 数据访问逻辑

}

3. @ConfigurationProperties

1

2

3

4

5

6

@ConfigurationProperties(prefix = "app")

public class AppProperties {

    private String name;

    private String version;

    // getters/setters

}

4. @Scope

1

2

3

4

5

@Bean

@Scope("prototype")

public MyPrototypeBean myPrototypeBean() {

    return new MyPrototypeBean();

}

三、依赖注入

1. @Autowired

1

2

3

4

5

@Service

public class UserService {

    @Autowired

    private UserRepository userRepository;

}

2. @Qualifier

1

2

3

@Autowired

@Qualifier("primaryDataSource")

private DataSource dataSource;

3. @Value

1

2

3

4

3. @Value

作用:注入属性值

使用场景:注入简单配置值

示例:

四、Web MVC开发

1. @RestController/@Controller

1

2

3

4

5

6

7

8

@RestController

@RequestMapping("/api/users")

public class UserController {

    @GetMapping("/{id}")

    public User getUser(@PathVariable Long id) {

        // ...

    }

}

2. @RequestMapping/@GetMapping/@PostMapping等

1

2

3

4

@PostMapping("/create")

public ResponseEntity<User> createUser(@RequestBody UserDto userDto) {

    // ...

}

3. @RequestBody/@ResponseBody

1

2

3

4

@PostMapping

public User create(@RequestBody User user) {

    return userService.save(user);

}

4. @PathVariable/@RequestParam

1

2

3

4

@GetMapping("/search")

public List<User> searchUsers(@RequestParam String keyword) {

    // ...

}

五、数据访问

1. @Entity/@Table

1

2

3

4

5

6

7

8

@Entity

@Table(name = "users")

public class User {

    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private Long id;

    // ...

}

2. @Transactional

1

2

3

4

5

6

7

@Service

public class UserService {

    @Transactional

    public void updateUser(User user) {

        // ...

    }

}

3. @RepositoryRestResource

作用:将JPA仓库暴露为REST端点

使用场景:快速开发RESTful数据服务

示例:

六、测试相关

1. @SpringBootTest

1

2

3

4

5

6

@SpringBootTest

class MyIntegrationTests {

    @Autowired

    private MyService myService;

    // 测试方法

}

2. @WebMvcTest

1

2

3

4

5

6

@WebMvcTest(UserController.class)

class UserControllerTests {

    @Autowired

    private MockMvc mockMvc;

    // 测试方法

}

七、高级特性

1. @EnableCaching/@Cacheable

1

2

3

4

5

6

7

@Service

public class UserService {

    @Cacheable("users")

    public User getUser(Long id) {

        // 只有第一次会执行,后续从缓存获取

    }

}

2. @EnableScheduling/@Scheduled

1

2

3

4

5

6

7

@Component

public class MyScheduler {

    @Scheduled(fixedRate = 5000)

    public void doTask() {

        // 每5秒执行一次

    }

}

3. @Async

1

2

3

4

5

6

7

@Service

public class AsyncService {

    @Async

    public void asyncMethod() {

        // 异步执行

    }

}

最佳实践建议

原文链接:
相关文章
最新更新