gpt4 book ai didi

java - 如何通过Spring在Controller中添加构造函数

转载 作者:行者123 更新时间:2023-11-29 06:37:24 34 4
gpt4 key购买 nike

我想将三个属性(companyTypescarrierListscabinLevels)初始化为全局变量:

@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();
...
}

我该怎么做?

最佳答案

依赖注入(inject)完成后,有几种方法可以执行初始化:您可以使用@PostConstruct。一些方法的注释。例如:

@PostConstruct
public void initialize() {
//do your stuff
}

或者你可以使用 Spring 的 InitializingBean界面。创建一个实现此接口(interface)的类。例如:

@Component
public class MySpringBean implements InitializingBean {


@Override
public void afterPropertiesSet()
throws Exception {
//do your stuff
}
}

关于java - 如何通过Spring在Controller中添加构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18503528/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com