I am developing a web application with spring 4. I have more no of controllers. Under them most of the controller use same services. So I planned to move all those common services to a common controller and extend it from the actual controller.
Here are my classes.
BaseController.java
class BaseController {
final static Logger _log = Logger.getLogger(BaseController.class.getName());
public BaseController(IInstitutionService institutionService, IAccessService accessService, IUserService userService, ICostCenterService costCenterService, HttpServletRequest request, ModelMapper modelMapper, IAssetService assetService) {
this.institutionService = institutionService;
this.accessService = accessService;
this.userService = userService;
this.costCenterService = costCenterService;
this.request = request;
this.modelMapper = modelMapper;
this.assetService = assetService;
}
private final IInstitutionService institutionService;
private final HttpServletRequest request;
final IAccessService accessService;
final IUserService userService;
final ICostCenterService costCenterService;
final ModelMapper modelMapper;
final IAssetService assetService;
//common methods
}
TransferController.java
@Controller
@RequestMapping("myrequests/transfer")
public class TransferController extends BaseController {
final static Logger _log = Logger.getLogger(TransferController.class.getName());
private final ITransferService transferService;
@Autowired
public TransferController(IInstitutionService institutionService, IAccessService accessService, IUserService userService, ICostCenterService costCenterService, HttpServletRequest request, ModelMapper modelMapper, IAssetService assetService, ITransferService transferService) {
super(institutionService, accessService, userService, costCenterService, request, modelMapper, assetService);
this.transferService = transferService;
}
@RequestMapping(path = "new", method = RequestMethod.GET)
public String createRequest(ModelMap model) {
TransferRequest transferRequest = new TransferRequest();
User loggedInUser = userService.findByUserId(getPrincipal());
transferRequest.setRequesterContactNo(loggedInUser.getExtension());
transferRequest.setRequesterEmail(loggedInUser.getEmail());
model.addAttribute("transferRequest", transferRequest);
model.addAttribute("hodList", accessService.listUserByType(UserProfileType.HOD.getName(), getInstitution().getId()).stream().collect(Collectors.toMap(User::getUserId, User::getFullName)));
model.addAttribute("userList", accessService.listUserByType(UserProfileType.USER.getName(), getInstitution().getId()).stream().collect(Collectors.toMap(User::getUserId, User::getFullName)));
model.addAttribute("costCenterList", costCenterService.list().stream().collect(Collectors.toMap(CostCenter::getNo, CostCenter::getName)));
model.addAttribute("assetDTOList", assetService.listByUser(getPrincipal(), getInstitution().getCode()).stream().map(asset -> modelMapper.map(asset, AssetDTO.class)).collect(Collectors.toList()));
return "newtransferrequest";
}
}
Like above I have more controllers which extends BaseController. Is this right approach? Are I am doing anything wrong in the design?
Aucun commentaire:
Enregistrer un commentaire