为什么HibernateTransactionManager不自动注入Spring注入? [英] Why HibernateTransactionManager doesn't autowire Spring injections?

查看:252
本文介绍了为什么HibernateTransactionManager不自动注入Spring注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用于自动审计Hibernate CRUD操作的Interceptor类中,Spring的@Autowired注释不起作用。



我认为这是因为在配置时,Spring没有时间配置自动装配。在其他班级,一切都很好。这里有解决方法或权利吗?感谢。

类PersistenteConfig

  // ... 
@Configuration
@EnableTransactionManagement
@PropertySource({classpath:/foo/bar/persistence-mysql.properties})
@ComponentScan({ foo.bar})

// ...
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory){
HibernateTransactionManager txManager =新的HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);

AuditLogInterceptorImpl interceptor = new AuditLogInterceptorImpl();
txManager.setEntityInterceptor(interceptor);
返回txManager;

//

类AuditLogInterceptorImpl strong>

  // ... 
@Service
@Transactional
公共类AuditLogInterceptorImpl扩展EmptyInterceptor {

private static final long serialVersionUID = 1L;

@Autowired //< ==不适用于...:o(对象在运行时为空)
EventsService eventsService;

// ...


解决方案

<当你用这种方式实例化的时候,你的拦截器没有被配置成托管的Spring bean。尝试以下操作,使拦截器成为一个托管(并因此被注入)bean:



类PersistenteConfig

  // ... 
@Configuration
@EnableTransactionManagement
@PropertySource({classpath:/ foo / bar / persistence-mysql .properties})
@ComponentScan({foo.bar})

// ...
@Bean
public AuditLogInterceptor auditLogInterceptor(){
返回新的AuditLogInterceptorImpl();


$Be
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory){
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);

txManager.setEntityInterceptor(auditLogInterceptor());
返回txManager;
}
// ...


In an Interceptor class for automatically audit Hibernate CRUD operations, @Autowired annotation from Spring is not working.

I supose that it is due to, at configuration time, Spring doesn't have had time to configure autowiring stuff yet. In other classes everything is doing well. Is there a workaround or a "right-to-do" here? Thanks.

Class PersistenteConfig

//...
@Configuration
@EnableTransactionManagement
@PropertySource({"classpath:/foo/bar/persistence-mysql.properties"})
@ComponentScan({"foo.bar" })

        //...
               @Bean
               @Autowired
               public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
                  HibernateTransactionManager txManager = new HibernateTransactionManager();
                  txManager.setSessionFactory(sessionFactory);

                  AuditLogInterceptorImpl interceptor = new AuditLogInterceptorImpl();
                  txManager.setEntityInterceptor(interceptor);
                  return txManager;
               }
        //...

Class AuditLogInterceptorImpl

//...
    @Service
    @Transactional
    public class AuditLogInterceptorImpl extends EmptyInterceptor {

        private static final long serialVersionUID = 1L;

        @Autowired // <== NOT WORKING HERE... :o (object is null at runtime)
        EventsService eventsService;

//...

解决方案

Your interceptor is not configured as a managed Spring bean when instantiated this way. Try the following, making the interceptor a managed (and thus injected) bean:

Class PersistenteConfig

//...
@Configuration
@EnableTransactionManagement
@PropertySource({"classpath:/foo/bar/persistence-mysql.properties"})
@ComponentScan({"foo.bar" })

        //...
               @Bean
               public AuditLogInterceptor auditLogInterceptor() {
                   return new AuditLogInterceptorImpl();
               }

               @Bean
               @Autowired
               public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
                  HibernateTransactionManager txManager = new HibernateTransactionManager();
                  txManager.setSessionFactory(sessionFactory);

                  txManager.setEntityInterceptor(auditLogInterceptor());
                  return txManager;
               }
        //...

这篇关于为什么HibernateTransactionManager不自动注入Spring注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