2010-10-12 32 views
5

我想使用HibernateInterceptor作爲建議,我試圖autowire它。自動裝配HibernateInterceptor作爲建議

的代碼如下,

@Aspect 
public class InterceptorAdvice{ 


    private HibernateInterceptor hibernateInterceptor; 

    @Autowired 
    public void setHibernateInterceptor(@Qualifier("hibernateInterceptor") HibernateInterceptor hibernateInterceptor) { 
     this.hibernateInterceptor = hibernateInterceptor; 
    } 

    @Around("execution(* *..*.dao..*.*(..))") 
    public Object interceptCall(ProceedingJoinPoint joinPoint) throws Exception { 
     Object obj = null; 
     try{ 
      ....... 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     return obj; 

    } 

} 

以下是我的XML映射,

<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor" autowire="byName"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 
<!--To enable AspectJ AOP--> 
<aop:aspectj-autoproxy/> 
<!--Your advice--> 
<bean class="com.web.aop.InterceptorAdvice"/> 
<!--Looks for any annotated Spring bean in com.app.dao package--> 
<context:component-scan base-package="com.web.dao"/> 
<!--Enables @Autowired annotation--> 
<context:annotation-config/> 

當我檢查hibernateInterceptop,我得到的是NULL:(......不知道爲什麼它無法自動導入休眠攔截器

任何想法?感謝您的時間

乾杯, J

+0

當您在Spring配置中爲'HibernateInterceptor'手動設置另一個屬性時會發生什麼? – 2010-10-15 15:42:23

回答

0

你有幾個攔截器?

如果您在@Qualifier中使用@Autowired,我建議您改用@Resource。這更多...標準。

@Resource(name="hibernateInterceptor") 
public void setHibernateInterceptor(HibernateInterceptor value) { 
    hibernateInterceptor = value; 
} 

我有@Autowired @Qualifier和問題一次,但從未與@Resource(@Resource它JSR250,不是春天,但春天支持的話)。

如果您只有一個hibernateInterceptor,那麼您可以使用帶有名稱限定符的@Resource。