Spring - Spring注解汇总
***前提***
1-在springcontext-xml里,开启注解
2-
***生命周期相关***
@PostConstruct
在Spring中,在初始化一个对象时,执行顺序为
1 | Constructor() >> @Autowired >> @PostConstruct |
- 其实从依赖注入的字面意思就可以知道,要将对象p注入到对象a,那么首先就必须得生成对象p与对象a,才能执行注入。所以,如果一个类A中有个成员变量p被@Autowired注解,那么@Autowired注入是发生在A的构造方法执行完之后的。
- 如果想在生成对象时候完成某些初始化操作,而偏偏这些初始化操作又依赖于依赖注入,那么就无法在构造函数中实现。为此,可以使用@PostConstruct注解一个方法来完成初始化,@PostConstruct注解的方法将会在依赖注入完成后被自动调用。
@PreDestroy
@PreDestroy注解在容器移除对象之前调用
***用来生成bean对象***
@Component (@Controller, @Service, @Repository)
@Component是所有受Spring 管理组件的通用形式。
这几个注解的作用都是生产bean, 这些注解都是注解在类上,将类注解成spring的bean工厂中一个一个的bean。@Controller, @Service, @Repository基本就是语义更加细化的@Component。
@ComponentScan
如果想让上述的@Component (@Controller, @Service, @Repository) 成功的注册为bean进入spring容器,必须搭配着@ComponentScan使用。
@Configuration + @Bean
@Configuration标注在类上,相当于把该类作为spring的一个xml配置文件。
@Bean标注在方法上(返回某个实例的方法),等价于spring的xml配置文件中的<bean>,作用为:注册bean对象
@Component 方式 PK @Bean
参考:https://blog.csdn.net/w605283073/article/details/89221522
相同点:
- @Component 和 @Bean 是两种使用注解来定义bean的方式。
不同点:
- @Component注释类和bean之间存在隐式的一对一映射 --> 即每一个类一个bean
@Bean将bean的声明与类定义分离,
用于显式声明单个bean --> 即每一个类多个bean
为什么有了@Compent,还需要@Bean呢:
- 如果想将第三方的类变成组件,你又没有没有源代码,也就没办法使用
@Component
进行自动配置,这种时候使用@Bean
就比较合适了
***用来注入对象***
@Autowired
先byType, 后byName
@Autowired默认按类型匹配的方式,在容器查找匹配的Bean,当有且仅有一个匹配的Bean时,Spring将其注入@Autowired标注的变量中.
@Qualifier
@Qualifier 就是 autowire=byName 名称匹配, @Autowired注解判断多个bean类型相同时,就需要使用 @Qualifier("xxx") 来指定依赖的bean的id
@Resource (J2EE注解)
先byName, 后byType
@Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配。
@Autowired @Qualifier是Spring的注解,@Resource是J2EE的注解。
***用来注入属性***
@Value("xxx")
下图@Value("kuansghen2")是直接给属性赋值
如果想从配置文件中获取,需要加入大括号,比如@Value("${user.name}")
***用来注入context/config文件***
@Import(xxx.class)
KuangConfig2.java也有@Configuration标签。这里的@Import标签相当于xml方式的<import xxxx>
@PropertySource
@PropertySource是Spring boot为了方便引入properties配置文件提供的一个注解
配置文件config.properties
1 2 3 4 | jdbc.driver = oracle.jdbc.driver.OracleDriver jdbc.username= sassy jdbc.password = password jdbc.url = jdbc\:oracle\:thin\:@(DESCRIPTION\=(ADDRESS\=(PROTOCOL\=TCP)(HOST\= 10.221 . 129.208 )(PORT\= 1523 ))(CONNECT_DATA\=(SERVICE_NAME\=otatransuser))) |
用法:@PropertySource + @Value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | @Configuration @PropertySource ( "classpath:jdbc.properties" ) public class PropertiesWithJavaConfig { @Value (${jdbc.driver}) private String driver; @Value (${jdbc.url}) private String url; @Value (${jdbc.username}) private String username; @Value (${jdbc.password}) private String password; //(TBC)要想使用@Value 用${}占位符注入属性,这个bean是必须的,这个就是占位bean,另一种方式是不用value直接用Envirment变量直接getProperty('key') @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } } |
@ConfigurationProperties
当配置文件是.properties文件
用法:@PropertySource + @ConfigurationProperties
使用@Value注解方式有一个不太友好的地方就是,当项目中有大量的属性进行配置的时候,我们需要一个个的在类的字段中增加@Value注解,这样确实很费劲,不过我们可以通过Springboot提供的@ConfigurationProperties注解解决这个问题。
比如我们配置的prefix = "jdbc",PropertiesWithJavaConfig类中有一个driver字段,则driver字段需要匹配的属性是 --> prefix+字段 = jdbc.driver
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @Configuration @PropertySource ( "classpath:jdbc.properties" ) @ConfigurationProperties (prefix = "jdbc" ) public class PropertiesWithJavaConfig { private String driver; private String url; private String username; private String password; //... } |
当配置文件是.yaml文件
注意,两边属性的名字必须保持一致
***作用域***
@Scope("Singleton")
***任务调度***
task:scheduled-tasks
Spring任务调度<task:scheduled-tasks>【含cron参数详解】 (转载)
***web相关***
@RequestBody
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?