Programming/JAVA
[JAVA] Springboot DI(의존성 주입)
신샤인
2022. 1. 2. 22:58
반응형
Service의 구조체의 의존성 주입을 위해(Controller)
1. Autowired 롬복을 사용
2. 최종적인 값(변하지 않는 객체 값) final 사용
StudentController.java
...
public class StudentController {
private final StudentService studentService;
@Autowired
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
Business Logic을 담는 Service 클래스 생성
Component 대신 Service 롬복 사용
StudentService.java
package com.js.app.student;
import java.time.LocalDate;
import java.time.Month;
import java.util.List;
import org.springframework.stereotype.Service;
@Service
public class StudentService {
public List<Student> getStudents() {
return List.of(new Student(
1L, "mariam","mariam@gmail.com",
LocalDate.of(2000, Month.JANUARY,6),
21)
);
}
}
참고
https://www.youtube.com/watch?v=9SGDpanrc8U&t=1600s
끝
반응형