Springboot注解总结

Spring Boot是一个非常流行的Java框架,用于创建微服务。它提供了快速开发、配置简化和生产就绪功能的优点。在Spring Boot中,注解(Annotations)扮演了非常重要的角色,它们提供了元数据,允许开发人员以声明的方式编写少量的代码来实现大量的功能。下面是一些Spring Boot中最常用的注解及其代码示例。

目录

1. @SpringBootApplication

2. @RestController

3. @RequestMapping 和 @GetMapping / @PostMapping

4. @Autowired

5. @Service, @Repository, @Component

1. @SpringBootApplication

这是Spring Boot的核心注解,用于启动Spring应用的自动配置。它是一个方便的注解,结合了@Configuration@EnableAutoConfiguration@ComponentScan注解的功能。

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);
    }
}

2. @RestController

这个注解是@Controller@ResponseBody注解的组合,用于创建RESTful web服务。使用@RestController,返回的数据直接写入HTTP响应体中,通常是JSON或XML格式。

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, World!";
    }
}

3. @RequestMapping 和 @GetMapping / @PostMapping

@RequestMapping注解用于映射web请求到Spring Controller的方法。@GetMapping@PostMapping@RequestMapping的专化版本,分别用于处理HTTP GET和POST请求。

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
public class UserController {

    @GetMapping("/")
    public List<User> getAllUsers() {
        // 逻辑处理
    }

    @PostMapping("/")
    public User createUser(User user) {
        // 逻辑处理
    }
}

4. @Autowired

@Autowired注解用于自动装配Spring容器中的beans。它可以用在字段、构造函数和方法上。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    private final UserRepository userRepository;

    @Autowired
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
}

5. @Service, @Repository, @Component

这些注解用于表示Spring管理的各种组件。@Service用于标注服务层组件,@Repository用于标注数据访问组件,而@Component是一个通用的组件注解,可用于任何Spring管理的组件。

import org.springframework.stereotype.Service;

@Service
public class MyService {
    // 服务层逻辑
}

import org.springframework.stereotype.Repository;

@Repository
public interface MyRepository extends JpaRepository<MyEntity, Long> {
    // 数据访问方法
}

import org.springframework.stereotype.Component;

@Component
public class MyComponent {
    // 一般组件逻辑
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值