当前位置: 首页 > 知识库问答 >
问题:

Spring数据存储库的方面建议不起作用

关昊天
2023-03-14

我试图创建一些切入点和之前的建议为存储库,以便能够在Spring启动的Spring数据中过滤一些存储库的实体管理器。我在项目中也有Web和服务层,AounLogging适用于两者。但是我不能为存储库做同样的事情。我已经挣扎了2天,我尝试了很多东西来修复它。我几乎阅读了关于此的每个文档、问题和线程(代理问题CGlib和JDK代理等)。我使用jhipster创建项目。

除了@Pointcut和CrudRepository,我无法部署应用程序。甚至其部署的@Before也不需要在存储库中调用方法。我想我有一个类似的问题,比如下面的问题。代理混淆

    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.xxx.zzz.business.repository.ApplyRepository com.xxx.zzz.web.rest.applyResource.ApplyRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applyRepository': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy173]: 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.$Proxy173
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 61 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applyRepository': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy173]: 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.$Proxy173
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:116)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1523)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:314)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 63 more
Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy173]: 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.$Proxy173
    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.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:447)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:333)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:293)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.postProcessObjectFromFactoryBean(AbstractAutowireCapableBeanFactory.java:1719)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:113)
    ... 70 more
Caused by: java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy173
    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:56)
    at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:202)
    ... 77 more

有人知道它可能是什么吗?

类和配置如下所示。

存储库:

public interface ApplyRepository extends JpaRepository<Apply,Long>,QueryDslPredicateExecutor<Apply> {

public Page<Apply> findAll(Predicate predicate, Pageable p); 
}
...
}

数据库配置:

@Configuration
@EnableJpaRepositories("com.xxx.zzz.business.repository")
@EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
@EnableTransactionManagement//(proxyTargetClass = false)
public class DatabaseConfiguration  {
....

AspectJ配置:

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
// @EnableLoadTimeWeaving(aspectjWeaving = ...     )
public class LoggingAspectConfiguration {
...

系统架构:

@Aspect
public class SystemArchitecture {

/**
 * A join point is in the web layer if the method is defined
 * in a type in the com.xyz.someapp.web package or any sub-package
 * under that.W
 */
@Pointcut("within(com.xxx.zzz.web.rest..*)")
public void inWebLayer() {
}

/**
 * A join point is in the service layer if the method is defined
 * in a type in the com.xyz.someapp.service package or any sub-package
 * under that.
 */
@Pointcut("within(com.xxx.zzz.business.service..*)")
public void inServiceLayer() {
}

/**
 * A join point is in the data access layer if the method is defined
 * in a type in the com.xyz.someapp.dao package or any sub-package
 * under that.
 */
@Pointcut("within(com.xxx.zzz.business.repository..*)")
public void inDataAccessLayer() {
}



/**
 *  All layers
 */
@Pointcut("inWebLayer() || inServiceLayer() || inDataAccessLayer()")
public void inALL(){
}



 @Pointcut("within(org.springframework.data.repository.CrudRepository)")
    //@Pointcut("execution(*org.springframework.data.repository.Repository+.*               (..))")
    //@Pointcut("execution(* com.xxx.zzz.business.repository+.*(..))")
    //@Pointcut("execution(* org.springframework.data.jpa.repository.JpaRepository+.*(..))")
     //@Pointcut("execution(*        com.xxx.zzz.business.repository.ApplyRepository.*(..))")
    public void inDATAExec(){}

}

过滤光谱:

@Aspect
@Transactional
public class FilterAspect {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @PersistenceContext
    private EntityManager entitymanager;

    @Before("com.xxx.zzz.aop.logging.SystemArchitecture.inDATAExec())") //  "execution(* com.xxx.zzz.business.repository.InvoiceRepository.*(..))"
    public void doAccessCheck() {
        if (TransactionSynchronizationManager.isActualTransactionActive() && SecurityUtils.isAuthenticated()) {
            Session session = entitymanager.unwrap(Session.class);
            session.enableFilter("GLOBAL_FILTER").setParameter("customerId", SecurityUtils.getCurrentCustomerId());
        }
    }

编辑:我解决了问题。它不知何故与错误的观点和名称有关。我试图更改存储库中自定义注释的切入点。它不适用于方法或类级别。我在下面的链接中读到了这方面的问题。建议1建议2我为目标和注释挣扎了几个小时。但没有结果。在Spring数据存储库中添加自定义注释以进行提示真的不可能吗?

注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
//@Inherited
public @interface CustomerRequired {
    String value() default "customerrequired";
}

存储库:

public interface InvoiceRepository extends JpaRepository<Invoice,String>, QueryDslPredicateExecutor<Invoice> {

