【Spring&SpringBoot】注解梳理(含自定义)

 参考:https://www.baeldung.com/tag/spring-annotations/   按不同维度介绍了注解

>> Spring Conditional Annotations

>> AliasFor Annotation in Spring

>> Spring @EntityScan vs. @ComponentScan

>> Spring @Component Annotation

>> Where Should the Spring @Service Annotation Be Kept?

>> Difference Between @ComponentScan and @EnableAutoConfiguration in Spring Boot

>> Differences in @Valid and @Validated Annotations in Spring

>> Guide to @DynamicPropertySource in Spring

>> The Spring @ConditionalOnProperty Annotation

>> Spring @RequestParam Annotation

>> Spring @RequestParam vs @PathVariable Annotations

>> Spring @Import Annotation

>> Testing Spring Boot @ConfigurationProperties

>> Guide to @SpringBootConfiguration in Spring Boot

>> The Spring @Qualifier Annotation

>> Guide to @EnableConfigurationProperties

>> Spring Null-Safety Annotations

>> Spring @Primary Annotation

>> @Component vs @Repository and @Service in Spring

>> Controlling Bean Creation Order with @DependsOn Annotation


>> Spring Core Annotations

>> Spring Web Annotations

>> Spring Boot Annotations

>> Spring Data Annotations

>> @Lookup Annotation in Spring

>> The Spring @Controller and @RestController Annotations

>> A Quick Guide to the Spring @Lazy Annotation

>> The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5

>> Spring 5 Testing with @EnabledIf Annotation

>> Spring’s RequestBody and ResponseBody Annotations

>> Quick Guide to the Spring @Enable Annotations

>> Guide to @ConfigurationProperties in Spring Boot

>> A Quick Guide to Spring @Value

>> Implementing a Custom Spring AOP Annotation

>> The @ServletComponentScan Annotation in Spring Boot

>> Spring @RequestMapping New Shortcut Annotations

>> Spring MVC and the @ModelAttribute Annotation

>> Spring Bean Annotations

>> A Spring Custom Annotation for a Better DAO

>> Spring Data MongoDB – Indexes, Annotations and Converters

 

Spring Core注解: 

https://www.baeldung.com/spring-core-annotations
    @Autowired: 构造器注入、setter注入、字段注入三种方式
    @Bean、@Qualifier、@DependOn、@Lazy、@Primary、@Scope、
    @Import: specific @Configuration classes without component scanning   https://www.cnblogs.com/yichunguo/p/12122598.html 
                第一种用法:@Import({ 要导入的容器中的组件,可以是configuration,也可以是一个具体的类数组 } ):容器会自动注册这个组件,id默认是全类名  
                第二种用法:ImportSelector接口实现类:返回需要导入的组件的全类名数组,springboot底层用的特别多【重点 】
                第三种用法:ImportBeanDefinitionRegistrar:手动注册bean到容器
      @Profile
      @Configuration
             @ImportResource(locations={"classpath:applicationContext.xml"})  导入bean XML文件,连同@Configuration使用
               @Import(xxxConfig.class)/ @Import(xxx.class)
               @ComponentScan:和 @configuratin配合使用,扫描指定路径下的bean   @ComponentScan:和 @configuratin配合使用,扫描指定路径下的bean
      @PropertySource("classpath:/annotations.properties")class VehicleFactoryConfig {}
      @Value: 构造器注入、setter注入、字段注入,需要配合@PropertyResource使用
     @PropertySources 导入多个
 
    @Alias:样例ComponentScan注解的定义  
    @ComponentScan  vs  @EnableAutoConfiguration   (SpringBoot)
@ComponentScan scans for annotated Spring components  -- 对应spring,扫描service、controller等
@EnableAutoConfiguration is used to enable the auto-configuration  --- 对应springboot,自动发现和注册bean

 属性读取:

Spring:
                     @PropertySource("classpath:foo.properties")、  @PropertySources(定义多个属性文件):作用在类上,从名字可知,不支持导入yaml格式
                     @Value( "${jdbc.url}" ): 读取参数,SPEL表达式; 更复杂场景参考下面Value专区                 
SpringBoot:
           1)自动检测并读取 src/main/resouces/application.properties或yml ,所以只需将属性放入该文件,不需指定文件
           2)手工指定 java -jar app.jar --spring.config.location=classpath:/another-location.properties 或 config/*/ 
           3) @ConfigurationProperties(prefix = "server.default"):如果属性是一组,如jdbc,自动映射这些属性到Java对象中 POJO(非Bean)
                https://www.baeldung.com/configuration-properties-in-spring-boot     映射list、map等复杂元素
                

      通过如下方式,注册Java Bean;或通过下面的EnableConfigurationProperties实现

      

posted @   飞翔在天  阅读(111)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-12-10 【分布式事务】基本原理(含不同模式的简单对比)
点击右上角即可分享
微信分享提示