Skip to content
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

[이현주/주디] 4주차 정리내용 제출 #47

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4d8122d
[김현우/패트릭] 1주차 정리
HxWOO Sep 25, 2023
94d1de6
Merge pull request #2 from HIUMC/HxWOO
HxWOO Sep 25, 2023
38433c5
1주차 정리 수정
HxWOO Sep 25, 2023
0a9d9d0
Merge pull request #3 from HIUMC/HxWOO
HxWOO Sep 25, 2023
64a9e69
Update README.md
HxWOO Sep 25, 2023
e8f115d
Update README.md
HxWOO Sep 25, 2023
4360ac5
1주차 정리
yuuddin Sep 26, 2023
b9edb0d
정연 1주차 정리
jeongyeon0208 Sep 26, 2023
c0a1b0a
[Feat(권동민(앨런)): 스프링 입문 커밋]
oznchex Sep 26, 2023
e6f3cee
소연:1주차 코드
soyeoneeii Sep 26, 2023
b438d5c
Merge pull request #6 from HIUMC/jeongyeon
jeongyeon0208 Sep 26, 2023
e5e216a
Merge pull request #8 from HIUMC/oznchex
oznchex Sep 26, 2023
91271f5
[Feat(쿠키/이윤서)] 스프링 입문
yoonsseo Sep 26, 2023
3e1ff62
Merge pull request #10 from HIUMC/yoonsseo
yoonsseo Sep 26, 2023
1c54ac4
[Init(쿠키/이윤서)] 초기설정
yoonsseo Sep 26, 2023
e1ad374
Merge pull request #11 from HIUMC/yoonsseo
yoonsseo Sep 26, 2023
386e9d8
[Feat(전민제): 1주차 정리]
Iaminjae Sep 27, 2023
08fa762
Merge pull request #12 from HIUMC/Iaminjae
Iaminjae Sep 27, 2023
45493aa
1주차 정리
yuuddin Sep 27, 2023
7881138
Delete .gitignore
HxWOO Sep 27, 2023
1714088
Merge pull request #7 from HIUMC/soyeon
HxWOO Sep 27, 2023
85cb7cf
Delete .idea directory
HxWOO Sep 27, 2023
c5038cd
Merge pull request #5 from HIUMC/yujin
HxWOO Sep 27, 2023
ac78194
Delete .idea directory
HxWOO Sep 27, 2023
dff13dc
Delete src directory
HxWOO Sep 27, 2023
04c7196
Delete .gitignore
HxWOO Sep 27, 2023
84359d9
Delete git.iml
HxWOO Sep 27, 2023
e477e5b
[Feat(이현주(주디)): 스프링 입문 최초 커밋]
dlguszoo Sep 27, 2023
6ce3977
[Feat(이현주(주디)): 스프링 핵심 커밋]
dlguszoo Oct 5, 2023
7723261
Study: Java 학습
dlguszoo Oct 5, 2023
5d6ae4a
[Feat(이현주(주디)): 스프링 핵심 2주차 커밋]
dlguszoo Oct 12, 2023
dd492e4
[Feat(이현주(주디)): 스프링 핵심 2주차 커밋]
dlguszoo Oct 12, 2023
e66993d
[Feat(이현주(주디)): 스프링 핵심 3주차 커밋]
dlguszoo Nov 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[Feat(이현주(주디)): 스프링 핵심 3주차 커밋]
dlguszoo committed Nov 2, 2023
commit e66993de380eef955e0972b72f5ba91244f18879
4 changes: 3 additions & 1 deletion hyeon-ju/스프링_핵심_원리/README.MD
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
https://six-warlock-900.notion.site/2-1-189af929a9c84b4d835c5e59a87d0ca6?pvs=4 (1/3)
<br/>
https://six-warlock-900.notion.site/3-2-d347a708f08842ff836b724a05beb161?pvs=4 (2/3)
https://six-warlock-900.notion.site/3-2-d347a708f08842ff836b724a05beb161?pvs=4 (2/3)
<br/>
https://six-warlock-900.notion.site/4-3-fc25a8fd405a4179a15d0c43883ac371?pvs=4 (3/3)
19 changes: 19 additions & 0 deletions hyeon-ju/스프링_핵심_원리/core/build.gradle
Original file line number Diff line number Diff line change
@@ -7,6 +7,14 @@ plugins {
group = 'hello'
version = '0.0.1-SNAPSHOT'

//lombok 설정 추가 시작
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
//lombok 설정 추가 끝

java {
sourceCompatibility = '17'
}
@@ -17,6 +25,17 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
//web 라이브러리 추가
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'jakarta.inject:jakarta.inject-api:2.0.1'

//lombok 라이브러리 추가 시작
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
//lombok 라이브러리 추가 끝

testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
Original file line number Diff line number Diff line change
@@ -16,8 +16,9 @@
) //스프링 빈을 쫙 긁어서 자동으로 끌어 올려줌
public class AutoAppConfig {

@Bean(name = "memoryMemberRepository")
/*@Bean(name = "memoryMemberRepository")
MemberRepository memberRepository() {
return new MemoryMemberRepository();
}
*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package hello.core;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.lang.annotation.Target;

@Getter
@Setter
//롬복이 자동으로 변수보고 getter, setter 만들어 줌
@ToString
public class HelloLombok {

private String name;
private int age;

public static void main(String[] args) {
HelloLombok helloLombok = new HelloLombok();
helloLombok.setName("sdgssge");

String name = helloLombok.getName();
System.out.println("helloLombok = " + helloLombok);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package hello.core.annotation;

import org.springframework.beans.factory.annotation.Qualifier;

import java.lang.annotation.*;

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Qualifier("mainDiscountPolicy")
public @interface MainDiscountPolicy {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package hello.core.common;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class MyLogger {

private String uuid;
private String requestURL;

public void setRequestURL(String requestURL) {
this.requestURL = requestURL;
}

public void log(String message) {
System.out.println("[" + uuid + "]" + "[" + requestURL + "] " + message);
}

@PostConstruct
public void init() {
uuid = UUID.randomUUID().toString(); //전 세계 하나뿐인 글로벌한 ID 생성
System.out.println("[" + uuid + "] request scope bean create: " + this);
}

@PreDestroy
public void close() {
System.out.println("[" + uuid + "] request scope bean close: " + this);
}
}
Original file line number Diff line number Diff line change
@@ -2,7 +2,10 @@

import hello.core.member.Grade;
import hello.core.member.Member;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class FixDiscountPolicy implements DiscountPolicy {

private int discountFixAmount = 1000; //1000원 할인
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package hello.core.discount;

import hello.core.annotation.MainDiscountPolicy;
import hello.core.member.Grade;
import hello.core.member.Member;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Component
@MainDiscountPolicy
public class RateDiscountPolicy implements DiscountPolicy {

private int discountPercent = 10;
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package hello.core.order;

import hello.core.annotation.MainDiscountPolicy;
import hello.core.discount.DiscountPolicy;
import hello.core.discount.FixDiscountPolicy;
import hello.core.discount.RateDiscountPolicy;
import hello.core.member.Member;
import hello.core.member.MemberRepository;
import hello.core.member.MemoryMemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
//@RequiredArgsConstructor //final이 붙은 값을 가지고 생성자를 만들어 줌
public class OrderServiceImpl implements OrderService{
private final MemberRepository memberRepository;
private DiscountPolicy discountPolicy;
private final DiscountPolicy discountPolicy;

@Autowired
public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy discountPolicy) {
public OrderServiceImpl(MemberRepository memberRepository, @MainDiscountPolicy DiscountPolicy discountPolicy) {
this.memberRepository = memberRepository;
this.discountPolicy = discountPolicy;
}

@Override
public Order createOrder(Long memberID, String itemName, int itemPrice) {
Member member = memberRepository.findById(memberID);
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package hello.core.web;

import hello.core.common.MyLogger;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequiredArgsConstructor
public class LogDemoController {

private final LogDemoService logDemoService;
private final MyLogger myLogger; //주입받는 것이 아닌 찾을 수 있는 DL 기능 객체가 주입 됨

@RequestMapping("log-demo")
@ResponseBody //문자를 그대로 온다고 보낼 수 있음
public String logDemo(HttpServletRequest request) throws InterruptedException { //자바에서 제공하는 표준 survlet에 의한 http-request 정보(고객 요청 정보)를 받을 수 있음
String requestURl = request.getRequestURL().toString();

System.out.println("myLogger = " + myLogger.getClass());
myLogger.setRequestURL(requestURl);

myLogger.log("controller test");
logDemoService.logic("testId");
return "OK";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package hello.core.web;

import hello.core.common.MyLogger;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class LogDemoService {

private final MyLogger myLogger;
public void logic(String id) {
myLogger.log("service id = " + id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package hello.core.autowired;

import hello.core.AutoAppConfig;
import hello.core.discount.DiscountPolicy;
import hello.core.member.Grade;
import hello.core.member.Member;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.*;

public class AllBeanTest {

@Test
void findAllBean() {
ApplicationContext ac = new AnnotationConfigApplicationContext(AutoAppConfig.class, DiscountService.class);
//DiscountService를 스프링 빈에 등록, 후에 AutoAppConfig 추가했으므로 AutoAppConfig도 다 등록, 이때 AutoAppConfig는 컴포넌트 스캔을 한다
DiscountService discountService = ac.getBean(DiscountService.class);
Member member = new Member(1L, "userA", Grade.Vip);
int discountPrice = discountService.discount(member, 10000, "fixDiscountPolicy");

assertThat(discountService).isInstanceOf(DiscountService.class);
assertThat(discountPrice).isEqualTo(1000);

int rateDiscountPrice = discountService.discount(member, 20000, "rateDiscountPolicy");
assertThat(rateDiscountPrice).isEqualTo(2000);
}

static class DiscountService { //기존 오더 서비스를 손대면 너무 복잡해지므로, 새로운 서비스를 만듬
private final Map<String, DiscountPolicy> policyMap;
private final List<DiscountPolicy> policies;

public DiscountService(Map<String, DiscountPolicy> policyMap, List<DiscountPolicy> policies) {
this.policyMap = policyMap;
this.policies = policies;
System.out.println("policyMap = " + policyMap);
System.out.println("policies = " + policies);
}

public int discount(Member member, int price, String discountCode) { //할인 코드를 빈 이름이랑 매칭시킨다
DiscountPolicy discountPolicy = policyMap.get(discountCode);
return discountPolicy.discount(member, price);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package hello.core.autowired;

import hello.core.member.Member;
import jakarta.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Optional;

public class AutowiredTest {
@Test
void AutowiredOption() {
ApplicationContext ac = new AnnotationConfigApplicationContext(TestBean.class);//TestBean이 스프링 빈으로 등록이 된다.
//ac.getBean(); //TestBean 받기
}

static class TestBean {

@Autowired(required = false) //false 안해주면 멤버가 빈으로 등록되는 것이 아니라서 오류가 난다
//required = false를 해두면 의존관계가 없을 시, 메소드 자체가 호출이 아예 안된다.
public void setNoBean1(Member noBean1) { //Member는 스프링 관련된 빈이 아님. 즉 스프링 컨테이너에서 관리 되는 것이 아닌 것을 아무거나 집어 넣은 것.
System.out.println("noBean1 = " + noBean1);
}

@Autowired
public void setNoBean2(@Nullable Member noBean2) { //호출은 되지만, null로 들어간다
System.out.println("noBean2 = " + noBean2);
}

@Autowired(required = false)
public void setNoBean3(Optional<Member> noBean3) {
System.out.println("noBean3 = " + noBean3);
}
}
}
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ void vip_x() {
//when
int discount = discountPolicy.discount(member, 10000);
//then
assertThat(discount).isEqualTo(1000);
//assertThat(discount).isEqualTo(1000);

//캡쳐한 error 메시지 참고하자
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package hello.core.lifecycle;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

public class BeanLifeCycleTest {

@Test
public void lifeCycleTest() {
ConfigurableApplicationContext ac = new AnnotationConfigApplicationContext(LifeCycleConfig.class);
NetworkClient client = ac.getBean(NetworkClient.class);
ac.close();
}

@Configuration
static class LifeCycleConfig {
@Bean
public NetworkClient networkClient() {
NetworkClient networkClient = new NetworkClient();
networkClient.setUrl("http://hello-spring.dev");
return networkClient;
}
}
}
Loading