Reputation: 75
I've started using Spring Boot few weeks ago and I still can't get my head around the notion of "AbstractController".
public abstract class AbstractController {
protected static final String CUSTOMER_SESSION_ID = "auth";
protected static final String SHOOPY_CART_ID = "SHOOPY_CART";
protected static final String REFERER_HEADER = "Referer";
@Autowired
protected CatalogService catalogService;
@Autowired
protected TotalAmountCalculator amountCalculator;
@Autowired
protected CustomerRepository customerRepo;
@Autowired
protected CartRepository cartRepository;
//Insert necessary data to the header tag
public void populateHeaderData(Model model, Cart activeCart) {
HeaderDTOBuilder.HeaderDTO headerDTO = new HeaderDTOBuilder().
withAmountCalculator(amountCalculator)
.withActiveCart(activeCart)
.withCategories(catalogService.getCategories()).build();
model.addAttribute("headerDTO", headerDTO);
}
public void populateHeaderData(Model model, HttpServletRequest request) {
Cart activeCart = getCartFrom(request);
if(activeCart == null){
activeCart = new Cart();
}
populateHeaderData(model, activeCart);
}
}
I would love to understand what's the difference between a Controller and an AbstractController.
Upvotes: 1
Views: 3850
Reputation: 462
You would only create an abstract controller when you have or plan to have several controller classes with shared logic or instance variables.
Upvotes: 1
Reputation: 8758
Adding Abstract
to the name of the class is a simple way to designate that this class is abstract
(declared as public abstract class
in your case). This means that this class was developed to incorporate some common functionality which will be reused by several child classes. Also there are such classes in JDK itself like java.util.AbstractList
You can find more information about concept of abstract classes here https://www.journaldev.com/1582/abstract-class-in-java
Upvotes: 2