DATAJPA-564 - Added EvaluationContextExtension SPI for configuring extensions to the EvaluationContext.

This commit is contained in:
Thomas Darimont
2014-07-04 17:16:43 +02:00
committed by Oliver Gierke
parent bcdb2881a5
commit 1a89612f25
16 changed files with 398 additions and 243 deletions

View File

@@ -25,6 +25,7 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
@@ -35,6 +36,8 @@ import org.springframework.data.jpa.domain.sample.User;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.jpa.repository.sample.UserRepository;
import org.springframework.data.jpa.repository.sample.UserRepositoryImpl;
import org.springframework.data.jpa.repository.support.EvaluationContextExtension;
import org.springframework.data.jpa.repository.support.ExtensibleEvaluationContextProvider;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
import org.springframework.data.repository.core.NamedQueries;
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
@@ -45,6 +48,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
* Integration test for {@link UserRepository} using JavaConfig.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
@ContextConfiguration(inheritLocations = false, loader = AnnotationConfigContextLoader.class)
public class JavaConfigUserRepositoryTests extends UserRepositoryTests {
@@ -57,7 +61,15 @@ public class JavaConfigUserRepositoryTests extends UserRepositoryTests {
@Autowired BeanFactory beanFactory;
@Bean
public UserRepository userRepository() throws IOException {
public EvaluationContextExtension sampleEvaluationContextExtension() {
return new SampleEvaluationContextExtension();
}
@Bean
public UserRepository userRepository() throws Exception {
ExtensibleEvaluationContextProvider evaluationContextProvider = new ExtensibleEvaluationContextProvider();
evaluationContextProvider.setApplicationContext((ApplicationContext) beanFactory);
JpaRepositoryFactoryBean<UserRepository, User, Integer> factory = new JpaRepositoryFactoryBean<UserRepository, User, Integer>();
factory.setEntityManager(entityManager);
@@ -65,7 +77,7 @@ public class JavaConfigUserRepositoryTests extends UserRepositoryTests {
factory.setRepositoryInterface(UserRepository.class);
factory.setCustomImplementation(new UserRepositoryImpl());
factory.setNamedQueries(namedQueries());
factory.setExpressionEvaluationContextProvider(new SampleExpressionEvaluationContextProvider());
factory.setExpressionEvaluationContextProvider(evaluationContextProvider);
factory.afterPropertiesSet();
return factory.getObject();

View File

@@ -15,18 +15,27 @@
*/
package org.springframework.data.jpa.repository;
import java.util.Collections;
import java.util.Map;
import org.springframework.data.jpa.repository.SampleSecurity.SampleSecurityContextHolder;
import org.springframework.data.jpa.repository.support.EvaluationContextProvider;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.data.jpa.repository.support.DefaultEvaluationContextExtension;
import org.springframework.data.jpa.repository.support.EvaluationContextExtension;
/**
* A sample implementation of a custom {@link EvaluationContextExtension}.
*
* @author Thomas Darimont
*/
public class SampleExpressionEvaluationContextProvider implements EvaluationContextProvider {
public class SampleEvaluationContextExtension extends DefaultEvaluationContextExtension {
@Override
public EvaluationContext getEvaluationContext() {
return new StandardEvaluationContext(SampleSecurityContextHolder.getCurrent());
public String getScope() {
return "security";
}
@Override
public Map<String, Object> getProperties() {
return Collections.singletonMap("principal", SampleSecurityContextHolder.getCurrent().getPrincipal());
}
}

View File

@@ -15,9 +15,9 @@
*/
package org.springframework.data.jpa.repository.support;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.hamcrest.CoreMatchers.*;
import java.io.IOException;
import java.io.Serializable;
@@ -36,6 +36,7 @@ import org.springframework.data.jpa.repository.custom.CustomGenericJpaRepository
import org.springframework.data.jpa.repository.custom.UserCustomExtendedRepository;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -48,17 +49,14 @@ public class JpaRepositoryFactoryUnitTests {
JpaRepositoryFactory factory;
@Mock
EntityManager entityManager;
@Mock
@SuppressWarnings("rawtypes")
JpaEntityInformation metadata;
@Mock EntityManager entityManager;
@Mock @SuppressWarnings("rawtypes") JpaEntityInformation metadata;
@Before
public void setUp() {
// Setup standard factory configuration
factory = new JpaRepositoryFactory(entityManager) {
factory = new JpaRepositoryFactory(entityManager, StandardEvaluationContextProvider.INSTANCE) {
@Override
@SuppressWarnings("unchecked")
@@ -67,6 +65,8 @@ public class JpaRepositoryFactoryUnitTests {
return metadata;
};
};
factory.setQueryLookupStrategyKey(Key.CREATE_IF_NOT_FOUND);
}
/**
@@ -125,6 +125,7 @@ public class JpaRepositoryFactoryUnitTests {
public void createsProxyWithCustomBaseClass() {
JpaRepositoryFactory factory = new CustomGenericJpaRepositoryFactory(entityManager);
factory.setQueryLookupStrategyKey(Key.CREATE_IF_NOT_FOUND);
UserCustomExtendedRepository repository = factory.getRepository(UserCustomExtendedRepository.class);
repository.customMethod(1);