BE Study 1st

 This Blog is for studying Java Backend.

Let's Tear down the source code of https://github.com/sivaprasadreddy/techbuzz.git


package com.sivalabs.techbuzz.posts.web.controllers;


import com.sivalabs.techbuzz.config.logging.Loggable;

import com.sivalabs.techbuzz.posts.domain.services.CategoryService;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.GetMapping;


@Controller

@Loggable

class HomeController {

    private static final Logger log = LoggerFactory.getLogger(HomeController.class);

    private final CategoryService categoryService;


    HomeController(CategoryService categoryService) {

        this.categoryService = categoryService;

    }


    @GetMapping("/")

    public String home(Model model) {

        log.info("Handle home request");

        model.addAttribute("categories", categoryService.getAllCategories());

        return "posts/home";

    }

}


above is the code of "HomeController"

Let's take a look at it line by line.


@Controller

@Loggable

The above character @ is called "Annotation." It can be understood as a standardized comment. It does not affect the execution of a code primarily, but there are lots of libraries and frameworks that recognizes it and utilizes it for their implementations: Spring is one of them.

To be specific, '@Controller' shows that this class is a controller. '@Loggable' is an annotation provided by   jcabi-aspects library. Makes a method loggable via Logger, which is applicable with sl4fj.


class HomeController {

The above code is the code for defining class 'HomeController'. It might literally mean a controller for the home page.


    private static final Logger log = LoggerFactory.getLogger(HomeController.class);

I could figure out 'private' and 'static' since I've learned OOP(C++) before, but I wasn't sure what the word 'final' means. 'final' is an equivalent of 'const' of C++. It means that its value cannot be changed ever since it's initialized. Thus, 'private static final' would eventually mean that the variable('log') can be accessed exclusively by the class 'HomeController', and its value is shared by the class itself and all of its instances, and it never changes after its initialization.

I think I should know the class 'Logger' and 'LoggerFactory' to fully understand the code. This is a code for defining a Logger for logging. 'slf4j' stands for 'Simple Logging Facade for Java.' It's kind of a framework that enables easy logging.

I've learned that it can be getClass() instead of HomeController.class(class of itself). Or I can just use an annotation @Slf4j instead of a full definition(private static final Logger log = LoggerFactory.getLogger(HomeController.class);)


    private final CategoryService categoryService;

The above code is a private final variable in a class called 'CategoryService'.

    HomeController(CategoryService categoryService) {

        this.categoryService = categoryService;

    }


The above code must be the constructor. It has the categoryService as a parameter, and sets it as its own variable.


    @GetMapping("/")


The above code is an annotation called 'GetMapping'. It does have a function that when you are requested 'GET' of url "/", the method should be called; this must be a great example of the 'annotation that are utilized by frameworks or libraries' which I mentioned above.


    public String home(Model model) {

        log.info("Handle home request");

        model.addAttribute("categories", categoryService.getAllCategories());

        return "posts/home";

    }


this must be a public method that has 'Model model' as its parameter and returns "post/home" in String format. Going through it, the first line

        log.info("Handle home request");
would mean that it will log "Handle home request" in 'info' type. This must be a method for 'Logger log' defined above.

        model.addAttribute("categories", categoryService.getAllCategories());
This would be a method for 'Model model', which has a string "categories" and a method for categoryService.getAllCategories() as its parameters. I am not sure, but I guess it would function like getting names of all the categories of the class 'categoryService', and add it to 'model's attribute with a keyword 'categories'.

        return "posts/home";
This would be the return for the method.



댓글

이 블로그의 인기 게시물

Interface of Java

Data Analytics Overview(OLTP vs OLAP)

Leetcode