|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright 2012-2019 the original author or authors.
|
|
|
|
|
* Copyright 2012-2020 the original author or authors.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
@ -23,11 +23,11 @@ import java.util.Collections;
|
|
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.BeanFactory;
|
|
|
|
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
|
|
|
|
import org.springframework.beans.factory.config.BeanDefinition;
|
|
|
|
|
import org.springframework.beans.factory.config.ConstructorArgumentValues;
|
|
|
|
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
|
|
|
|
import org.springframework.beans.factory.support.GenericBeanDefinition;
|
|
|
|
|
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
|
|
|
@ -109,29 +109,15 @@ public class EntityScanPackages {
|
|
|
|
|
Assert.notNull(registry, "Registry must not be null");
|
|
|
|
|
Assert.notNull(packageNames, "PackageNames must not be null");
|
|
|
|
|
if (registry.containsBeanDefinition(BEAN)) {
|
|
|
|
|
BeanDefinition beanDefinition = registry.getBeanDefinition(BEAN);
|
|
|
|
|
ConstructorArgumentValues constructorArguments = beanDefinition.getConstructorArgumentValues();
|
|
|
|
|
constructorArguments.addIndexedArgumentValue(0, addPackageNames(constructorArguments, packageNames));
|
|
|
|
|
EntityScanPackagesBeanDefinition beanDefinition = (EntityScanPackagesBeanDefinition) registry
|
|
|
|
|
.getBeanDefinition(BEAN);
|
|
|
|
|
beanDefinition.addPackageNames(packageNames);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
|
|
|
|
|
beanDefinition.setBeanClass(EntityScanPackages.class);
|
|
|
|
|
beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(0,
|
|
|
|
|
StringUtils.toStringArray(packageNames));
|
|
|
|
|
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
|
|
|
|
|
registry.registerBeanDefinition(BEAN, beanDefinition);
|
|
|
|
|
registry.registerBeanDefinition(BEAN, new EntityScanPackagesBeanDefinition(packageNames));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String[] addPackageNames(ConstructorArgumentValues constructorArguments,
|
|
|
|
|
Collection<String> packageNames) {
|
|
|
|
|
String[] existing = (String[]) constructorArguments.getIndexedArgumentValue(0, String[].class).getValue();
|
|
|
|
|
Set<String> merged = new LinkedHashSet<>();
|
|
|
|
|
merged.addAll(Arrays.asList(existing));
|
|
|
|
|
merged.addAll(packageNames);
|
|
|
|
|
return StringUtils.toStringArray(merged);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@link ImportBeanDefinitionRegistrar} to store the base package from the importing
|
|
|
|
|
* configuration.
|
|
|
|
@ -162,4 +148,25 @@ public class EntityScanPackages {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static class EntityScanPackagesBeanDefinition extends GenericBeanDefinition {
|
|
|
|
|
|
|
|
|
|
private Set<String> packageNames = new LinkedHashSet<>();
|
|
|
|
|
|
|
|
|
|
EntityScanPackagesBeanDefinition(Collection<String> packageNames) {
|
|
|
|
|
setBeanClass(EntityScanPackages.class);
|
|
|
|
|
setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
|
|
|
|
|
addPackageNames(packageNames);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Supplier<?> getInstanceSupplier() {
|
|
|
|
|
return () -> new EntityScanPackages(StringUtils.toStringArray(this.packageNames));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addPackageNames(Collection<String> additionalPackageNames) {
|
|
|
|
|
this.packageNames.addAll(additionalPackageNames);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|