Skip to content

basic

core concepts

  • Inversion of Control: The principle of object-oriented programming, in which objects of the program do not depend on concrete implementations of other objects, but may have knowledge about their abstractions (interfaces) for later interaction.
  • Dependency injection: This is a situation where we introduce an element of one class into another. In practice, DI is implemented by passing parameters to the constructor or using setters. Libraries that implement this approach are also called IoC containers.
  • Aspect Oriented programing: A programing paradigm that allows you to distinguish cross-through(functional) functionality in application. In OOP, the key unit is the class, while in AOP, the key element is the aspect. DI helps to separate application classes into separate modules, and AOP helps to separate cross-cutting concerns from the objects they affect.

Spring Boot Starters

Spring-boot-starter-web

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId><!--提供了REST-->
</dependency>

一般一些工具,可以新提供接口作为对应信息的展示:叫作 provide endpoints、exposing endpoints

spring-boot-starter-data-rest

spring-boot-starter-security

files

  • src
  • main
    • Java
    • resources
    • application.properties
    • static
    • templates
  • test

run

  • Run
  • mvn package // target
  • java -jar xxx.jar(will start ebedded server)

  • spring-boot-maven-pluginmvnw: maven wrapper

  • mvn xxx: run
  • mvn clean xxx:run
  • mvn package
  • java -jar xxx.jar
  • Central vs Local Maven Repo // 本机也是中央存储的呀

IoC

Inversion of Control: The approach of outsourcing the construction and management of objects.

Component Scanning

@SpringBootApplication(scanBasePackages = {"com.lianmeihu.platform", "com.lianmeihu.common"})

Autowired

  • constructor injection
  • field injection

多个 implementations 时:

  • 使用 @Qualifier()
  • @Primary //only one

Bean

Initialization

@Lazy or global config // 只是启动快一点,没有必要,所以默认 disabled 的

Bean Scopes

  • Deafult scope is signleton.
  • prototype: create a new bean for each container request/injection.
  • request
  • session

LifeCycle methods

  • @PostConstruct
  • @PreDestroy

Java Config Bean

@Configuration+@Bean: Make an existing third-party class available to Spring framework.方法手动返回 new Class(). Method name 就是 bean id.

Model View Controller

The MVC Design

  • Model: Data needed by view
  • Controller: Glues the Model and View together. Take the data and sends them to view. //Populates view with meaningful data.

Controller

Annotations

  • @Component: 基础注解,表示一个 JavaBean 可以被注入到 Spring 容器中

  • @Controller: Converts a class into a controller. Controller is the entry point of all web request.

  • @GetMapping: Converts a method into handler method
  • @PostMapping
  • @RequestParam

Model

  • POJO data: plain old Java Object

Three layer

CRUD: Create Read Update Delete data

Controller - Service - Repository(crud and data access OR DAO = Data Access Object = 数据存取对象)

Beans and Dependency Injection

  • Bean: an object that lives inside the Spring container( A @Component class is automatically discovered by Spring). The Spring container manages the object for you.
  • Tight Coupling: Creating an object(dependency) inside of a dependent class. Makes it impossible to unit testing.
  • Dependency: An object that another class depends on.
  • @Component @Service @Repository @Bean

Testing

unit test - no spring container

@Mock: creates a mock.

@InjectMocks: creates an real instance of the class and injects the mocks that are created with the @Mock annotations into this instance.

Grade grade = new Grade("Harry", "Potions", "C-");
when(gradeRepository.getGrades()).thenReturn(Arrays.asList(grade));
when(gradeRepository.getGrade(0)).thenReturn(grade);

所以 mockito 还是 junit 会覆盖掉原来的这些方法咯~

integration test

Databse

ORM: Object Relational Mapping

public interface GradeRepository extends CrudRepository<Grade, Long> {
     Optional<Grade> findByStudentIdAndCourseId(Long studentId, Long courseId);
     List<Grade> findByStudentId(Long studentId);
     List<Grade> findByCourseId(Long courseId);
     @Transactional
     void deleteByStudentIdAndCourseId(Long studentId, Long courseId);
}

想想 go 里面针对这种 Crud 的定式是怎么做的

Authentication

Authentication v.s. Authorization

  • Baisc auth: pass name+pw
  • JWT(Token based auth): pass a token
  • JSON Web Token: Header.Payload.Secret
  • JWT Authorization

Exception

@RestControllerAdvice 类: 相当于 interceptor

@ControllerAdvice
public class XXHandler{
  @ExceptionHandler(MsgWithoutRException.class)
  @ResponseStatus(HttpStatus.OK)
  public ResponseEntity<XxxErrorResponse> handleException(xxxException exc) {

  }
}

AOP

关于 AOP: Aspect(cross-cutting logic)-Oriented Programming 在我看来更像 mixin 这个概念, AOP 里的 Join Point 就是要使用的位置

Two AOP Frameworks:

  • Spring AOP: Only supports method-level join points; run-time weaving;

  • AspectJ: Support all; Compile-time weaving, requires extra compilation step.

In practice

配置

resource/xxx.properties, 通过 @Configuration 注解注入

mybatis v.s. hibernate: 半自动ORM vs 全自动ORM

Jackson data binding

Jackson 也是自动的 call setxxx methods.