简体   繁体   English

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

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

In my service class I need the hibernate session available. 在我的服务类中,我需要可用的hibernate会话。 I currently do this in the beans.xml: 我目前在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..) (手工复制,可能是一些错别字..)

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 ? 我正在使用注释而不是XML,我想知道是否有一种方法可以使用它们配置代理,因为我上面包括hibernate拦截器 If not - is there a way that I can reduce the amount of XML (with about 7 DAOs it makes it very cluttered) 如果没有 - 有没有办法可以减少XML的数量(大约7个DAO使它变得非常混乱)

Ok, Let's go. 好了,走吧。 You said 你说

I am moving to using annotations over XML 我正在转向使用XML注释

Enable an aspect as follows 启用以下方面

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());
     }

}

But keep in mind It just works if your DAO's implements some interface (For instance, UserDAOImpl implements UserDAO). 但请记住,如果您的DAO实现了某些接口 (例如,UserDAOImpl实现UserDAO),它就可以正常工作。 Spring AOP uses JDK dynamic proxy in this case. 在这种情况下,Spring AOP使用JDK动态代理。 If you does not have any interface, you can rely on your IDE To refactor your code by using Extract interface 如果您没有任何界面,则可以依赖IDE来使用Extract界面重构代码

Declare your xml as follows (Be aware i am using Spring 2.5 xsd schema) 按如下方式声明你的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>

Do not forget To put in the classpath besides Spring libraries 不要忘记 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