注解 | 对象范围 | 全称 | 介绍 |
@PostConstruct | @Target(METHOD) | javax.annotation.PostConstruct |
- PostConstruct 注解用于标记在依赖注入完成后需要执行的方法,以执行任何初始化操作。
- 类中只能有一个方法被标记为这个注解。
- 可以应用于拦截器以及普通类,执行目标为Method @Target(METHOD)
- PostConstruct注解应用于非拦截器类中的方法必须为无参(void)方法
- PostConstruct注解应用的方法可以是public, protected, package private or private
- 在web开发中,PostConstruct注解应用的方法不可是static静态的,但可以是final
教程:一文带你深入理解SpringBean生命周期之PostConstruct、PreDestroy详解 - 掘金 |
@PreDestroy | @Target(METHOD) | javax.annotation.PreDestroy | 作用为:在容器销毁Bean的时候回调执行,用于释放资源 注:除作用不同外,其他注意事项同@PostConstruct注解一致 |
@Resource | @Target({TYPE, FIELD, METHOD}) | javax.annotation.Resource |
- @Resource和@Autowired都是做bean的注入时使用。区别在于@Autowired按byType自动注入。@Resource属于注解是属于J2EE,@Autowired注解属于Spring,推荐使用@Resource
- 此注解标记了应用程序需要的资源。作用于组件类、以及组件类的字段或者方法
- 此注解可以应用于私有字段
public class MyComponent {
private ConnectionFactory connectionFactory;
@Resource(name = "jdbc/MyDB")
private DataSource dataSource;
@Resource(name = "jms/MyFactory")
public void setConnectionFactory(ConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
}
// 其他代码
} 教程:@Resource注解用法_@resource使用-优快云博客 |
@Resources | @Target(TYPE) | javax.annotation.Resources | |
@Autowired | @Target({ ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE }) | org.springframework.beans.factory .annotation.Autowired |
- 可以对类成员变量、构造方法、方法进行标注,以完成自动装备的工作
- @Resource有两个属性name和type。Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。@Autowired只根据type进行注入,不会去匹配name。如果涉及到type无法辨别注入对象时,那需要依赖@Qualifier或@Primary注解一起来修饰。
教程:@Autowired和@Resource的区别 - 掘金 |
@Qualifier | @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE }) | org.springframework.beans.factory .annotation.Qualifier |
- 主要场景为自动装配类时可以定位具体的实现类(
@Autowired 按类型装配Spring Bean。 如果容器中有多个相同类型的bean,则框架将抛出NoUniqueBeanDefinitionException ) - 通过使用
@Qualifier 注解,我们可以消除需要注入哪个bean的问题
public interface Vehicle {
String start();
String stop();
}
//---------分割线-----------------
@Component
public class Bike implements Vehicle {
@Override
public String start() {
return "Bike started";
}
@Override
public String stop() {
return "Bike stopped";
}
}
//---------分割线-----------------
@Component
public class Car implements Vehicle {
@Override
public String start() {
return "Car started";
}
@Override
public String stop() {
return "Car stopped";
}
}
//---------分割线 应用于属性-----------------
@Service
public class Vehicle1Service {
@Autowired
@Qualifier("bike") //如果不加次注解,将抛出异常
private Vehicle vehicle;
public String start() {
return vehicle.start();
}
public String stop() {
return vehicle.stop();
}
}
//---------分割线 应用与构造方法-----------------
@Service
public class Vehicle2Service {
private Vehicle vehicle;
public VehicleService(@Qualifier("bike") Vehicle vehicle) {
this.vehicle = vehicle;
}
public String start() {
return vehicle.start();
}
public String stop() {
return vehicle.stop();
}
} 教程:https://www.cnblogs.com/CreateMyself/p/12153891.html |
@Primary | @Target({ ElementType.TYPE, ElementType.METHOD }) | org.springframework.context .annotation.Primary |
- 作用:当有多个相同类型的bean时,使用@Primary来赋予bean更高的优先级。
- @Parimary用于定义默认bean,@Qualifier用于定位具体的bean
教程:Spring中@Primary注解-优快云博客 |
@Component | @Target({ElementType.TYPE}) | org.springframework.stereotype .Component |
- @Component用于将一个类标识为组件。被@Component注解的类会被自动检测,当使用基于注解的配置和类路径扫描时,它们会被视为候选对象。其他类级别的注解也可以被视为标识组件的特殊类型,比如@Repository注解或AspectJ的@Aspect注解。
- @Component的作用是把普通POJO实例化到Spring容器中,相当于配置文件中的,@Component 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次。基于@Component注解有三个扩展,分别是:@Repository 、@Service、 @Controller。被他们四个标注的类,会被纳入到Spring容器中进行管理
|
@Controller | @Target({ElementType.TYPE}) | org.springframework.stereotype.Controller | 属于@component的扩展注解,用于标注控制层 |
@Service | @Target({ElementType.TYPE}) | org.springframework.stereotype.Service |
- 属于@component的扩展注解,用于标注服务层
- 如果你不知道要在项目的业务层采用
@Service 还是@Component 注解。那么,@Service 是一个更好的选择
|
@Repository | @Target({ElementType.TYPE}) | org.springframework.stereotype.Repository | 属于@component的扩展注解,用于标注数据访问层,也可以说用于标注数据访问组件,即DAO组件 |
@Value | @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE }) | org.springframework.beans.factory.annotation.Value |
- 用于字段、方法/构造函数参数级别的注解,初始化影响参数的默认值表达式。可以在设置变量的时候设置默认值
- 配置文件支持properties、XML、和YML文件
- 配置支持SpEL表达式
- 使用方法为:
- @Value(“常量”) 常量,包括字符串,网址,文件路径等
- @Value(“${}”
: default_value ) 读取配置文件 - @Value(“#{}”
? : default_value ) 读取注入bean(对象)的属性
// 注入普通字符串
@Value("Jack")
private String username;
// 注入文件资源
@Value("classpath:com/test/config.xml")
private Resource resource;
// 注入URL资源
@Value("http://www.baidu.com")
private Resource url;
//假如application.yml配置文件 有myUserName: 张三配置
//读取配置文件
@Value("${myUserName}")
private String myUserName;
//Value(“#{}”), 用于读取bean对象的属性
@Data
@Component
public class User {
private String id;
// 读取配置文件数据
@Value("${myUserName}")
private String username ;
}
@RestController
@RequestMapping("/consumer")
@Slf4j
public class ConsumerController {
// 读取bean属性
@Value("#{user.username}")
private String username;
} 教程:深度解析@Value注解,你真的彻底了解过吗?-51CTO.COM 关于Spring中@Value注解使用 - 掘金 |
@Configuration | @Target(ElementType.TYPE) | org.springframework.context.annotation.Configuration |
@Configuration 是@Conponent的衍生注解,用于标识一个类作为配置类(同等于xml配置),它通常与@Bean 注解一起使用,用于定义 bean 的创建和配置。这使得 Spring 能够识别并加载这些配置类,从而创建应用程序上下文并管理 bean。- 应用此注解类的要求:1)不可以是final类型 2)不可以是匿名类 3)@Configuration必须是静态类
教程:SpringBoot中@Configuration注解-优快云博客 Spring管理的@Configuration注解使用 - 掘金 |
@Bean | @Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE }) | org.springframework.context .annotation.Bean |
- @Bean注解用于告诉方法,产生一个Bean对象,然后这个Bean对象交给Spring管理。产生这个Bean对象的方法Spring只会调用一次,随后这个Spring将会将这个Bean对象放在自己的IOC容器中。
- @Bean注解注册bean,同时可以指定初始化和销毁方法@Bean(name="testBean",initMethod="start",destroyMethod="cleanup")
- name属性相当于<bean>标签的id
- @Bean注解在返回实例的方法上,如果未通过@Bean指定bean的名称,则默认与标注的方法名相同(第一个单词转小写)
- @Bean注解默认作用域为单例singleton作用域,可通过@Scope(“prototype”)设置为原型作用域
- 既然@Bean的作用是注册bean对象,那么完全可以使用@Component、@Controller、@Service、@Ripository等注解注册bean,当然需要配置@ComponentScan注解进行自动扫描。
教程:https://www.cnblogs.com/javazhiyin/p/11175068.html |
@Import | @Target(ElementType.TYPE) | org.springframework.context.annotation.Import |
@Import 注解用于在一个配置类中引入其他配置类,从而实现配置的模块化和重用。通过@Import 注解,可以将其他配置类中定义的 bean 导入到当前的配置中,从而实现更好的组织和管理。- @Import的三种用法 1)直接填class数组方式 2)ImportSelector方式【重点】 3)ImportBeanDefinitionRegistrar方式
|
@RequestMapping | @Target({ElementType.METHOD, ElementType.TYPE}) | org.springframework.web.bind .annotation.RequestMapping | 要配置 Web 请求的映射,就需要你用上 @RequestMapping 注解。 注解可以在控制器类的级别和/或其中的方法的级别上使用。 可以将多个请求映射到一个方法上去,只需要添加一个带有请求路径值列表 @RequestParam 注解配合 @RequestMapping 一起使用,可以将请求的参数同处理方法的参数绑定在一起。 @RequestMapping 注解能够处理 HTTP 请求的方法, 比如 GET, PUT, POST, DELETE 以及 PATCH @RequestMapping参数
- value:指定请求的实际地址,像/action/info这样的。
- method:指定请求的method类型, GET、POST、PUT、DELETE等。
- produces:它的作用是指定返回内容的类型,只有当request请求头中Accept属性包含该produces指定的类型才能返回数据成功。
- consumes:它的作用是指定request请求提交的内容类型(Content-Type)
- params:指定request请求地址中必须包含某些参数值,才让该方法处理。
教程:超详细 Spring @RequestMapping 注解使用技巧 - 掘金 |
@GetMapping | @Target({ElementType.METHOD, ElementType.TYPE}) | | @RequestMapping衍生注解 |
@PostMapping | @Target({ElementType.METHOD, ElementType.TYPE}) | | @RequestMapping衍生注解 |
@DeleteMapping | @Target({ElementType.METHOD, ElementType.TYPE}) | | @RequestMapping衍生注解 |
@PutMapping | @Target({ElementType.METHOD, ElementType.TYPE}) | | @RequestMapping衍生注解 |
@PatchMapping | @Target({ElementType.METHOD, ElementType.TYPE}) | | @RequestMapping衍生注解 |
@RequestParam | @Target({ElementType.PARAMETER}) | org.springframework.web.bind .annotation.RequestParam |
- 简单一点说作用就是把请求中的指定名称的参数传递给控制器中的形参赋值
- @RequestParam注解的value属性值没有对应上jsp中name值则会直接报400错误,因为required属性默认为true,如果加上required=false,没对应上则不会报错,而是获取值为null
- 标注在接口的方法参数上,被标注的参数的值来源于
request.getParameter 或request.getParameterValues 。
教程:@RequestParam用法及原理,你未必真知道-腾讯云开发者社区-腾讯云 |
@PathParam | @Target({ ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD }) | javax.ws.rs.PathParam | jboss包下面的一个实现 教程:理解RESTful中的@PathParam和@QueryParam注解-百度开发者中心 |
@QueryParam | @Target({ ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD }) | javax.ws.rs.QueryParam | jboss包下面的一个实现 教程:理解RESTful中的@PathParam和@QueryParam注解-百度开发者中心 |
@PathVariable | @Target({ElementType.PARAMETER}) | org.springframework.web.bind .annotation.PathVariable | 用于restful参数绑定,和@RequestMapping注解及其衍生类进行配合使用 教程:Spring @PathVariable Annotation | Baeldung |
@ResponseBody | @Target({ ElementType.TYPE, ElementType.METHOD }) | org.springframework.web.bind .annotation.ResponseBody | @ResponseBody用于请求处理方法上,表示方法返回的结果直接写入 HTTP 响应正文(ResponseBody)中,而不是将视图名称解析为视图的内容。该注解通常用于返回 JSON、XML 或二进制数据,而不是 HTML 视图。 教程:@ResponseBody注解-阿里云开发者社区 |
@RequestHeader | @Target({ElementType.PARAMETER}) | org.springframework.web.bind .annotation.RequestHeader | 在Spring MVC中,@RequestHeader注解用于获取HTTP请求头的值。在处理请求时,可能需要访问请求头中的某些信息,如客户端的User-Agent、Content-Type等。使用@RequestHeader注解,我们可以轻松地访问这些信息,以便在Controller中使用。 教程:SpringMVC @RequestHeader注解-腾讯云开发者社区-腾讯云 |
@CookieValue | @Target({ElementType.PARAMETER}) | org.springframework.web.bind.annotation.CookieValue | 将cookie参数映射到控制器方法参数中 教程:饼干探秘:深入Spring MVC中获取Cookie数据的技术解析_springframework cookie-优快云博客 |
@RequestBody | @Target({ElementType.PARAMETER}) | org.springframework.web.bind.annotation.RequestBody | @RequestBody注解是Spring框架中常用的注解之一,用于将HTTP请求的请求体中的数据绑定到一个Java对象上。通过使用@RequestBody注解,可以将请求体中的JSON/XML等格式的数据转换为Java对象,并传递给Controller中的方法进行处理。 教程:@RequestBody注解的作用和示例 – 编程技术之美-IT之美 【SpringBoot教程】RequestBody对象数组提交接口开发实战-腾讯云开发者社区-腾讯云 |
@Scope | @Target({ElementType.TYPE, ElementType.METHOD}) | org.springframework.context .annotation.Scope | 用来表示Spring中Bean的作用域范围, 该注解只能写在类上或者方法上。 @Scope作用于范围:singleton(默认)、prototype、request、session 作用在类上,搭配@Component、@Service注解使用 作用于方法上,搭配@Bean使用 教程:【Spring注解必知必会】全面了解@Scope - 掘金 |
@Transactional | @Target({ElementType.METHOD, ElementType.TYPE}) | org.springframework.transaction.annotation.Transactional | 事务注解 |