    @CustomerRequired
    public Page<Invoice> findAll(Predicate predicate, Pageable p);

     ...
}

切入点和建议:

@Pointcut(value = "@target(customerRequired)",argNames = "customerRequired")
    public void targetCustomer(@SuppressWarnings("unused") CustomerRequired customerRequired) {/**/}


@Before(value = "com.xxx.zzz.aop.logging.SystemArchitecture.targetCustomer(customerRequired) && com.xxx.zzz.aop.logging.SystemArchitecture.inDataLayer()")
public void doAccessCheck(JoinPoint joinPoint, CustomerRequired customerRequired) {
    if (TransactionSynchronizationManager.isActualTransactionActive() && SecurityUtils.isAuthenticated()) {
        Session session = entitymanager.unwrap(Session.class);
        session.enableFilter("GLOBAL_FILTER").setParameter("customerId", SecurityUtils.getCurrentCustomerId());
    }
}

共有1个答案

花和宜
2023-03-14

而不是用

@Pointcut("within(org.springframework.data.repository.CrudRepository)")
public void inDATAExec(){}

使用喜欢跟随

@Pointcut("this(org.springframework.data.repository.Repository)")
public void inDATAExec(){}

它所做的是

any join point (method execution only in Spring AOP) where the 
proxy implements the Repository interface

你可以看看http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html

希望有帮助!

 类似资料:
  • 这根本不是关于Spring靴的。 我的英语可以更好。 使用下面的Spring Data配置,我正在尝试执行DML请求。 恰好是< code>CrudRepository#save方法。 然而,执行Spring的CrudRepository#保存方法,我接下来要做的是: 只有选择由功能记录。 没有执行任何“插入”或“更新”语句来hibernate.show_sql日志记录。 数据库根本没有变化。 =

  • 我是Spring data neo4j的新手,我对GraphRepository有一些错误/问题。 我第一次有了这个: 但是阅读一些文档,存储库已经提供了这样的方法。我不需要写它们。 这是我的产品域名。 这是我的实验班 未检测到查找字节 Id 这正常吗? 这是我的pom.xml

  • 我需要使用spring@Cacheable注释缓存对MongoDB的调用: 不幸的是,使用@Cacheable注释接口中的任何方法都会导致以下异常: 我正在寻找一种方法来缓存对DB的调用(这相当昂贵)。有什么想法吗?

  • 我在java应用程序中遇到以下异常。 我对entity表中的其他3个字段有一个唯一的约束,对attributes表中的name、Value有一个唯一的约束。当我尝试persist an实体(它有一组属性)时,repository.save()方法会失败,原因有两个:1。属性表中可能已经存在一个属性,由于唯一约束(这是我想要的。但我仍然想创建与实体的这些属性的关联)而无法插入2。实体可能已经存在,但

  • 嗨,我有一个使用Spring 5、Spring Data、Spring AOP和Java 11的应用程序(我也在使用JAVA 9模块系统)。我的spring-context/spring-aspects版本是5.1.2.RELEASE Spring数据版本是2.1.2.RELEASE这两个版本都是最新的。我以下列方式创建了存储库: 我已启用Spring Data Repositories使用 在我

  • 我正在处理mysql主从复制。我正在使用Spring data jpa(Spring boot)。 我需要的是所有写入操作都转到主服务器,只读操作平均分配给多个只读从服务器。 为此,我需要: 使用特殊的JDBC驱动程序:com.mysql.jdbc.ReplicationDriver 设置复制:在URL中: 需要关闭自动提交。(*)连接需要设置为只读。 为了确保JDBC连接设置为只读,我创建了一个