java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring Boot 常用注解

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

作者:上官箫羽

这篇文章主要介绍了Spring Boot 常用注解详解与使用最佳实践建议,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

一、核心启动注解

1. @SpringBootApplication

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

2. @EnableAutoConfiguration

3. @Configuration

@Configuration
public class AppConfig {
    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}

4. @ComponentScan

@SpringBootApplication
@ComponentScan({"com.example.main", "com.example.controllers"})
public class MyApplication {
    // ...
}

二、Bean定义与管理

1. @Bean

@Configuration
public class AppConfig {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

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

@Service
public class UserServiceImpl implements UserService {
    // 业务逻辑
}
@Repository
public class UserRepositoryImpl implements UserRepository {
    // 数据访问逻辑
}

3. @ConfigurationProperties

@ConfigurationProperties(prefix = "app")
public class AppProperties {
    private String name;
    private String version;
    // getters/setters
}

4. @Scope

@Bean
@Scope("prototype")
public MyPrototypeBean myPrototypeBean() {
    return new MyPrototypeBean();
}

三、依赖注入

1. @Autowired

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
}

2. @Qualifier

@Autowired
@Qualifier("primaryDataSource")
private DataSource dataSource;

3. @Value

3. @Value
作用:注入属性值
使用场景:注入简单配置值
示例:

四、Web MVC开发

1. @RestController/@Controller

@RestController
@RequestMapping("/api/users")
public class UserController {
    @GetMapping("/{id}")
    public User getUser(@PathVariable Long id) {
        // ...
    }
}

2. @RequestMapping/@GetMapping/@PostMapping等

@PostMapping("/create")
public ResponseEntity<User> createUser(@RequestBody UserDto userDto) {
    // ...
}

3. @RequestBody/@ResponseBody

@PostMapping
public User create(@RequestBody User user) {
    return userService.save(user);
}

4. @PathVariable/@RequestParam

@GetMapping("/search")
public List<User> searchUsers(@RequestParam String keyword) {
    // ...
}

五、数据访问

1. @Entity/@Table

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    // ...
}

2. @Transactional

@Service
public class UserService {
    @Transactional
    public void updateUser(User user) {
        // ...
    }
}

3. @RepositoryRestResource

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

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

示例

六、测试相关

1. @SpringBootTest

@SpringBootTest
class MyIntegrationTests {
    @Autowired
    private MyService myService;
    // 测试方法
}

2. @WebMvcTest

@WebMvcTest(UserController.class)
class UserControllerTests {
    @Autowired
    private MockMvc mockMvc;
    // 测试方法
}

七、高级特性

1. @EnableCaching/@Cacheable

@Service
public class UserService {
    @Cacheable("users")
    public User getUser(Long id) {
        // 只有第一次会执行,后续从缓存获取
    }
}

2. @EnableScheduling/@Scheduled

@Component
public class MyScheduler {
    @Scheduled(fixedRate = 5000)
    public void doTask() {
        // 每5秒执行一次
    }
}

3. @Async

@Service
public class AsyncService {
    @Async
    public void asyncMethod() {
        // 异步执行
    }
}

最佳实践建议

到此这篇关于Spring Boot 常用注解详解与使用指南的文章就介绍到这了,更多相关Spring Boot 常用注解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文