-
Notifications
You must be signed in to change notification settings - Fork 10
[21기_임도현] spring tutorial 미션 제출합니다. #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: limdodod
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/gradlew text eol=lf | ||
*.bat text eol=crlf | ||
*.jar binary |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
*# | ||
*.iml | ||
*.ipr | ||
*.iws | ||
*.jar | ||
*.sw? | ||
*~ | ||
.#* | ||
.*.md.html | ||
.DS_Store | ||
.attach_pid* | ||
.classpath | ||
.factorypath | ||
.gradle | ||
.metadata | ||
.project | ||
.recommenders | ||
.settings | ||
.springBeans | ||
.vscode | ||
/code | ||
MANIFEST.MF | ||
_site/ | ||
activemq-data | ||
bin | ||
build | ||
!/**/src/**/bin | ||
!/**/src/**/build | ||
build.log | ||
dependency-reduced-pom.xml | ||
dump.rdb | ||
interpolated*.xml | ||
lib/ | ||
manifest.yml | ||
out | ||
overridedb.* | ||
target | ||
.flattened-pom.xml | ||
secrets.yml | ||
.gradletasknamecache | ||
.sts4-cache | ||
|
||
.idea |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Read Me First | ||
The following was discovered as part of building this project: | ||
|
||
* The original package name ' com.ceos21.spring-boot' is invalid and this project uses 'com.ceos21.spring_boot' instead. | ||
|
||
# Getting Started | ||
|
||
### Reference Documentation | ||
For further reference, please consider the following sections: | ||
|
||
* [Official Gradle documentation](https://docs.gradle.org) | ||
* [Spring Boot Gradle Plugin Reference Guide](https://docs.spring.io/spring-boot/3.4.3/gradle-plugin) | ||
* [Create an OCI image](https://docs.spring.io/spring-boot/3.4.3/gradle-plugin/packaging-oci-image.html) | ||
* [Spring Web](https://docs.spring.io/spring-boot/3.4.3/reference/web/servlet.html) | ||
|
||
### Guides | ||
The following guides illustrate how to use some features concretely: | ||
|
||
* [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/) | ||
* [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/) | ||
* [Building REST services with Spring](https://spring.io/guides/tutorials/rest/) | ||
|
||
### Additional Links | ||
These additional references should also help you: | ||
|
||
* [Gradle Build Scans – insights for your project's build](https://scans.gradle.com#gradle) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,111 @@ | ||
# spring-tutorial-21st | ||
CEOS back-end 21st spring tutorial project | ||
### Spring이 지원하는 기술 | ||
--- | ||
#### IoC/DI | ||
1. IOC (Inversion of Control) : 제어의 역전 | ||
- 객체의 생성과 관리를 개발자가 아닌 Spring 프레임워크가 담당한다!! | ||
|
||
IoC 컨테이너의 작동 방식 | ||
- 객체를 Class로 정의 | ||
- 객체 간의 연관성 지정: Spring 설정파일 (Config) 또는 어노테이션을 통해 객체가 어떻게 연결될지 (의존성 주입) 지정 | ||
- IoC 컨테이너가 이 정보를 바탕으로 객체 생성, 주입 | ||
|
||
|
||
2. DI (Dependency Injection) : 의존성 주입 | ||
- 객체가 직접 코드에서 생성한 것이 아니라, 이미 설정된 속성을 통해 필요한 객체(종속성)를 주입받는 방식 | ||
|
||
1️⃣ 생성자 주입 (권장) | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class UserService { | ||
|
||
private final UserRepository userRepository; | ||
|
||
public UserService(UserRepository userRepository) { | ||
this.userRepository = userRepository; | ||
} | ||
} | ||
|
||
2️⃣ Setter 주입 | ||
|
||
3️⃣ 필드 주입 | ||
|
||
( 2️⃣,3️⃣은 의존성이 없어도 객체가 생성될 수 있어 권장하지 않음!) | ||
|
||
|
||
#### AOP (Aspect Oriented Programming) | ||
- 핵심 기능과 부가 기능을 분리하는 방식 | ||
- 객체지향프로그래밍(OOP)는 공통적인 부가 기능의 중복 코드 발생 --> 이를 해결하기 위함 | ||
|
||
|
||
#### PSA (Portable Service Abstraction) | ||
> Service Abstraction (서비스 추상화) | ||
>> 추상화 계층을 이용해서 특정 기술의 구현부를 숨기고 개발자에게 편의성을 제공해 주는 것 | ||
|
||
- PSA란 환경의 변화와 관계없이 일관된 방식의 기술로의 접근 환경을 제공하는 추상화 구조 | ||
- Spring Web MVC, Spring Transaction, Spring Cache 등 | ||
|
||
|
||
### **Spring Bean이란?** | ||
- Spring 컨테이너가 관리하는 자바의 객체 | ||
- 객체가 의존관계를 등록할 때 Spring 컨테이너에서 빈을 찾고, 그 빈과 의존성을 만든다. | ||
|
||
- 스프링에서 bean 생성시 별다른 설정이 없으면 싱글톤 적용 | ||
- 스프링은 컨테이너를 통해 직접 싱글톤 객체를 생성하고 관리하는데,요청이 들어올 때마다 매번 객체를 생성하지 않고, 이미 만들어진 객체를 공유함 | ||
--- | ||
## Bean을 등록하는 방법 | ||
|
||
1️⃣ @Component -> @Autowired | ||
|
||
: Spring이 자동으로 클래스를 스캔하고 Bean 등록 | ||
: 다른 클래스에서는 @Autowired 어노테이션을 통해 주입받아 사용 | ||
|
||
2️⃣ @Configuration -> @Bean | ||
|
||
: Spring 설정 파일에 @Configuration 어노테이션 추가 -> @Bean으로 지정 | ||
--- | ||
## Spring Bean의 생명주기 | ||
|
||
스프링 IoC 컨테이너 생성 → 스프링 빈 생성 → 의존관계 주입 → 초기화 콜백 메소드 호출 → 사용 → 소멸 전 콜백 메소드 호출 → 스프링 종료 | ||
|
||
|
||
|
||
### **스프링 어노테이션** | ||
- 코드 사이에 특별한 의미, 기능을 수행하도록 하는 기술 (@) | ||
|
||
|
||
- @SpringBootApplication | ||
: Spring Boot을 자동으로 실행시켜주는 어노테이션 | ||
|
||
|
||
- @ComponentScan | ||
: @Component, @Service, @Repository, @Controller, @Configuration이 붙은 빈들을 찾아서 | ||
Context에 빈을 등록해 주는 어노테이션 (@SpringBootApplication에 포함) | ||
Comment on lines
+81
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @componentscan 어노테이션이 위의 어노테이션이 붙은 빈들을 탐색한다고 정리해주셨는데, 그럼 그냥 해당 프로젝트 내에 모든 범위를 탐색할까요?! 어느 파일, 어느 범위까지 탐색하는지도 알아두면 좋을 것 같아요 ! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 기본적으로 @SpringBootApplication이 선언된 클래스가 있는 패키지 기준으로 탐색합니다. @componentscan( 이런식으로, basePackages를 사용해 탐색할 패키지의 시작위치를 정하면, 이 패키지를 포함한 하위 패키지를 모두 탐색해 @component가 있는지를 확인합니다. |
||
|
||
|
||
- @RequestMapping | ||
: 어떤 URL을 어떤 메소드가 처리할지 매핑 | ||
: GET/POST/PUT/PATCH/DELETE 정의 | ||
|
||
|
||
- @Transactional | ||
: 메서드 실행이 정상적으로 완료되면 자동 커밋, 하나라도 실패히면 자동 롤백 | ||
|
||
- @RestController: @Controller + @ResponseBody | ||
: JSON 형식의 데이터나 문자열 등을 반환할 때, 더 간단하게 사용 | ||
|
||
@Repository: DAO 역할을 하는 클래스에 사용 | ||
|
||
@Service: 비즈니스 로직을 담당하는 클래스에 사용 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 정리👍입니다 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 정리해주셔서 감사합니다 ㅠㅠ 공부할 때 참고할게용 |
||
|
||
|
||
### **단위 테스트와 통합 테스트** | ||
|
||
1. 단위테스트 (Unit Test) | ||
- 개별 메서드나 함수, 클래스 등 작은 기능 단위를 대상으로 함 | ||
- 코드의 특정 부분이 정확하게 동작하는지 확인 | ||
- JUnit 사용 | ||
|
||
2. 통합테스트 (Integration Test) | ||
- 전체 시스템이나 주요 컴포넌트들이 함께 어떻게 작동하는지를 검증하는데 초점을 둠 | ||
- 실제 환경과 가까운 조건에서 여러 컴포넌트의 상호작용 검증 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
plugins { | ||
id 'java' | ||
id 'org.springframework.boot' version '3.4.3' | ||
id 'io.spring.dependency-management' version '1.1.7' | ||
} | ||
|
||
group = 'com.ceos21' | ||
version = '0.0.1-SNAPSHOT' | ||
|
||
java { | ||
toolchain { | ||
languageVersion = JavaLanguageVersion.of(17) | ||
} | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation 'org.springframework.boot:spring-boot-starter-web' | ||
|
||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' | ||
compileOnly 'org.projectlombok:lombok' | ||
annotationProcessor 'org.projectlombok:lombok' | ||
runtimeOnly 'com.h2database:h2' | ||
|
||
testImplementation 'org.springframework.boot:spring-boot-starter-test' | ||
testImplementation('org.springframework.boot:spring-boot-starter-test') | ||
testRuntimeOnly 'org.junit.platform:junit-platform-launcher' | ||
} | ||
|
||
tasks.named('test') { | ||
useJUnitPlatform() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12.1-bin.zip | ||
networkTimeout=10000 | ||
validateDistributionUrl=true | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
순환 참조 문제도 공부해 보면 좋을 것 같습니다~!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
맞아요 !
순환참조 문제란 A 클래스가 B 클래스의 Bean 을 주입받고, B 클래스가 A 클래스의 Bean 을 주입받는 상황처럼 서로 순환되어 참조할 경우 발생하는 문제입니다.
생성자 주입은 객체 생성 시점에 의존 관계가 확정돼서 실행시점에 순환 참조가 감지되는데 Setter 주입이나 필드 주입은 런타임 시점에 주입돼서 런타임에 오류가 발생해 지양하는걸로 알고 있습니당 ~~