纠正一个 Hibernate 的错误结论
文章目录
引言
之前一直以为Hibernate独立于Spring,Hibernate相关的拦截器、监听器都不能注入Spring容器对象,最近发现这个观点是错误的。
Hibernate可以注入Spring容器对象,只是repository的创建顺序原因,使得对象注入失败。
例子
原结论
User实体:
@Entity
@EntityListeners(PasswordEncodeListener.class)
public class User {
}
实体监听器:
@Component
public class PasswordEncodeListener {
@Autowired
private UserRepository repository;
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private UserService userService;
@Autowired
private NoRepoService noRepoService;
@PrePersist
public void encode(User user) {
System.out.println(repository);
System.out.println(jdbcTemplate);
System.out.println(userService);
System.out.println(noRepoService);
}
}
执行保存用户的方法,打印结果:
null
null
null
null
所以之前得出了错误的结论,认为Hibernate独立于Spring,无法使用Spring容器内的对象。
依赖注入
修改监听器,去除对仓库的依赖,包括去除直接依赖仓库的UserRepository与间接依赖仓库的UserService。
@Component
public class PasswordEncodeListener {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private NoRepoService noRepoService;
@PrePersist
public void encode(User user) {
System.out.println(jdbcTemplate);
System.out.println(noRepoService);
}
}
再执行保存用户的方法,结果如下:
org.springframework.jdbc.core.JdbcTemplate@7e9f2c32
club.yunzhi.jpa.service.NoRepoServiceImpl@3976ebfa
更新之前的错误观点:
Hibernate的监听器、拦截器并不是独立于Spring的,而是因为如果直接或间接依赖了仓库,因为创建的顺序问题,会导致所有对象注入失败。如果不依赖于仓库,对象可以注入成功。
解决方案
所以在Hibernate内操作数据库就有了两种方案:
第一种就是我们过去的ApplicationContextHolder,静态方法获取context,手动获取仓库对象。
第二种方案就是基于这次的新发现,可以注入JdbcTemplate,进行数据库的操作。
总结
虽然我总结的错误结论并不影响日常的使用,但是认为还是有必要进行纠正,大家以后都会是优秀的技术员、架构师,不希望因为我的一个错误结论,让大家在以后成长的道路上遇到困难。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论