繁体   English   中英

使用Spring注释自动应用Hibernate Interceptor?

[英]Use Spring annotations to automatically apply Hibernate Interceptor?

在我的服务类中,我需要可用的hibernate会话。 我目前在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>

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

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

好了,走吧。 你说

我正在转向使用XML注释

启用以下方面

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来使用Extract界面重构代码

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

<?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

看看@Autowired注释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM