79603677

Date: 2025-05-02 15:39:53
Score: 0.5
Natty:
Report link

While the transaction is not provided in @PostConstruct methods, it's possible to use it "the standard way" via ApplicationContext.getBean():

@Transactional(readOnly = true)
public class MyServiceImpl implements MyService {

    @Autowired
    private MyDao myDao;
    private CacheList cacheList;

    @Autowired 
    private ApplicationContext appContext;

    @PostConstruct
    public void init() {
        appContext.getBean(MyService.class).onInit();
    }

    @Transactional(readOnly = true)
    public void onInit() {
        this.cacheList = new CacheList();
        this.cacheList.reloadCache(this.myDao.getAllFromServer());
    }

    ...
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @PostConstruct
  • Low reputation (0.5):
Posted by: tequilacat