2010-01-25 23 views
7

Nella mia classe di servizio ho bisogno della sessione di sospensione disponibile. Io attualmente faccio nel beans.xml:Utilizzare le annotazioni Spring per applicare automaticamente Hibernate Interceptor?

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

(copiato a mano, possono essere alcuni errori di battitura ..)

mi sto muovendo a utilizzare le annotazioni oltre XML, mi chiedevo se ci fosse un modo di usarli per configurare il proxy come ho sopra incluso l'interceptor di ibernazione? In caso contrario, c'è un modo per ridurre la quantità di XML (con circa 7 DAO lo rende molto ingombrante)

risposta

8

Ok, andiamo. Hai detto

mi sto muovendo a usare annotazioni su XML

Abilitare un aspetto come segue

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

} 

Ma tenere a mente Funziona solo se l'attrezzi del DAO po 'di interfaccia (Ad esempio, UserDAOImpl implementa UserDAO). Spring AOP utilizza il proxy dinamico JDK in questo caso. Se non ha un'interfaccia, è possibile contare sul vostro IDE Per refactoring del codice utilizzando l'interfaccia estratto

dichiarare il vostro XML come segue (Siate consapevoli sto usando Primavera schema 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> 

non dimenticare per mettere nel classpath oltre librerie primavera

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

Grazie - sembra esattamente quello di cui ho bisogno! –

0

Dai un'occhiata all'annotazione @Autowired.

+1

a meno che non ho capito male, non è possibile utilizzare per aggiungere Autowired intercettori? –

Problemi correlati