我想将三个属性(companyTypes
,carrierLists
和cabinLevels
)初始化为全局变量:
@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();
...
}
我该怎么做?
答案 0 :(得分:3)
完成依赖项注入后,有几种方法可以执行初始化:您可以在某些方法上使用@PostConstruct注释。例如:
@PostConstruct
public void initialize() {
//do your stuff
}
或者您可以使用Spring的InitializingBean界面。创建一个实现此接口的类。例如:
@Component
public class MySpringBean implements InitializingBean {
@Override
public void afterPropertiesSet()
throws Exception {
//do your stuff
}
}
答案 1 :(得分:0)
您应该能够添加默认构造函数以进行初始化。
public TicketRuleController() {
super();
// Init yours properties here.
}