Search in sources :

Example 16 with SpelAwareProxyProjectionFactory

use of org.springframework.data.projection.SpelAwareProxyProjectionFactory in project spring-data-commons by spring-projects.

the class ProjectingJackson2HttpMessageConverter method initProjectionFactory.

/**
 * Creates a new {@link SpelAwareProxyProjectionFactory} with the {@link JsonProjectingMethodInterceptorFactory}
 * registered for the given {@link ObjectMapper}.
 *
 * @param mapper must not be {@literal null}.
 * @return
 */
private static SpelAwareProxyProjectionFactory initProjectionFactory(ObjectMapper mapper) {
    Assert.notNull(mapper, "ObjectMapper must not be null!");
    SpelAwareProxyProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
    projectionFactory.registerMethodInvokerFactory(new JsonProjectingMethodInterceptorFactory(new JacksonMappingProvider(mapper)));
    return projectionFactory;
}
Also used : JacksonMappingProvider(com.jayway.jsonpath.spi.mapper.JacksonMappingProvider) SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory)

Example 17 with SpelAwareProxyProjectionFactory

use of org.springframework.data.projection.SpelAwareProxyProjectionFactory in project spring-data-commons by spring-projects.

the class RepositoryFactorySupport method getProjectionFactory.

/**
 * Returns the {@link ProjectionFactory} to be used with the repository instances created.
 *
 * @param classLoader will never be {@literal null}.
 * @param beanFactory will never be {@literal null}.
 * @return will never be {@literal null}.
 */
protected ProjectionFactory getProjectionFactory(ClassLoader classLoader, BeanFactory beanFactory) {
    SpelAwareProxyProjectionFactory factory = new SpelAwareProxyProjectionFactory();
    factory.setBeanClassLoader(classLoader);
    factory.setBeanFactory(beanFactory);
    return factory;
}
Also used : SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory)

Example 18 with SpelAwareProxyProjectionFactory

use of org.springframework.data.projection.SpelAwareProxyProjectionFactory in project spring-data-keyvalue by spring-projects.

the class SpelQueryCreatorUnitTests method createQueryForMethodWithArgs.

private KeyValueQuery<SpelExpression> createQueryForMethodWithArgs(String methodName, Object... args) throws NoSuchMethodException, SecurityException {
    Class<?>[] argTypes = new Class<?>[args.length];
    if (!ObjectUtils.isEmpty(args)) {
        for (int i = 0; i < args.length; i++) {
            argTypes[i] = args[i].getClass();
        }
    }
    Method method = PersonRepository.class.getMethod(methodName, argTypes);
    doReturn(Person.class).when(metadataMock).getReturnedDomainClass(method);
    PartTree partTree = new PartTree(method.getName(), method.getReturnType());
    SpelQueryCreator creator = new SpelQueryCreator(partTree, new ParametersParameterAccessor(new QueryMethod(method, metadataMock, new SpelAwareProxyProjectionFactory()).getParameters(), args));
    KeyValueQuery<SpelExpression> q = creator.createQuery();
    q.getCriteria().setEvaluationContext(new StandardEvaluationContext(args));
    return q;
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) QueryMethod(org.springframework.data.repository.query.QueryMethod) SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) QueryMethod(org.springframework.data.repository.query.QueryMethod) Method(java.lang.reflect.Method) PartTree(org.springframework.data.repository.query.parser.PartTree)

Example 19 with SpelAwareProxyProjectionFactory

use of org.springframework.data.projection.SpelAwareProxyProjectionFactory in project spring-data-mongodb by spring-projects.

the class MongoQueryMethodUnitTests method rejectsNullMappingContext.

@Test
public void rejectsNullMappingContext() throws Exception {
    Method method = PersonRepository.class.getMethod("findByFirstname", String.class, Point.class);
    assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> new MongoQueryMethod(method, new DefaultRepositoryMetadata(PersonRepository.class), new SpelAwareProxyProjectionFactory(), null));
}
Also used : SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory) DefaultRepositoryMetadata(org.springframework.data.repository.core.support.DefaultRepositoryMetadata) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 20 with SpelAwareProxyProjectionFactory

use of org.springframework.data.projection.SpelAwareProxyProjectionFactory in project spring-data-mongodb by spring-projects.

the class StringBasedAggregationUnitTests method createAggregationForMethod.

private StringBasedAggregation createAggregationForMethod(String name, Class<?>... parameters) {
    Method method = ClassUtils.getMethod(SampleRepository.class, name, parameters);
    ProjectionFactory factory = new SpelAwareProxyProjectionFactory();
    MongoQueryMethod queryMethod = new MongoQueryMethod(method, new DefaultRepositoryMetadata(SampleRepository.class), factory, converter.getMappingContext());
    return new StringBasedAggregation(queryMethod, operations, PARSER, QueryMethodEvaluationContextProvider.DEFAULT);
}
Also used : SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory) ProjectionFactory(org.springframework.data.projection.ProjectionFactory) SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory) DefaultRepositoryMetadata(org.springframework.data.repository.core.support.DefaultRepositoryMetadata) Method(java.lang.reflect.Method)

Aggregations

SpelAwareProxyProjectionFactory (org.springframework.data.projection.SpelAwareProxyProjectionFactory)28 Method (java.lang.reflect.Method)17 DefaultRepositoryMetadata (org.springframework.data.repository.core.support.DefaultRepositoryMetadata)15 ProjectionFactory (org.springframework.data.projection.ProjectionFactory)11 Test (org.junit.Test)6 Test (org.junit.jupiter.api.Test)4 PartTree (org.springframework.data.repository.query.parser.PartTree)4 QueryMethod (org.springframework.data.repository.query.QueryMethod)3 JacksonMappingProvider (com.jayway.jsonpath.spi.mapper.JacksonMappingProvider)2 ArrayList (java.util.ArrayList)2 Document (org.bson.Document)2 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)2 Query (org.springframework.data.mongodb.core.query.Query)2 ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)2 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 MappingProvider (com.jayway.jsonpath.spi.mapper.MappingProvider)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 JsonParseException (org.bson.json.JsonParseException)1 Before (org.junit.Before)1