SpringBoot常用注解

本文详细介绍Spring Boot项目中配置文件参数的使用方法,包括@Value、@Component、@ConfigurationProperties等注解的运用,以及@RestController、@GetMapping、@PostMapping等接口映射注解的实践。同时,讲解了如何通过@Autowired引入其他代码文件,如配置类和服务文件,并探讨了事务管理、表单验证及参数传递的不同方式。

自己小白入门做笔记用的

项目配置文件中的参数
@Value("${para}") → para是配置文件中的变量
@Component \n @ConfigurationProPerties(prefix = "pre") → 写在实体类中,pre是配置文件中一组变量的组名,也就是前缀
接口文件
@RestController → 写在接口函数外面的类的上面,相当于@Controller \n @ResposeBody
@GetMapping("map") → get请求方式的接口的映射
@PostMapping("map") → post请求方式的接口的映射
@RequestMapping("map") → 两种请求方式都可以
其他的
@Autowired → 引入其他代码文件,比如引入项目配置文件中的参数最后一条所说的实体类,即配置类,也可以引入Service文件
@Transactional → 事务,下面的函数中的操作作为数据库事务进行
@MIN @Valid → 表单验证
传递参数
方式一 get和post

必传:

@GetMapping("/getid")
public String getid(@ReauestParam("id") Integer myId) {
	return "id:" + myId;
}

非必传:

@GetMapping("/getid")
public String getid(@ReauestParam(value = "id", required = false, defaultValue = "0") Integer myId) {
	return "id:" + myId;
}

defaultValue默认值必须写成字符串的形式,变量类型仍然是后面设置的类型,此处是Integer
URL:/say?id=100

方式二 仅限get
@GetMapping("/getid/{id}")
public String getid(@PathVariable("id") Integer myId) {
	return "id:" + myId;
}

URL:/say/100

### Spring Boot 常用注解及功能说明 Spring Boot 是一个快速开发框架,通过整合常用的第三方依赖、简化 XML 配置并采用注解形式,内置 HTTP 服务器(如 Jetty 和 Tomcat),使得开发者可以以 Java 应用程序的形式执行项目[^3]。以下是 Spring Boot常用注解及其功能说明: #### 1. `@SpringBootApplication` 这是一个复合注解,整合了以下三个注解的功能: - **`@SpringBootConfiguration`**:标识当前类为 Spring Boot 的配置类[^1]。 - **`@EnableAutoConfiguration`**:启用 Spring Boot 的自动配置机制,根据项目中的依赖和应用上下文自动配置 Spring 应用程序[^2]。 - **`@ComponentScan`**:自动扫描指定包及其子包中的 Spring 组件,并将它们注册为 Spring 容器中的 Bean[^2]。 示例代码如下: ```java package com.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MyStartApplication { public static void main(String[] args) { SpringApplication.run(MyStartApplication.class, args); } } ``` #### 2. `@Configuration` 用于标识一个类作为配置类,类似于 Spring XML 配置文件的作用。该注解通常与 `@Bean` 结合使用,定义方法返回的实例作为 Spring 容器中的 Bean[^3]。 示例代码如下: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public MyService myService() { return new MyServiceImpl(); } } ``` #### 3. `@Component`, `@Service`, `@Repository`, `@Controller` 这些注解都属于组件扫描的一部分,用于将类标记为 Spring 容器中的 Bean: - **`@Component`**:通用注解,表示该类是一个组件。 - **`@Service`**:用于业务逻辑层的类[^3]。 - **`@Repository`**:用于数据访问层的类[^3]。 - **`@Controller`**:用于控制器层的类,通常与 Web 层结合使用。 示例代码如下: ```java import org.springframework.stereotype.Service; @Service public class MyService { public String getMessage() { return "Hello, Spring Boot!"; } } ``` #### 4. `@RestController` 这是一个组合注解,相当于 `@Controller` 和 `@ResponseBody` 的结合。它主要用于构建 RESTful Web 服务,直接返回 JSON 或 XML 格式的数据。 示例代码如下: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @GetMapping("/message") public String getMessage() { return "Hello, RESTful API!"; } } ``` #### 5. `@Autowired` 用于自动装配依赖。它可以作用于构造函数、字段、Setter 方法等,Spring 容器会根据类型或名称自动注入相应的 Bean。 示例代码如下: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class MyService { private final MyRepository myRepository; @Autowired public MyService(MyRepository myRepository) { this.myRepository = myRepository; } public String getData() { return myRepository.findData(); } } ``` #### 6. `@Value` 用于从配置文件中读取属性值,或者直接赋值给字段。可以通过 `${}` 占位符获取配置文件中的值。 示例代码如下: ```properties app.name=MyApp app.version=1.0.0 ``` ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class AppInfo { @Value("${app.name}") private String name; @Value("${app.version}") private String version; public String getAppInfo() { return name + " - " + version; } } ``` #### 7. `@Bean` 用于在 `@Configuration` 类中定义方法,将方法返回的对象注册为 Spring 容器中的 Bean[^3]。 示例代码如下: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public MyService myService() { return new MyServiceImpl(); } } ``` #### 8. `@PostConstruct` 和 `@PreDestroy` 这两个注解分别用于定义 Bean 初始化后和销毁前的回调方法。`@PostConstruct` 标记的方法会在 Bean 创建完成后执行,而 `@PreDestroy` 标记的方法会在 Bean 销毁前执行[^4]。 示例代码如下: ```java import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.stereotype.Component; @Component public class MyComponent { @PostConstruct public void init() { System.out.println("Bean initialized."); } @PreDestroy public void destroy() { System.out.println("Bean destroyed."); } } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值