Spring-注解

Spring 注解分类


Spring 注解驱动模型

Spring 元注解
java 复制代码
@Documented
@Retention()
@Target()
// 可以继承相关的属性
@Inherited
@Repeatable()
Spirng 模式注解

@ComponentScan 原理

ClassPathScanningCandidateComponentProvider#findCandidateComponents

java 复制代码
public Set<BeanDefinition> findCandidateComponents(String basePackage) {
	if (this.componentsIndex != null && indexSupportsIncludeFilters()) {
		return addCandidateComponentsFromIndex(this.componentsIndex, basePackage);
	}
	else {
		return scanCandidateComponents(basePackage);
	}
}
组合注解

比如@RestController,@SpringBootApplication

注解转换为AnnotationAttributes然后进行合并统一

Spring属性别名

显性别名:

隐式别名:

这个组合注解

继承EnableAutoConfiguration的属性,用隐式别名来表达,也就是直接拿过来不需要改变它的值:

如果说我们要补充语义,引用某个属性,自己定义新的属性名称来引用父类的:

举例如果我们要派生这个注解,我们不可能在注解里面写死扫描路径,这个时候就要通过隐式别名来继承注解ComponentScan的属性,这样我们就可以配置scanBasePackages

java 复制代码
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@ComponentScan
public @interface MyComponentScan {
    @AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
    String scanBasePackages() default "";

}

我们也可以这样:

java 复制代码
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@ComponentScan
public @interface MyComponentScan {
	// 传递性别名 basePackages -> value value -> scanBasePackages
    @AliasFor(annotation = ComponentScan.class, attribute = "value")
    String scanBasePackages() default "";
}

Spring属性覆盖

注解和原注解出现同名属性的时候会覆盖原注解的属性

java 复制代码
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@ComponentScan
public @interface MyComponentScan {
    @AliasFor(annotation = ComponentScan.class, attribute = "value")
    String scanBasePackages() default "";
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@MyComponentScan
public @interface MyComponentScan2 {
    // 显示覆盖
    String[] scanBasePackages() default "";
	// 隐式覆盖 
    @AliasFor(attribute = "scanBasePackages")
    String[] packages() default {};
}
@MyComponentScan2(packages = {"com.yong.annotationpkg"})
// packages 覆盖scanBasePackages scanBasePackages覆盖MyComponentScan scanBasePackages scanBasePackages 引用的是ComponentScan

Spring @Enable 模块驱动

案例1通过@Configration实现

java 复制代码
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DemoConfig.class)
public @interface EnableDemo {

}
@Configuration
public class DemoConfig {
    @Bean
    Person person() {
        Person person = new Person();
        person.setName("liy");
        person.setId(11L);
        return person;
    }
}

@EnableDemo
public class EnableAnnotationDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(EnableAnnotationDemo.class);
        context.refresh();
        Person bean = context.getBean(Person.class);
        System.out.println(bean);
    }
}

案例2通过ImportSelector接口实现:

java 复制代码
public class DemoImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{"com.yong.annotationpkg.DemoConfig"};
    }
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DemoImportSelector.class)
public @interface EnableDemo {

}
@EnableDemo
public class EnableAnnotationDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(EnableAnnotationDemo.class);
        context.refresh();
        Person bean = context.getBean(Person.class);
        System.out.println(bean);
    }
}

案例3通过

java 复制代码
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DemoImportBeanDefinitionRegister.class)
public @interface EnableDemo {

}
public class DemoImportBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
        AnnotatedBeanDefinition annotatedBeanDefinition = new AnnotatedGenericBeanDefinition(DemoConfig.class);
        registry.registerBeanDefinition("demoConfig", annotatedBeanDefinition);
    }
}
Spring 条件注解
java 复制代码
@Configuration
public class ProfileDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(ProfileDemo.class);
        ConfigurableEnvironment environment = context.getEnvironment();
        // 兜底方案
        environment.setDefaultProfiles("odd");
        // 激活方案
        environment.setActiveProfiles("even");
        context.refresh();
        Object odd = context.getBean(Integer.class);
        System.out.println(odd);
        context.close();
    }
    // 激活不同的环境注册不同的Bean
    @Bean
    @Profile("odd")
    public Integer odd() {
        return 2;
    }
    @Bean
    @Profile("even")
    public Integer even() {
        return 1;
    }
}

自定义实现:

java 复制代码
public class EvenProfileCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Environment environment = context.getEnvironment();
        return environment.acceptsProfiles("even");
    }
}
@Bean
 @Conditional(EvenProfileCondition.class)
 public Integer even() {
     return 1;
 }

ConditionEvaluator#shouldSkip

参考资料

小马哥核心编程思想

相关推荐
我命由我123452 分钟前
Kotlin 开发 - 双冒号操作符(引用顶层函数、引用成员函数、引用构造函数、引用属性、引用类)
android·java·开发语言·kotlin·android studio·android jetpack·android-studio
佳xuan3 分钟前
QA与RAG检索
java·服务器·前端
半瓶榴莲奶^_^3 小时前
jvm java虚拟机
java·jvm
invicinble9 小时前
这里对java的知识体系做一个全域的介绍
java·开发语言·python
wbs_scy9 小时前
【Linux 线程进阶】进程 vs 线程资源划分 + 线程控制全详解
java·开发语言
ss2739 小时前
食谱推荐系统功能测试如何写?
java·数据库·spring boot·功能测试
AI人工智能+电脑小能手10 小时前
【大白话说Java面试题】【Java基础篇】第15题:JDK1.7中HashMap扩容为什么会发生死循环?如何解决
java·开发语言·数据结构·后端·面试·哈希算法
try2find10 小时前
打印ascii码报错问题
java·linux·前端
014-code10 小时前
CompletableFuture 实战模板(超时、组合、异常链处理)
java·数据库
Nicander10 小时前
多数据源下@transcation事务踩坑
java·后端