自动装配 HibernateInterceptor 作为建议

发布于 2024-09-27 16:52:26 字数 1411 浏览 7 评论 0原文

我正在尝试使用 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 :(...不确定为什么它无法自动装配 hibernate 拦截器

有什么想法吗?感谢您的时间。

干杯, J

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花想c 2024-10-04 16:52:26

你有几个拦截器吗?

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

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文