Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: spring-projects/spring-data-commons
...
head fork: btalbott/spring-data-commons
  • 3 commits
  • 9 files changed
  • 0 commit comments
  • 2 contributors
Commits on Nov 12, 2012
Spring Buildmaster spring-buildmaster DATACMNS-192 - Prepare next development iteration. 8007e25
Oliver Gierke olivergierke 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.
b4a553e
Commits on Nov 13, 2012
Oliver Gierke olivergierke DATACMNS-247 - Improved implementation of RepositoryBeanDefinitionReg…
…istrarSupport.

RepositoryBeanDefinitionRegistrarSupport not implements BeanClassLoaderAware and ResourceLoaderAware as Spring 3.2 will regard these callback interfaces and inject the resources. We still default these injected resources upon invocation to be able to run on Spring 3.1 as well.
c523abe
2  pom.xml
View
@@ -6,7 +6,7 @@
<name>Spring Data Commons Distribution</name>
<description>Spring Data Commons</description>
<url>http://www.springsource.org/spring-data</url>
- <version>1.4.0.RELEASE</version>
+ <version>1.4.1.BUILD-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
2  spring-data-commons-core/pom.xml
View
@@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons-parent</artifactId>
- <version>1.4.0.RELEASE</version>
+ <version>1.4.1.BUILD-SNAPSHOT</version>
<relativePath>../spring-data-commons-parent/pom.xml</relativePath>
</parent>
11 ...ommons-core/src/main/java/org/springframework/data/repository/config/RepositoryBeanDefinitionParser.java
View
@@ -104,12 +104,17 @@ private void registerGenericRepositoryFactoryBean(
AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
beanDefinition.setSource(configuration.getSource());
+ RepositoryBeanNameGenerator generator = new RepositoryBeanNameGenerator();
+ generator.setBeanClassLoader(parser.getReaderContext().getBeanClassLoader());
+
+ String beanName = generator.generateBeanName(beanDefinition, parser.getRegistry());
+
if (LOG.isDebugEnabled()) {
- LOG.debug("Registering repository: " + configuration.getBeanId() + " - Interface: "
- + configuration.getRepositoryInterface() + " - Factory: " + extension.getRepositoryFactoryClassName());
+ LOG.debug("Registering repository: " + beanName + " - Interface: " + configuration.getRepositoryInterface()
+ + " - Factory: " + extension.getRepositoryFactoryClassName());
}
- BeanComponentDefinition definition = new BeanComponentDefinition(beanDefinition, configuration.getBeanId());
+ BeanComponentDefinition definition = new BeanComponentDefinition(beanDefinition, beanName);
parser.registerBeanComponent(definition);
} catch (RuntimeException e) {
handleError(e, configuration.getConfigurationSource().getElement(), parser.getReaderContext());
71 ...e/src/main/java/org/springframework/data/repository/config/RepositoryBeanDefinitionRegistrarSupport.java
View
@@ -17,21 +17,45 @@
import java.lang.annotation.Annotation;
+import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
+import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
+import org.springframework.util.ClassUtils;
/**
* Base class to implement {@link ImportBeanDefinitionRegistrar}s to enable repository
*
* @author Oliver Gierke
*/
-public abstract class RepositoryBeanDefinitionRegistrarSupport implements ImportBeanDefinitionRegistrar {
+public abstract class RepositoryBeanDefinitionRegistrarSupport implements ImportBeanDefinitionRegistrar,
+ BeanClassLoaderAware, ResourceLoaderAware {
+
+ private ResourceLoader resourceLoader;
+ 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.context.ResourceLoaderAware#setResourceLoader(org.springframework.core.io.ResourceLoader)
+ */
+ public void setResourceLoader(ResourceLoader resourceLoader) {
+ this.resourceLoader = resourceLoader;
+ }
/*
* (non-Javadoc)
@@ -47,13 +71,17 @@ public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanD
return;
}
- ResourceLoader resourceLoader = new DefaultResourceLoader();
+ defaultExternalResources(registry);
+
AnnotationRepositoryConfigurationSource configuration = new AnnotationRepositoryConfigurationSource(
annotationMetadata, getAnnotation());
RepositoryConfigurationExtension extension = getExtension();
extension.registerBeansForRoot(registry, configuration);
+ RepositoryBeanNameGenerator generator = new RepositoryBeanNameGenerator();
+ generator.setBeanClassLoader(beanClassLoader);
+
for (RepositoryConfiguration<AnnotationRepositoryConfigurationSource> repositoryConfiguration : extension
.getRepositoryConfigurations(configuration, resourceLoader)) {
@@ -62,11 +90,48 @@ public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanD
extension.postProcess(definitionBuilder, configuration);
- registry.registerBeanDefinition(repositoryConfiguration.getBeanId(), definitionBuilder.getBeanDefinition());
+ String beanName = generator.generateBeanName(definitionBuilder.getBeanDefinition(), registry);
+ registry.registerBeanDefinition(beanName, definitionBuilder.getBeanDefinition());
}
}
/**
+ * Workaround the lack of injectability of external resources into {@link ImportBeanDefinitionRegistrar}s in the
+ * Spring 3.1 timeline. We populate {@link #beanClassLoader} and default the {@link #resourceLoader} in case they
+ * haven't been set until a call to this method.
+ *
+ * @param registry must not be {@literal null}.
+ * @see SPR-9568
+ */
+ private void defaultExternalResources(BeanDefinitionRegistry registry) {
+
+ if (beanClassLoader == null) {
+ this.beanClassLoader = getBeanClassLoader(registry);
+ }
+
+ if (resourceLoader == null) {
+ this.resourceLoader = new DefaultResourceLoader(this.beanClassLoader);
+ }
+ }
+
+ /**
+ * Returns the bean class loader contained in the given registry if it is a {@link ConfigurableBeanFactory}. Falls
+ * back to the {@link ResourceLoader}'s {@link ClassLoader} or the global default one if that one is {@literal null}
+ * in turn.
+ *
+ * @param registry must not be {@literal null}.
+ * @return
+ */
+ private ClassLoader getBeanClassLoader(BeanDefinitionRegistry registry) {
+
+ if (registry instanceof ConfigurableBeanFactory) {
+ return ((ConfigurableBeanFactory) registry).getBeanClassLoader();
+ }
+
+ return resourceLoader == null ? ClassUtils.getDefaultClassLoader() : resourceLoader.getClassLoader();
+ }
+
+ /**
* Return the annotation to obtain configuration information from. Will be wrappen into an
* {@link AnnotationRepositoryConfigurationSource} so have a look at the constants in there for what annotation
* attributes it expects.
80 ...a-commons-core/src/main/java/org/springframework/data/repository/config/RepositoryBeanNameGenerator.java
View
@@ -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);
+ }
+ }
+ }
+}
3  ...-data-commons-core/src/main/java/org/springframework/data/repository/config/RepositoryConfiguration.java
View
@@ -17,6 +17,7 @@
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.data.repository.query.QueryLookupStrategy;
/**
@@ -30,7 +31,9 @@
* Returns the id of the {@link BeanDefinition} the repository shall be registered under.
*
* @return
+ * @deprecated bean ids should be determined using a {@link BeanNameGenerator} during classpath scanning.
*/
+ @Deprecated
String getBeanId();
/**
1  ...re/src/test/java/org/springframework/data/repository/config/DefaultRepositoryConfigurationUnitTests.java
View
@@ -41,7 +41,6 @@ public void supportsBasicConfiguration() {
RepositoryConfiguration<RepositoryConfigurationSource> configuration = new DefaultRepositoryConfiguration<RepositoryConfigurationSource>(
source, "com.acme.MyRepository");
- assertThat(configuration.getBeanId(), is("myRepository"));
assertThat(configuration.getConfigurationSource(), is(source));
assertThat(configuration.getImplementationBeanName(), is("myRepositoryImpl"));
assertThat(configuration.getImplementationClassName(), is("MyRepositoryImpl"));
77 ...s-core/src/test/java/org/springframework/data/repository/config/RepositoryBeanNameGeneratorUnitTest.java
View
@@ -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 {
+
+ }
+}
2  spring-data-commons-parent/pom.xml
View
@@ -6,7 +6,7 @@
<name>Spring Data Commons Parent</name>
<description>Spring Data Commons Parent</description>
<url>http://www.springsource.org/spring-data</url>
- <version>1.4.0.RELEASE</version>
+ <version>1.4.1.BUILD-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>

No commit comments for this range

Something went wrong with that request. Please try again.