java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot注解使用

Java springboot里注解大全和使用指南(最新整理)

作者:奋力向前123

在Java Spring Boot中,注解是简化开发、提高效率的关键工具,这篇文章给大家介绍Java springboot里注解大全和使用指南,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

在 Java Spring Boot 中,注解是简化开发、提高效率的关键工具。以下是一些核心注解及其使用指南,按功能分类整理:

一、核心启动与配置注解

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

@Configuration

@Configuration
public class AppConfig {
    @Bean
    public DataSource dataSource() {
        // 配置并返回一个数据源 Bean
        return new HikariDataSource();
    }
}
@SpringBootApplication(scanBasePackages = "com.example.demo")
public class DemoApplication {
    // ...
}

二、组件注册注解

@Component
public class ToolUtil {
    public String formatStr(String str) {
        return str.trim().toUpperCase();
    }
}

@Service

@Service
public class UserService {
    public User getUserById(Integer id) {
        // 实际场景中会调用 Repository 层查询数据库
        User user = new User();
        user.setId(id);
        user.setName("李四");
        return user;
    }
}

@Repository

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
    User findByName(String name);
}

@Controller

@Controller
@RequestMapping("/user")
public class UserController {
    @GetMapping("/info")
    public String userInfo(Model model) {
        model.addAttribute("username", "张三");
        return "user-info"; // 返回视图名
    }
}

@RestController

@RestController
@RequestMapping("/api/user")
public class UserRestController {
    @GetMapping("/info")
    public User getUserInfo() {
        User user = new User();
        user.setId(1);
        user.setName("张三");
        return user; // 直接返回 User 对象,会自动转为 JSON
    }
}

三、依赖注入注解

@RestController
public class UserRestController {
    private final UserService userService;
    @Autowired
    public UserRestController(UserService userService) {
        this.userService = userService;
    }
    @GetMapping("/info")
    public User getUserInfo() {
        return userService.getUserById(1);
    }
}

@Qualifier

@RestController
public class UserRestController {
    private final UserService userService;
    @Autowired
    @Qualifier("userServiceImpl") // 指定注入的 Bean 名称
    public UserRestController(UserService userService) {
        this.userService = userService;
    }
    // ...
}

@Value

@Component
public class AppConfig {
    @Value("${server.port}")
    private int port;

    // ...
}

四、Web 开发注解

@RestController
@RequestMapping("/api/user")
public class UserRestController {
    @GetMapping("/info")
    public User getUserInfo() {
        // ...
    }
    @PostMapping("/create")
    public User createUser(@RequestBody User user) {
        // ...
    }
}

@GetMapping@PostMapping@PutMapping@DeleteMapping

@RestController
@RequestMapping("/api/user")
public class UserRestController {
    @GetMapping("/info")
    public User getUserInfo() {
        // ...
    }
    @PostMapping("/create")
    public User createUser(@RequestBody User user) {
        // ...
    }
}

@RequestParam

@GetMapping("/search")
public List<User> searchUsers(@RequestParam String name, @RequestParam(defaultValue = "10") int size) {
    // ...
}

@PathVariable

@GetMapping("/user/{id}")
public User getUserById(@PathVariable Long id) {
    // ...
}

@RequestBody

@PostMapping("/create")
public User createUser(@RequestBody User user) {
    // ...
}

@ResponseBody

@Controller
public class UserController {
    @GetMapping("/info")
    @ResponseBody
    public User getUserInfo() {
        User user = new User();
        user.setId(1);
        user.setName("张三");
        return user;
    }
}

到此这篇关于Java springboot里注解大全和使用指南(最新整理)的文章就介绍到这了,更多相关 springboot注解使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

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