Spring Data Cassandra无法注册自定义存储库

时间:2014-12-09 21:59:47

标签: spring cassandra spring-data spring-data-cassandra

我在spring-data-cassandra 1.1.1.RELEASE上,我正在尝试创建一个自定义存储库。我有一个像

这样的存储库
@Repository
public interface EntityRepository extends TypedIdCassandraRepository<Entity, EntityKey>, EntityRepositoryCustom {    
}

我得到了一个EntityRepositoryCustom:

public interface EntityRepositoryCustom{
    TelemetryPoint getEntityByDeviceAndDate(Device device, DateTime dateTime);
}

及其impl:

public class EntityRepositoryImpl implements EntityRepositoryCustom {
    @Autowired
    CassandraOperations template;

    @Override
    public Entity getEntityByDeviceAndDate(Device device, DateTime dateTime) {
        Select select = QueryBuilder.select().from("entity");

        Clause deviceClause = QueryBuilder.eq("device_id", device.getDeviceId());
        Clause dateClause = QueryBuilder.eq("datetime", dateTime);
        select.where(deviceClause).and(dateClause);

        Entity one = template.selectOne(select, Entity.class);
        return one;
    }
}

启动Spring启动应用程序时,似乎无法发现自定义存储库impl类。我最终会遇到这样的异常:

Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy118]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy118
    at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:212)
    at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:109)
    at org.springframework.aop.framework.AbstractAdvisingBeanPostProcessor.postProcessAfterInitialization(AbstractAdvisingBeanPostProcessor.java:91)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.postProcessObjectFromFactoryBean(AbstractAutowireCapableBeanFactory.java:1713)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:113)
    ... 37 more

Caused by: java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy118
    at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
    at org.springframework.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33)
    at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
    at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:317)
    at org.springframework.aop.framework.ObjenesisCglibAopProxy.createProxyClassAndInstance(ObjenesisCglibAopProxy.java:57)
    at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:202)
    ... 42 more

请注意,我在JDK1.8_25上。类似的基于JPA的自定义存储库没有任何问题。

2 个答案:

答案 0 :(得分:2)

根据此spring-boot GitHub issue任何自定义存储库,它扩展了Spring Data的Repository不需要@Repository注释。

如果这没有帮助,将spring.dao.exceptiontranslation.enabled=false添加到我的application.properties对我有用。

答案 1 :(得分:1)

您是否尝试删除存储库界面上的@Repository注释?实际上,您应该在自定义存储库实现(@Component)之上添加一些EntityRepositoryImpl味道。