springboot 自定义Enable* 注解

时间:2024-04-11 07:19:43

项目结构:springboot 自定义Enable* 注解

1.定义一个注解类 EnableScanner

package com.boot.condition.bootcondition.simple;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * 〈一句话功能简述〉<br> 
 * 〈扫描包〉
 *
 * @author 26918
 * @create 2018/10/17
 * @since 1.0.0
 */
@SpringBootApplication
@EnableScanner(packages = {"com.boot.condition.bootcondition.simple.entity", "com.boot.condition.bootcondition.simple.bean"})
public class ScannerPackageApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(ScannerPackageApplication.class, args);

        context.close();
    }

}



import org.springframework.context.annotation.Import;

import java.lang.annotation.*;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(ScannerPackageRegistrar.class)
public @interface EnableScanner {

    String[] packages();
}

2.导入需要扫描的注册类 ScannerPackageRegistrar, 将扫描的对象设置到spring容器

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author 26918
 * @create 2018/10/17
 * @since 1.0.0
 */
public class ScannerPackageRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
        System.out.println("packages value::"+annotationMetadata.getAnnotationAttributes(EnableScanner.class.getName()));

        String[] arrs = (String[]) annotationMetadata.getAnnotationAttributes(EnableScanner.class.getName()).get("packages");
        List<String> packages = Arrays.asList(arrs);
        System.out.println(packages);

        BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(MyBeanDefinitionProcessor.class);
        bdb.addPropertyValue("packages", packages);

        beanDefinitionRegistry.registerBeanDefinition(MyBeanDefinitionProcessor.class.getName(), bdb.getBeanDefinition());
    }
}

3.定义一个MyBeanDefinitionProcessor implements BeanPostProcessor 用于初始回调对象

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import java.util.List;
/**
 * 〈一句话功能简述〉<br> 
 * 〈bean 初始回调〉
 *
 * @author 26918
 * @create 2018/10/17
 * @since 1.0.0
 */

public class MyBeanDefinitionProcessor implements BeanPostProcessor {

    private List<String> packages;

    public List<String> getPackages() {
        return packages;
    }

    public void setPackages(List<String> packages) {
        this.packages = packages;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        for (String pkg : packages) {
            if(bean.getClass().getName().startsWith(pkg)){
                System.out.println("instance bean:"+bean.getClass().getName());
            }
        }
        return bean;
    }
}