Java 如何通过Spring在Controller中添加构造函数
我想将三个属性(Java 如何通过Spring在Controller中添加构造函数,java,spring,Java,Spring,我想将三个属性(companyTypes、carrierLists和cabinLevels)初始化为全局变量: @Controller @RequestMapping("/backend/basic") public class TicketRuleController { @Autowired private CarrierService carrierService; @Autowired private CabinLevelService cabinLeve
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();
...
}
@控制器
@请求映射(“/backend/basic”)
公共类票务控制器{
@自动连线
私人承运人服务承运人服务;
@自动连线
私人驾驶室水平服务驾驶室水平服务;
@自动连线
私人公司类型服务公司类型服务;
私有列表companyTypes=companyTypeService.loadAllCompanyTypes();
私有列表carrierLists=carrierService.loadAllCarriers();
私有列表cabinLevels=cabinLevelService.loadAllCabinLevel();
...
}
我如何才能做到这一点?您应该能够为初始化添加构造函数
@Controller
@RequestMapping("/backend/basic")
public class TicketRuleController {
private final CarrierService carrierService;
private final CabinLevelService cabinLevelService;
private final CompanyTypeService companyTypeService;
private final List<DTOCompanyType> companyTypes;
private final List<DTOCarrier> carrierLists;
private final List<DTOCabinLevel> cabinLevels;
@Autowired
public TicketRuleController(
final CarrierService carrierService,
final CabinLevelService cabinLevelService,
final CompanyTypeService companyTypeService
) {
super();
this.carrierService = carrierService;
this.cabinLevelService = cabinLevelService;
this.companyTypeService = companyTypeService;
companyTypes = companyTypeService.loadAllCompanyTypes();
carrierLists = carrierService.loadAllCarriers();
cabinLevels = cabinLevelService.loadAllCabinLevel();
}
// …
}
@控制器
@请求映射(“/backend/basic”)
公共类票务控制器{
私人最终承运人服务承运人服务;
私人最终驾驶室等级服务驾驶室等级服务;
私人最终公司类型服务公司类型服务;
私人最终名单公司类型;
私人最终名单承运人名单;
私人最终列表cabinLevels;
@自动连线
公共票务控制器(
最终承运人服务承运人服务,
最终驾驶室水平服务驾驶室水平服务,
最终公司类型服务公司类型服务
) {
超级();
this.carrierService=carrierService;
this.cabinLevelService=cabinLevelService;
this.companyTypeService=companyTypeService;
companyTypes=companyTypeService.loadAllCompanyTypes();
carrierLists=carrierService.loadAllCarriers();
CabinLevel=CabinLevel服务.loadAllCabinLevel();
}
// …
}
在依赖项注入完成后,有几种方法可以执行初始化:您可以在某些方法上使用注释。例如:
@PostConstruct
public void initialize() {
//do your stuff
}
@Component
public class MySpringBean implements InitializingBean {
@Override
public void afterPropertiesSet()
throws Exception {
//do your stuff
}
}
也可以使用Spring的界面。创建一个实现此接口的类。例如:
@PostConstruct
public void initialize() {
//do your stuff
}
@Component
public class MySpringBean implements InitializingBean {
@Override
public void afterPropertiesSet()
throws Exception {
//do your stuff
}
}
您所说的
global
可能重复?这不是一个好主意。因为如果您想在初始化时使用注入的属性,它将不起作用,因为bean还没有被注入。您是对的。如果你想调用注入属性,它在构造函数中是不起作用的。对不起,我可以问个问题吗!我发现通过@PostConstruct的数据只有在服务器停止时才会被破坏。