【问题标题】:Use Spring annotations to automatically apply Hibernate Interceptor?使用Spring注解自动应用Hibernate Interceptor?
【发布时间】:2010-01-25 12:22:47
【问题描述】:

在我的服务类中,我需要可用的休眠会话。我目前在 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 上的注释,我想知道是否有一种方法可以像上面那样使用它们来配置代理包括休眠拦截器?如果没有 - 有没有办法可以减少 XML 的数量(大约 7 个 DAO 使它非常混乱)

【问题讨论】:

    标签: java spring


    【解决方案1】:

    好的,我们走吧。你说

    我正在转向使用 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 interface

    如下声明您的 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
    

    【讨论】:

    • 谢谢 - 这看起来正是我需要的!
    【解决方案2】:

    看看@Autowired注解。

    【讨论】:

    • 除非我理解错了,否则不能使用Autowired添加拦截器?
    猜你喜欢
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2018-12-10
    • 2014-03-30
    • 1970-01-01
    相关资源
    最近更新 更多