自动装配HibernateInterceptor作为建议

Abh*_*hek 5 java spring hibernate spring-aop

我试图使用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;

    }

}
Run Code Online (Sandbox Code Playgroud)

以下是我的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/>
Run Code Online (Sandbox Code Playgroud)

当我检查hibernateInterceptop时,我得到的只是NULL :( ...不确定为什么它无法自动装入hibernate拦截器

有任何想法吗?谢谢你的时间.

干杯,J

Cho*_*hos 0

你有几个拦截器吗?

如果您将@Autowired 与@Qualifier 一起使用,我建议您改用@Resource。这更……标准。

@Resource(name="hibernateInterceptor")
public void setHibernateInterceptor(HibernateInterceptor value) {
  hibernateInterceptor = value;
}
Run Code Online (Sandbox Code Playgroud)

我曾经在@Autowired和@Qualifier方面遇到过问题,但在@Resource方面却从未遇到过问题(@Resource是JSR250,不是Spring,但Spring支持它)。

如果你只有一个 hibernateInterceptor,你可以只使用 @Resource 和名称限定符。