I have converted a Spring 4.0-based project from xml to javaconfig.
On initialization, one of my beans needs to access Hibernate to fetch some config-data from the DB, through a Spring @Service (buildingService
). The bean initialization looks like this:
@Bean
@DependsOn({ "transactionManager", "webSocketHandler", "buildingService" })
Smarty smarty() {
Smarty bean = new Smarty();
bean.init(); // I also tried @Bean(initMethod = "init") with no difference
return bean;
}
The problem is that in bean.init()
, the service is accessed, which fails with a NullPointerException
.
I added buildingService
to @DependsOn
but it did not help.
Probably the @Service
-annotated classes are processed after the @Bean
!?
Can I initialize the @Service
-annotated class myself upfront?
Edit 1
Thanks so far for all the feedback !
I need to add some details:
buildingService is not a @Bean
, it's, well, a @Service
and looks like this:
@Service("buildingService")
@Transactional
public class BuildingService {
...
public List<Building> getAll() {
final Session session = sessionFactory.getCurrentSession();
final Query query = session.createQuery("from Building order by name");
return query.list();
}
...
}
Smarty is a Spring managed Bean, and initialized in an @Configuration
-annotated class which is doing the initialization of the root-context.
Smarty has a direct dependency on buildingService, like so:
@Resource(name = "buildingService")
private BuildingService buildingService;
I tried annotating Smarty.init()
with @PostConstruct
but this did not change anything.
Note that the first thing Smarty.init()
does is calling buildingService.getAll();