Skip to content

Commit 31618f9

Browse files
committed
DATACMNS-299 - Default scope of CDI repository beans to application scope.
Polished JavaDoc along the way.
1 parent c69330b commit 31618f9

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

src/main/java/org/springframework/data/repository/cdi/CdiRepositoryBean.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011 the original author or authors.
2+
* Copyright 2011-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import java.util.HashSet;
2323
import java.util.Set;
2424

25+
import javax.enterprise.context.ApplicationScoped;
2526
import javax.enterprise.context.spi.CreationalContext;
2627
import javax.enterprise.inject.Alternative;
2728
import javax.enterprise.inject.Stereotype;
@@ -52,6 +53,7 @@ public abstract class CdiRepositoryBean<T> implements Bean<T> {
5253
*
5354
* @param qualifiers must not be {@literal null}.
5455
* @param repositoryType has to be an interface must not be {@literal null}.
56+
* @param beanManager the CDI {@link BeanManager}, must not be {@literal null}.
5557
*/
5658
public CdiRepositoryBean(Set<Annotation> qualifiers, Class<T> repositoryType, BeanManager beanManager) {
5759

@@ -85,11 +87,11 @@ public Set<Type> getTypes() {
8587
}
8688

8789
/**
88-
* Returns an instance of an {@link EntityManager}.
90+
* Returns an instance of an the given {@link Bean}.
8991
*
90-
* @param beanManager The BeanManager.
91-
* @param bean The bean representing an EntityManager.
92-
* @return The EntityManager instance.
92+
* @param bean the {@link Bean} about to create an instance for.
93+
* @param type the expected type of the componentn instance created for that {@link Bean}.
94+
* @return the actual component instance.
9395
*/
9496
@SuppressWarnings("unchecked")
9597
protected <S> S getDependencyInstance(Bean<S> bean, Class<S> type) {
@@ -106,6 +108,7 @@ public final T create(CreationalContext<T> creationalContext) {
106108
if (LOGGER.isDebugEnabled()) {
107109
LOGGER.debug(String.format("Creating bean instance for repository type '%s'.", repositoryType.getName()));
108110
}
111+
109112
return create(creationalContext, repositoryType);
110113
}
111114

@@ -189,9 +192,19 @@ public Set<InjectionPoint> getInjectionPoints() {
189192
return Collections.emptySet();
190193
}
191194

195+
/*
196+
* (non-Javadoc)
197+
* @see javax.enterprise.inject.spi.Bean#getScope()
198+
*/
199+
public Class<? extends Annotation> getScope() {
200+
return ApplicationScoped.class;
201+
}
202+
192203
/**
193-
* @param creationalContext
194-
* @param repositoryType
204+
* Creates the actual component instance.
205+
*
206+
* @param creationalContext will never be {@literal null}.
207+
* @param repositoryType will never be {@literal null}.
195208
* @return
196209
*/
197210
protected abstract T create(CreationalContext<T> creationalContext, Class<T> repositoryType);

src/test/java/org/springframework/data/repository/cdi/CdiRepositoryBeanUnitTests.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011 the original author or authors.
2+
* Copyright 2011-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,9 @@
2525
import java.util.Collections;
2626
import java.util.Set;
2727

28+
import javax.enterprise.context.ApplicationScoped;
2829
import javax.enterprise.context.spi.CreationalContext;
30+
import javax.enterprise.inject.spi.Bean;
2931
import javax.enterprise.inject.spi.BeanManager;
3032

3133
import org.junit.Test;
@@ -96,16 +98,24 @@ public void detectsStereotypes() {
9698
assertThat(stereotypes, hasItem(StereotypeAnnotation.class));
9799
}
98100

101+
/**
102+
* @see DATACMNS-299
103+
*/
104+
@Test
105+
@SuppressWarnings("rawtypes")
106+
public void scopeDefaultsToApplicationScoped() {
107+
108+
Bean<SampleRepository> bean = new DummyCdiRepositoryBean<SampleRepository>(NO_ANNOTATIONS, SampleRepository.class,
109+
beanManager);
110+
assertThat(bean.getScope(), equalTo((Class) ApplicationScoped.class));
111+
}
112+
99113
static class DummyCdiRepositoryBean<T> extends CdiRepositoryBean<T> {
100114

101115
public DummyCdiRepositoryBean(Set<Annotation> qualifiers, Class<T> repositoryType, BeanManager beanManager) {
102116
super(qualifiers, repositoryType, beanManager);
103117
}
104118

105-
public Class<? extends Annotation> getScope() {
106-
return null;
107-
}
108-
109119
@Override
110120
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType) {
111121
return null;

0 commit comments

Comments
 (0)