【问题标题】:Autowiring HibernateInterceptor as Advice自动装配 HibernateInterceptor 作为建议
【发布时间】:2010-10-12 07:04:39
【问题描述】:

我正在尝试使用 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

【问题讨论】:

  • 当您在 Spring 配置中手动为 HibernateInterceptor 设置另一个属性时会发生什么?

标签: java hibernate spring spring-aop


【解决方案1】:

你有几个拦截器吗?

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

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

我曾经遇到过@Autowired 和@Qualifier 的问题,但从来没有遇到过@Resource(@Resource 是 JSR250,不是 Spring,但 Spring 支持它)。

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

【讨论】:

    猜你喜欢
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多