我正在构建一个SpringBoot应用程序,并且正在尝试编程填充我的测试数据库。
我想出了这个:
@Profile("dev")
@Component
public class DatabaseFillerOnStartup implements ApplicationListener<ContextRefreshedEvent> {
@Resource
private SomeRepository someRepository;
@Resource //This doesn't work
private SessionFactory sessionFactory;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
...我的一个实体有一个Blob,我想在其中保存一个映像:
private Blob createBlobWithSampleImage() {
InputStream imageStream = this.getClass().getClassLoader().getResourceAsStream("sample1.jpg");
LobCreator lobCreator = Hibernate.getLobCreator(sessionFactory.getCurrentSession());
try {
return lobCreator.createBlob(IOUtils.toByteArray(imageStream));
} catch (IOException e) {
throw new RuntimeException(e);
}
}问题是我无法成功地注入sessionFactory。
由: org.springframework.beans.factory.NoSuchBeanDefinitionException引起
有更好的方法来实现我想要的吗?
发布于 2016-02-09 20:27:51
您没有显示配置会话工厂的位置。您是在使用弹簧-启动-启动-数据-jpa,还是自己在@Configuration注释类中或通过标准xml配置连接SessionFactory?
编辑:基于这个答案,请查看这个堆叠溢出的答案。
https://stackoverflow.com/questions/35301279
复制相似问题