forked from spring-projects/spring-data-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DATACMNS-168 - Allow customizing repository bean names.
The bean name is now resolved through inspecting Spring stereotype annotations and @nAmed on the repository interface. If none found we're still using the uncapitalized simple interface name as we did until now.
- Loading branch information
Showing
6 changed files
with
186 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...src/main/java/org/springframework/data/repository/config/RepositoryBeanNameGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2012 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 | ||
* | ||
* http://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.BeanClassLoaderAware; | ||
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.support.BeanDefinitionRegistry; | ||
import org.springframework.beans.factory.support.BeanNameGenerator; | ||
import org.springframework.context.annotation.AnnotationBeanNameGenerator; | ||
import org.springframework.util.ClassUtils; | ||
|
||
/** | ||
* Special {@link BeanNameGenerator} to create bean names for Spring Data repositories. Will delegate to an | ||
* {@link AnnotationBeanNameGenerator} but let the delegate work with a customized {@link BeanDefinition} to make sure | ||
* the repository interface is inspected and not the actual bean definition class. | ||
* | ||
* @author Oliver Gierke | ||
*/ | ||
public class RepositoryBeanNameGenerator implements BeanNameGenerator, BeanClassLoaderAware { | ||
|
||
private static final BeanNameGenerator DELEGATE = new AnnotationBeanNameGenerator(); | ||
|
||
private ClassLoader beanClassLoader; | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(java.lang.ClassLoader) | ||
*/ | ||
public void setBeanClassLoader(ClassLoader classLoader) { | ||
this.beanClassLoader = classLoader; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.beans.factory.support.BeanNameGenerator#generateBeanName(org.springframework.beans.factory.config.BeanDefinition, org.springframework.beans.factory.support.BeanDefinitionRegistry) | ||
*/ | ||
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { | ||
|
||
AnnotatedBeanDefinition beanDefinition = new AnnotatedGenericBeanDefinition(getRepositoryInterfaceFrom(definition)); | ||
return DELEGATE.generateBeanName(beanDefinition, registry); | ||
} | ||
|
||
/** | ||
* Returns the type configured for the {@code repositoryInterface} property of the given bean definition. Uses a | ||
* potential {@link Class} being configured as is or tries to load a class with the given value's {@link #toString()} | ||
* representation. | ||
* | ||
* @param beanDefinition | ||
* @return | ||
*/ | ||
private Class<?> getRepositoryInterfaceFrom(BeanDefinition beanDefinition) { | ||
|
||
Object value = beanDefinition.getPropertyValues().getPropertyValue("repositoryInterface").getValue(); | ||
|
||
if (value instanceof Class<?>) { | ||
return (Class<?>) value; | ||
} else { | ||
try { | ||
return ClassUtils.forName(value.toString(), beanClassLoader); | ||
} catch (Exception o_O) { | ||
throw new RuntimeException(o_O); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
.../java/org/springframework/data/repository/config/RepositoryBeanNameGeneratorUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2012 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 | ||
* | ||
* http://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 static org.hamcrest.CoreMatchers.*; | ||
import static org.junit.Assert.*; | ||
|
||
import javax.inject.Named; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.beans.factory.support.BeanDefinitionBuilder; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.beans.factory.support.BeanNameGenerator; | ||
import org.springframework.beans.factory.support.DefaultListableBeanFactory; | ||
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport; | ||
|
||
/** | ||
* Unit tests for {@link RepositoryBeanNameGenerator}. | ||
* | ||
* @author Oliver Gierke | ||
*/ | ||
public class RepositoryBeanNameGeneratorUnitTest { | ||
|
||
BeanNameGenerator generator; | ||
BeanDefinitionRegistry registry; | ||
|
||
@Before | ||
public void setUp() { | ||
|
||
RepositoryBeanNameGenerator generator = new RepositoryBeanNameGenerator(); | ||
generator.setBeanClassLoader(Thread.currentThread().getContextClassLoader()); | ||
|
||
this.generator = generator; | ||
this.registry = new DefaultListableBeanFactory(); | ||
} | ||
|
||
@Test | ||
public void usesPlainClassNameIfNoAnnotationPresent() { | ||
assertThat(generator.generateBeanName(getBeanDefinitionFor(MyRepository.class), registry), is("myRepository")); | ||
} | ||
|
||
@Test | ||
public void usesAnnotationValueIfAnnotationPresent() { | ||
assertThat(generator.generateBeanName(getBeanDefinitionFor(AnnotatedInterface.class), registry), is("specialName")); | ||
} | ||
|
||
private BeanDefinition getBeanDefinitionFor(Class<?> repositoryInterface) { | ||
|
||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(RepositoryFactoryBeanSupport.class); | ||
builder.addPropertyValue("repositoryInterface", repositoryInterface.getName()); | ||
return builder.getBeanDefinition(); | ||
} | ||
|
||
interface PlainInterface { | ||
|
||
} | ||
|
||
@Named("specialName") | ||
interface AnnotatedInterface { | ||
|
||
} | ||
} |