本文介绍了使用Spring注释自动应用Hibernate Interceptor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的服务类中,我需要可用的hibernate会话。我目前在beans.xml中执行此操作:

In my service class I need the hibernate session available. I currently do this in the beans.xml:

<bean id = "userDao" class="org.springframework.aop.framework.ProxyFactoryBean">
 <property name="target">
   <ref bean="userDaoTarget" />
 </property>

 <property name="proxyInterfaces">
   <value>com.app.dao.UserDao</value>
 </property>

 <property name="interceptorNames">
   <list>
     <value>hibernateInterceptor</value>
   </list>
 </property>

 <qualifier value="proxy" />
</bean>

...

<bean id="hibernateInterceptor"
   class="org.springframework.orm.hibernate3.HibernateInterceptor">
 <property name="sessionFactory">
   <ref bean="sessionFactory" />
 </property>
<bean>

(手工复制,可能是一些错别字..)

(copied by hand, may be some typos..)

我正在转向使用XML注释,我想知道是否有一种方法可以使用它们配置代理,因为我上面的包括hibernate拦截器?如果没有 - 有没有办法可以减少XML的数量(大约7个DAO使它变得非常混乱)

I'm moving to using annotations over XML, I was wondering if there was a way to use them to configure the proxy as I have above including the hibernate interceptor? If not - is there a way that I can reduce the amount of XML (with about 7 DAOs it makes it very cluttered)

推荐答案

好的,我们走吧。你说

启用以下方面

package br.com.ar.aop;

@Aspect
public class HibernateInterceptorAdvice {

     @Autowired
     private HibernateInterceptor hibernateInterceptor;

     /**
       * I suppose your DAO's live in com.app.dao package
       */
     @Around("execution(* com.app.dao.*(..))")
     public Object interceptCall(ProceedingJoinPoint joinPoint) throws Throwable {
         ProxyFactory proxyFactory = new ProxyFactory(joinPoint.getTarget());
         proxyFactory.addAdvice(hibernateInterceptor);

         Class [] classArray = new Class[joinPoint.getArgs().length];
         for (int i = 0; i < classArray.length; i++)
             classArray[i] = joinPoint.getArgs()[i].getClass();

         return
             proxyFactory
                 .getProxy()
                 .getClass()
                 .getDeclaredMethod(joinPoint.getSignature().getName(), classArray)
                 .invoke(proxyFactory.getProxy(), joinPoint.getArgs());
     }

}

但请记住如果你的DAO实现了一些接口(例如,UserDAOImpl实现了UserDAO),它就可以工作。在这种情况下,Spring AOP使用JDK动态代理。如果您没有任何界面,则可以依赖IDE来使用提取界面重构代码

But keep in mind It just works if your DAO's implements some interface (For instance, UserDAOImpl implements UserDAO). Spring AOP uses JDK dynamic proxy in this case. If you does not have any interface, you can rely on your IDE To refactor your code by using Extract interface

按如下方式声明您的xml(请注意我使用的是Spring 2.5 xsd架构。

Declare your xml as follows (Be aware i am using Spring 2.5 xsd schema)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-2.5.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <!--SessionFactory settings goes here-->
    <bean class="org.springframework.orm.hibernate3.HibernateInterceptor">
        <property name="sessionFactory" ref="sessionFactory"/>
    <bean>
    <!--To enable AspectJ AOP-->
    <aop:aspectj-autoproxy/>
    <!--Your advice-->
    <bean class="br.com.ar.aop.HibernateInterceptorAdvice"/>
    <!--Looks for any annotated Spring bean in com.app.dao package-->
    <context:component-scan base-package="com.app.dao"/>
    <!--Enables @Autowired annotation-->
    <context:annotation-config/>
</beans>

不要忘记除了Spring库之外还要加入类路径

<SPRING_HOME>/lib/asm
<SPRING_HOME>/lib/aopalliance
<SPRING_HOME>/lib/aspectj

这篇关于使用Spring注释自动应用Hibernate Interceptor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:45