SpringBoot复习:(21)自定义ImportBeanDefinitionRegistrar

要达到的目的:将某个包下使用了某个自定义注解(比如@MyClassMapper)的类注册到Spring 容器。
一、自定义注解:

package com.example.demo.service;


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface MyClassMapper {
}

二、使用了自定义注解的组件类:

package com.example.demo.service;

@MyClassMapper
public class HelloService {
    public HelloService(){
        System.out.println("this is hello......");
    }
}

三、自定义一个ClasspathBeanDefinitionScanner:

package com.example.demo.component;

import com.example.demo.service.MyClassMapper;
import com.example.demo.service.MybatisMapper;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.stereotype.Component;

import java.util.Set;


public class MyClasspathBeanDefinitionScanner extends ClassPathBeanDefinitionScanner {
    public MyClasspathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) {
        super(registry, useDefaultFilters);
    }


    protected void registerFilters() {
        addIncludeFilter(new AnnotationTypeFilter(MyClassMapper.class));
    }
    @Override
    protected Set doScan(String... basePackages) {
        return super.doScan(basePackages);
    }
}


四、自定义ImportBeanDefinitionRegistrar

package com.example.demo.component;

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

@Component
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    public static String BASE_PACKAGE = "com.example.demo.service";
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        MyClasspathBeanDefinitionScanner scanner = new MyClasspathBeanDefinitionScanner(registry, false);
        scanner.registerFilters();
        scanner.doScan(BASE_PACKAGE);
    }
}

其中调用MyClasspathBeanDefinitionScanner进行组件扫描。

五、将自定义ImportBeanDefinitionRegistrar配置到容器

package com.example.demo.config;

import com.example.demo.component.MyClasspathBeanDefinitionScanner;
import com.example.demo.component.MyImportBeanDefinitionRegistrar;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(MyImportBeanDefinitionRegistrar.class)
public class MyConfig {
}

六、测试代码

package com.example.demo;

import com.example.demo.service.HelloService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.PropertySource;

@SpringBootApplication
@PropertySource("classpath:my.properties")
public class DemoApplication {

	public static void main(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
		System.out.println("abc");
		HelloService helloService = (HelloService) context.getBean("helloService");
		System.out.println(helloService);



	}

}

你可能感兴趣的:(SpringBoot,spring,boot,java,后端)