Question

I am trying to use HibernateInterceptor as a Advice and I am trying to autowire it.

The code is as follows,

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

    }

}

The following is my XML mapping,

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

When I check the hibernateInterceptop, all I get is NULL :(...Not sure why its unable to autowire the hibernate interceptor

Any ideas? Thanks for your time.

Cheers, J

Was it helpful?

Solution

Do you have several interceptors?

If you're using @Autowired with @Qualifier, I would suggest that you use @Resource instead. It's more... standard.

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

I had problems with @Autowired and @Qualifier once, but never with @Resource (@Resource it's JSR250, not Spring, but Spring supports it).

If you only have one hibernateInterceptor, you can just use @Resource with the name qualifier.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top