问题描述
在用于自动审计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;
// ...
类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注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!