2

3 つのプロパティ ( companyTypescarrierLists、および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(); 
    ...
}

これどうやってするの?

4

2 に答える 2

8

依存性注入が完了した後に初期化を実行する方法はいくつかあります。いくつかのメソッドで@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
    }
}
于 2013-08-29T07:26:15.770 に答える
1

初期化用のコンストラクターを追加できるはずです。

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

    // …
}
于 2013-08-29T06:26:15.117 に答える