How to add a constructor in Controller via Spring
java, spring
Solution
There are few ways to perform your initialization after dependency injection is completed: you can use @PostConstruct annotation on some method. For e.g.:
@PostConstruct
public void initialize() {
//do your stuff
}
Or you can use Spring's InitializingBean interface. Create a class which implements this interface. For e.g.:
@Component
public class MySpringBean implements InitializingBean {
@Override
public void afterPropertiesSet()
throws Exception {
//do your stuff
}
}
Problem
I want to initialize three properties (`companyTypes`, `carrierLists`, and `cabinLevels`) as global variables: ``` @Controller @RequestMapping("/backend/basic") public class TicketRuleController { @Autowired private CarrierService carrierService; @Autowired private CabinLevelService cabinLevelService; @Autowired private CompanyTypeService companyTypeService; private List<DTOCompanyType> companyTypes = companyTypeService.loadAllCompanyTypes(); private List<DTOCarrier> carrierLists = carrierService.loadAllCarriers(); private List<DTOCabinLevel> cabinLevels = cabinLevelService.loadAllCabinLevel(); ... } ``` How can I do this?