Follow up to my existing question, I am getting null session factory and hence my transaction is being failed. Here is what my class structure looks like
@Component
public class MyCachingObj extends HibernateMapper{
@PostConstruct
public void loadAll() {
...
getCurrentSession().getQuery(); //this method is in HibernateMapper
...
}
}
@Repository
public class HibernateMapper {
@Autowired
private SessionFactory _session;
public Session getCurrentSession() {
return _session;
}
}
What I have read so far, that @PostConstruct
doesn't guarantee that spring is done with all beans. Then how can i make sure that spring is done processing and i have session ready to be picked up ?
EDIT I ended up creating
@Component
@Transactional
@Repository
public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent>{..}
which had autowired sessions and autowired another Component
class. However, i noticed that this onApplicationEvent
method was being called twice. I ended up putting a boolean variable to load the underlying data only once.
Now few questions
- Why
onApplicationEvent
is being called twice. - My cached object is annotated with
@Component
, Is it safe to assume that as long as that object is being used by@Autowired
instance, it would remain singleton and would hold the data i loaded at startup ?