DATACMNS-1772 - Remove SpringDataAnnotationBeanNameGenerator and use BeanNameGenerator directly.
We now invoke BeanNameGenerator directly without additional indirections. Previously, SpringDataAnnotationBeanNameGenerator was calling BeanNameGenerator using null for BeanDefinitionRegistry which caused downstream null dereference.
This commit is contained in:
@@ -19,6 +19,7 @@ import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
||||
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.BeanNameGenerator;
|
||||
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -31,25 +32,32 @@ import org.springframework.util.ClassUtils;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Jens Schauder
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class RepositoryBeanNameGenerator {
|
||||
|
||||
private final ClassLoader beanClassLoader;
|
||||
private final SpringDataAnnotationBeanNameGenerator delegate;
|
||||
private final BeanNameGenerator generator;
|
||||
private final BeanDefinitionRegistry registry;
|
||||
|
||||
/**
|
||||
* Creates a new {@link RepositoryBeanNameGenerator} for the given {@link ClassLoader} and {@link BeanNameGenerator}.
|
||||
* Creates a new {@link RepositoryBeanNameGenerator} for the given {@link ClassLoader}, {@link BeanNameGenerator}, and
|
||||
* {@link BeanDefinitionRegistry}.
|
||||
*
|
||||
* @param beanClassLoader must not be {@literal null}.
|
||||
* @param generator must not be {@literal null}.
|
||||
* @param registry must not be {@literal null}.
|
||||
*/
|
||||
public RepositoryBeanNameGenerator(ClassLoader beanClassLoader, BeanNameGenerator generator) {
|
||||
public RepositoryBeanNameGenerator(ClassLoader beanClassLoader, BeanNameGenerator generator,
|
||||
BeanDefinitionRegistry registry) {
|
||||
|
||||
Assert.notNull(beanClassLoader, "Bean ClassLoader must not be null!");
|
||||
Assert.notNull(generator, "BeanNameGenerator must not be null!");
|
||||
Assert.notNull(registry, "BeanDefinitionRegistry must not be null!");
|
||||
|
||||
this.beanClassLoader = beanClassLoader;
|
||||
this.delegate = new SpringDataAnnotationBeanNameGenerator(generator);
|
||||
this.generator = generator;
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,7 +73,7 @@ class RepositoryBeanNameGenerator {
|
||||
? (AnnotatedBeanDefinition) definition //
|
||||
: new AnnotatedGenericBeanDefinition(getRepositoryInterfaceFrom(definition));
|
||||
|
||||
return delegate.generateBeanName(beanDefinition);
|
||||
return generator.generateBeanName(beanDefinition, registry);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -58,7 +58,7 @@ public abstract class RepositoryConfigurationSourceSupport implements Repository
|
||||
Assert.notNull(registry, "BeanDefinitionRegistry must not be null!");
|
||||
|
||||
this.environment = environment;
|
||||
this.beanNameGenerator = new RepositoryBeanNameGenerator(classLoader, generator);
|
||||
this.beanNameGenerator = new RepositoryBeanNameGenerator(classLoader, generator, registry);
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
/*
|
||||
* Copyright 2017-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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.repository.config;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.BeanNameGenerator;
|
||||
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
|
||||
|
||||
/**
|
||||
* Simple extension to Spring's {@link AnnotationBeanNameGenerator} to work without a {@link BeanDefinitionRegistry}.
|
||||
* Although he API of the extended class requires a non-{@literal null} registry it can actually work without one unless
|
||||
* {@link AnnotationBeanNameGenerator#buildDefaultBeanName} is overridden and expecting a non-{@literal null} value
|
||||
* here.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @since 2.0
|
||||
* @soundtrack Nils Wülker - Never Left At All (feat. Rob Summerfield)
|
||||
*/
|
||||
class SpringDataAnnotationBeanNameGenerator {
|
||||
|
||||
private final BeanNameGenerator delegate;
|
||||
|
||||
public SpringDataAnnotationBeanNameGenerator(BeanNameGenerator delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a bean name for the given {@link BeanDefinition}.
|
||||
*
|
||||
* @param definition must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("null")
|
||||
public String generateBeanName(BeanDefinition definition) {
|
||||
return delegate.generateBeanName(definition, null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user