【问题标题】:Applying a hibernate filter using Spring AOP使用 Spring AOP 应用休眠过滤器
【发布时间】:2017-03-28 09:35:43
【问题描述】:

我正在尝试使用 Spring AOP 对数据库表应用过滤器。我希望此过滤器仅适用于此表。我已经配置了一个 Aspect 来应用过滤器,但我一定是配置不正确。

连接点功能似乎从未被执行。我希望它从休眠的 SessionFactory.getCurrentSession() 中检索会话并对其应用过滤器。

我的方面类:

@Aspect
@Component
public class MyAspect {

@Pointcut("execution(* org.hibernate.SessionFactory.getCurrentSession(..))")
protected void hibernateSessionFetch() {

}


@AfterReturning(pointcut = "hibernateSessionFetch()", returning = "result")
public void enableFilter(JoinPoint joinPoint, Object result) {

    System.out.println("*********** ASPECT ************");

    Session session = (Session) result;

    User u = new User("John Smith", 20);

    // apply the filter
    Filter filter = session.enableFilter("restrictToUser");
    filter.setParameter("name", u.getName());

}

我的目标班级:

@Entity
@Table(name = "USER")
@org.hibernate.annotations.DynamicInsert
@org.hibernate.annotations.Filter(
  name = "restrictToUser",
  condition="name = :name"
)
@Component
public class User extends com.system.admin.SkeletonDTO<User>
  implements java.io.Serializable {
// user information
}

在 XML 中我是这样配置的:

<context:annotation-config></context:annotation-config>
<aop:aspectj-autoproxy proxy-target-class="true"/>

有人知道我做错了什么吗?

【问题讨论】:

  • 如果没有组件扫描,您的方面将不会被拾取。还要确保它与您的LocalSessionFactoryBean 在同一个ApplicationContext 中。
  • 嗨@M.Deinum,不幸的是这不起作用。我还尝试添加注释 @Configurable 以及 &lt;context:spring-configured /&gt; 但这也不起作用。
  • 这不会改变任何事情。如前所述,确保它在相同的上下文中,并且您的 SessionFactory 是一个实际的 bean(否则它将不起作用)。

标签: java spring hibernate aop aspectj


【解决方案1】:

按以下方式更改您的班级:

@Aspect
@Component
public class MyAspect {

@Pointcut("execution (* org.hibernate.internal.SessionFactoryImpl.SessionBuilderImpl.openSession(..))")
protected void hibernateSessionFetch() {

}


@AfterReturning(pointcut = "hibernateSessionFetch()", returning = "result")
public void enableFilter(JoinPoint joinPoint, Object result) {

    System.out.println("*********** ASPECT ************");

    if (result!= null && Session.class.isInstance(result)) {
        Session session = (Session) result;

        User u = new User("John Smith", 20);

        // apply the filter
        Filter filter = session.enableFilter("restrictToUser");
        filter.setParameter("name", u.getName());
    }
}

然后尝试在 META-INF/aop.xml 中配置 AspectJ Load-Time Weaver

<aspectj>

    <weaver options="-Xreweavable -verbose -showWeaveInfo">
        <include within="org.blablabla.MyAspect"/>
        <include within="org.hibernate.internal.SessionFactoryImpl.SessionBuilderImpl"/>
    </weaver>

    <aspects>
        <aspect name="org.blablabla.MyAspect"/>
    </aspects>

</aspectj>

没有 AspectJ 的替代方法(并且证明有效)是使用 TransactionManagerCustomizers:

@Configuration
public class HibernateFilterConfig {
    @Bean
    @ConditionalOnMissingBean
    public PlatformTransactionManager transactionManager(
            ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
        JpaTransactionManager transactionManager = new JpaTransactionManager() {
            @Override
            @NonNull
            protected EntityManager createEntityManagerForTransaction() {
                final EntityManager entityManager = super.createEntityManagerForTransaction();
                Session session = entityManager.unwrap(Session.class);
                User u = new User("John Smith", 20);
                if (u != null && u.getName() != null) {
                    session.enableFilter("restrictToUser").setParameter("name", u.getName());
                }
                return entityManager;
            }
        };
        transactionManagerCustomizers.ifAvailable((customizers) -> customizers.customize(transactionManager));

        return transactionManager;
    }
}

有用的链接: https://callistaenterprise.se/blogg/teknik/2020/10/17/multi-tenancy-with-spring-boot-part5/ https://github.com/spring-projects/spring-framework/issues/25125

【讨论】:

    猜你喜欢
    • 2019-08-26
    • 1970-01-01
    • 2014-11-16
    • 2017-06-08
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 2011-10-15
    相关资源
    最近更新 更多