Spring Boot 自定义 Bean 加载器

JDr*_*818 5 java jdbi spring-boot jdbi3 spring-autoconfiguration

我正在将 JDBI 与 Spring Boot 结合使用。我遵循了这个指南,这导致必须创建一个类:JdbiConfig其中,对于应用程序上下文中想要的每个 dao,您必须添加:

@Bean
public SomeDao someDao(Jdbi jdbi) {
    return jdbi.onDemand(SomeDao.class);
}
Run Code Online (Sandbox Code Playgroud)

我想知道 Spring Boot 中是否有某种方法可以创建自定义处理器来创建 bean 并将它们放入应用程序上下文中。关于这如何工作,我有两个想法:

  1. 使用自定义注释对 DAO 进行注释,@JdbiDao并编写一些内容来获取它们。我曾尝试将这些手动注入到应用程序启动中,但问题是它们可能无法及时加载以进行注入,因为在类扫描期间无法识别它们。
  2. 创建一个JdbiDao每个存储库接口都可以扩展的类。然后用标准注释接口@Repository并创建自定义处理器以通过以下方式加载它们Jdbi#onDemand

这是我的两个想法,但我不知道有什么方法可以实现。我坚持手动创建一个 bean?以前解决过这个问题吗?

Mạn*_*yễn 4

The strategy is to scan your classpath for dao interface, then register them as bean.

We need: BeanDefinitionRegistryPostProcessor to register additional bean definition and a FactoryBean to create the jdbi dao bean instance.

  1. Mark your dao intercface with @JdbiDao
@JdbiDao
public interface SomeDao {
}
Run Code Online (Sandbox Code Playgroud)
  1. Define a FactoryBean to create jdbi dao
public class JdbiDaoBeanFactory implements FactoryBean<Object>, InitializingBean {

    private final Jdbi jdbi;
    private final Class<?> jdbiDaoClass;
    private volatile Object jdbiDaoBean;

    public JdbiDaoBeanFactory(Jdbi jdbi, Class<?> jdbiDaoClass) {
        this.jdbi = jdbi;
        this.jdbiDaoClass = jdbiDaoClass;
    }

    @Override
    public Object getObject() throws Exception {
        return jdbiDaoBean;
    }

    @Override
    public Class<?> getObjectType() {
        return jdbiDaoClass;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        jdbiDaoBean = jdbi.onDemand(jdbiDaoClass);
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. Scan classpath for @JdbiDao annotated interfaces:
public class JdbiBeanFactoryPostProcessor
        implements BeanDefinitionRegistryPostProcessor, ResourceLoaderAware, EnvironmentAware, BeanClassLoaderAware, BeanFactoryAware {

    private BeanFactory beanFactory;
    private ResourceLoader resourceLoader;
    private Environment environment;
    private ClassLoader classLoader;

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false) {
            @Override
            protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
                // By default, scanner does not accept regular interface without @Lookup method, bypass this
                return true;
            }
        };
        scanner.setEnvironment(environment);
        scanner.setResourceLoader(resourceLoader);
        scanner.addIncludeFilter(new AnnotationTypeFilter(JdbiDao.class));
        List<String> basePackages = AutoConfigurationPackages.get(beanFactory);
        basePackages.stream()
                .map(scanner::findCandidateComponents)
                .flatMap(Collection::stream)
                .forEach(bd -> registerJdbiDaoBeanFactory(registry, bd));
    }

    private void registerJdbiDaoBeanFactory(BeanDefinitionRegistry registry, BeanDefinition bd) {
        GenericBeanDefinition beanDefinition = (GenericBeanDefinition) bd;
        Class<?> jdbiDaoClass;
        try {
            jdbiDaoClass = beanDefinition.resolveBeanClass(classLoader);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        beanDefinition.setBeanClass(JdbiDaoBeanFactory.class);
        // Add dependency to your `Jdbi` bean by name
        beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("jdbi"));
        beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(Objects.requireNonNull(jdbiDaoClass));

        registry.registerBeanDefinition(jdbiDaoClass.getName(), beanDefinition);
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. Import our JdbiBeanFactoryPostProcessor
@SpringBootApplication
@Import(JdbiBeanFactoryPostProcessor.class)
public class Application {
}
Run Code Online (Sandbox Code Playgroud)