I am building a SpringBoot application and I am trying to programatically populate my test database.
I came up with this:
@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) {
...
One of my entities has a Blob where I want to save an image:
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);
}
}
The problem is that I can't manage to inject the sessionFactory.
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException
Is there a better way to achieve what I want?
You're not showing where you've configured your session factory. Are you using spring-boot-starter-data-jpa or are you wiring up the SessionFactory yourself in an @Configuration annotated class or via standard xml bean config?
Edit: Based on that answer check out this stackoverflow answer .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.