2010-01-25 39 views
7

Trong lớp dịch vụ của tôi, tôi cần phiên ngủ đông. Tôi hiện làm điều này trong beans.xml:Sử dụng chú thích Spring để tự động áp dụng Interceptor Hibernate?

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

(sao chép bằng tay, có thể có một số lỗi chính tả ..)

Tôi đang chuyển sang sử dụng các chú thích trên XML, tôi đã tự hỏi nếu có một cách nào để sử dụng chúng để định cấu hình proxy như tôi có ở trên bao gồm trình chặn chặn ngủ đông? Nếu không - là có một cách mà tôi có thể giảm số lượng XML (với khoảng 7 DAO nó làm cho nó rất lộn xộn)

Trả lời

8

Ok, đi thôi. Bạn nói

Tôi đang chuyển sang sử dụng chú thích trên XML

Enable một khía cạnh như sau

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

} 

Nhưng hãy nhớ Nó chỉ hoạt động nếu dụng cụ của DAO của bạn một số giao diện (Ví dụ: UserDAOImpl triển khai UserDAO). Spring AOP sử dụng JDK proxy động trong trường hợp này. Nếu bạn không có bất kỳ giao diện, bạn có thể dựa vào IDE của bạn Để Refactor mã của bạn bằng cách sử dụng Extract giao diện

Khai xml của bạn như sau (Hãy nhận biết tôi đang sử dụng Spring 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> 

Đừng quên để đặt trong classpath bên cạnh thư viện mùa xuân

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

Cảm ơn bạn - điều này có vẻ chính xác như những gì tôi cần! –

0

Hãy xem chú thích @Autowired.

+1

Trừ khi tôi đã hiểu lầm, bạn không thể sử dụng Autowired để thêm tên lửa đánh chặn? –

Các vấn đề liên quan