Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
0ba7b98
[8월 1일 숙제]spring-mvc-1
dayaelee Aug 3, 2024
257d8a7
[spring-mvc-1] ivy.lee(이다예) 과제 제출합니다.
dayaelee Aug 5, 2024
18c9ca4
[spring-mvc-2] ivy.lee(이다예) 과제 제출합니다.
dayaelee Aug 5, 2024
f36b2c2
Feat: 1.1. Bean Registration, 어떤 어노테이션을 붙였을 때 Bean으로 생성되는지 학습하기
dayaelee Aug 14, 2024
cf8ace6
Feat:1.2 Bean Autowiring, 어떤 방법으로 Component에 Bean을 주입하는지 학습하기
dayaelee Aug 14, 2024
24ecb3e
Feat: 2.1. Constructor Injection, ConstructorInjection으로 InjectionBea…
dayaelee Aug 14, 2024
1e73430
Feat: 2.2. Setter Injection, Setter Injection으로 InjectionBean 주입받기
dayaelee Aug 14, 2024
e66249c
Feat: 2.3. Field Injection, FieldInjection으로 InjectionBean 주입받기
dayaelee Aug 14, 2024
e1c91a8
Feat: 2.3. Field Injection, 주석 삭제
dayaelee Aug 14, 2024
ae95b61
Feat: 3.1. @ComponentScan, ComponentScan에 대해 학습하고, ComponenetScanBean…
dayaelee Aug 14, 2024
7b202dd
Feat: 1.1. Declaring a Bean, Java-based Configuration을 하기 위한 클래스로 지정하…
dayaelee Aug 14, 2024
1e057c3
Feat: 1.2 Bean Dependencies,AuthenticationPrincipalArgumentResolver를 …
dayaelee Aug 14, 2024
0cb9f37
Feat: 2.1. Using @PropertySource and Environment, Java-based Configur…
dayaelee Aug 14, 2024
ae61c19
Feat: 2.2. Using @PropertySource and @Value, ext-api.properties의 goog…
dayaelee Aug 14, 2024
7553778
2.3. Externalized Configuration (Spring Boot), Java-based Configurati…
dayaelee Aug 14, 2024
16fdf85
3.1. @Profile, Java-based Configuration을 하기 위한 클래스로 지정하기, dev 프로파일일 때…
dayaelee Aug 14, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

@Component
public class AutowiredBean {
/*
어떤 방법으로 Component에 Bean을 주입하는지 학습하기
*/
@Autowired
private SpringBean springBean;

public String sayHello() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/*
어떤 어노테이션을 붙였을 때 Bean으로 생성되는지 학습하기
*/
@Component
public class SpringBean {
public String hello() {
return "Hello";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
public class ConstructorInjection {
private InjectionBean injectionBean;

/*
ConstructorInjection으로 InjectionBean 주입받기
*/
public ConstructorInjection(InjectionBean injectionBean){
this.injectionBean = injectionBean;
}

public String sayHello() {
return injectionBean.hello();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
@Service
public class FieldInjection {

/*
FieldInjection으로 InjectionBean 주입받기
*/
@Autowired
private InjectionBean injectionBean;

public String sayHello() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
public class SetterInjection {
private InjectionBean injectionBean;

/*
Setter Injection으로 InjectionBean 주입받기
*/
@Autowired
public void setInjectionBean(InjectionBean injectionBean){
this.injectionBean = injectionBean;
}

public String sayHello() {
return injectionBean.hello();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cholog.scan;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
/*
ComponentScan에 대해 학습하고, ComponenetScanBean을 Bean으로 등록하기
*/
@ComponentScan(basePackages = "cholog.scan")
public class ContextConfiguration {
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cholog.configuration;
import org.springframework.stereotype.Component;

//@Component
public class AuthService {
public String findMemberName() {
return "사용자";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

import cholog.configuration.AuthService;
import cholog.configuration.AuthenticationPrincipalArgumentResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

// TODO: Java-based Configuration을 하기 위한 클래스로 지정하기
@Configuration
@ComponentScan(basePackages = "cholog.scan")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SpringCoreApplication.java에 @SpringBootApplication@componentscan을 들고 있어서,
여기엔 @componentscan이 필요 없습니다!

public class AppConfig {

// TODO: AuthService 빈을 등록하기
@Bean
public AuthService authService() {
return null;
return new AuthService();
// 실제 구현체가 없으면 빈이 등록되지 않는다.
}

// TODO: AuthenticationPrincipalArgumentResolver를 빈 등록하고 authService에 대한 의존성을 주입하기
public AuthenticationPrincipalArgumentResolver authenticationPrincipalArgumentResolver() {
return null;
@Bean
public AuthenticationPrincipalArgumentResolver authenticationPrincipalArgumentResolver(AuthService authService) {
return new AuthenticationPrincipalArgumentResolver(authService);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
import cholog.profile.InmemoryMessageRepository;
import cholog.profile.JdbcMessageRepository;
import cholog.profile.MessageRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

// TODO: Java-based Configuration을 하기 위한 클래스로 지정하기
@Configuration
public class ProfileConfig {

// TODO: dev 프로파일일 때만 InmemoryMessageRepository 빈이 등록되도록 설정하기
@Bean("dataSource")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"dataSource"가 어떤 걸 의미하는지 궁금합니다!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readme 예시에서 이런 식으로 나와 있어 저도 꼭 적어야 하는건가? 하는 생각이었지만 찾아본 내용을 공유해드립니다!

@bean 어노테이션 괄호 안의 dataSource는 빈(bean)의 이름을 지정하기 위해 사용됩니다.
@bean 어노테이션에서 이름을 지정하지 않으면, 메서드 이름이 자동으로 빈의 이름으로 사용됩니다.

dataSource라는 이름을 사용하는 이유는 주로 데이터베이스와의 연결을 관리하는 빈이기 때문입니다
빈 이름을 동일하게 설정하는 이유는 환경별로 다른 구현체를 사용하되, 애플리케이션 코드에서는 일관된 방식으로 빈을 사용할 수 있도록 하기 위해서이다.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 그렇군요! 감사합니다!!

@Profile("dev")
public MessageRepository inMemoryMessageRepository() {
return new InmemoryMessageRepository();
}

// TODO: prod 프로파일일 때만 InmemoryMessageRepository 빈이 등록되도록 설정하기
@Bean("dataSource")
@Profile("prod")
public MessageRepository jdbcMessageRepository() {
return new JdbcMessageRepository();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package cholog.property.config;

import cholog.property.JwtTokenKeyProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// TODO: Java-based Configuration을 하기 위한 클래스로 지정하기

@Configuration
public class AuthConfig {
// TODO: application.properties의 security.jwt.token.secret-key 값을 활용하여 JwtTokenKeyProvider를 빈으로 등록하기
@Value("${security.jwt.token.secret-key}")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

의존성 주입 부분에서 메서드 주입 방식이 아닌 필드 주입 방식을 쓰신 것 같은데 메서드 파라미터로 직접 값을 주입하는 방식이 코드의 의존성이 메서드 시그니처에 명시적으로 드러나게 되기 때문에 가독성과 의존성 관리를 위해서 메서드 주입 방식을 쓰시는 것도 좋을 듯합니다!

private String secretKey;

@Bean
public JwtTokenKeyProvider jwtTokenKeyProvider() {
return new JwtTokenKeyProvider("");
return new JwtTokenKeyProvider(secretKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import cholog.property.GoogleDriveRestClient;
import cholog.property.GoogleMapsRestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

// TODO: Java-based Configuration을 하기 위한 클래스로 지정하기
// TODO: ext-api.properties 파일을 활용하기 위한 설정 추가하기
@Configuration
@PropertySource("classpath:ext-api.properties")
public class PropertySourceConfig {

private final Environment env;
Expand All @@ -14,15 +18,17 @@ public PropertySourceConfig(Environment env) {
this.env = env;
}

// TODO: ext-api.properties의 google.api.endpoint 값을 Environment를 사용해서 가져오기
// TODO: 위 endpoint 값을 사용하여 GoogleMapsRestClient를 빈으로 등록하기
@Bean
public GoogleMapsRestClient googleMapsRestClient() {
return new GoogleMapsRestClient("");
String endpoint = env.getProperty("google.api.endpoint");
return new GoogleMapsRestClient(endpoint);
}

// TODO: ext-api.properties의 google.api.endpoint 값을 어노테이션을 사용해서 가져오기
// TODO: 위 endpoint 값을 사용하여 GoogleMapsRestClient를 빈으로 등록하기
@Value("${google.api.endpoint}")
private String googleApiEndpoint;
Comment on lines +27 to +28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 방법도 좋고, parameter 안에 @value를 사용하는 법도 있습니다!


@Bean
public GoogleDriveRestClient googleDriveRestClient() {
return new GoogleDriveRestClient("");
return new GoogleDriveRestClient(googleApiEndpoint);
}
}
23 changes: 19 additions & 4 deletions spring-mvc-1/initial/src/main/java/cholog/MemberController.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
package cholog;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MemberController {

public String world() {
@GetMapping("/hello")
public String world(@RequestParam(name = "name", required = false) String name, Model model) {
// TODO: /hello 요청 시 resources/templates/static.html 페이지가 응답할 수 있도록 설정하세요.
// TODO: 쿼리 파라미터로 name 요청이 들어왔을 때 해당 값을 hello.html에서 사용할 수 있도록 하세요.
return null;
if (name == null){
// 쿼리 파라미터가 없는 경우
return "static";
} else {
// 쿼리 파라미터가 있는 경우.
model.addAttribute("name", name);
return "hello";
}


}

@GetMapping("/json")
@ResponseBody
public Person json() {
// TODO: /json 요청 시 {"name": "brown", "age": 20} 데이터를 응답할 수 있도록 설정하세요.
return null;
return new Person("brown", 20);
}
}
26 changes: 13 additions & 13 deletions spring-mvc-2/initial/src/main/java/cholog/MemberController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.*;

@Controller
public class MemberController {
Expand All @@ -19,41 +18,42 @@ public class MemberController {
private final AtomicLong index = new AtomicLong(1);

@PostMapping("/members")
public ResponseEntity<Void> create() {
public ResponseEntity<Void> create(@RequestBody Member member) {
// TODO: member 정보를 받아서 생성한다.
Member newMember = Member.toEntity(null, index.getAndIncrement());
Member newMember = Member.toEntity(member, index.getAndIncrement());
members.add(newMember);
return ResponseEntity.created(URI.create("/members/" + newMember.getId())).build();
}

@GetMapping("/members")
public ResponseEntity<List<Member>> read() {
// TODO: 저장된 모든 member 정보를 반환한다.
return null;
return new ResponseEntity<>(members, HttpStatus.OK);
}

@PutMapping("/members/{id}")
public ResponseEntity<Void> update() {
public ResponseEntity<Void> update(@PathVariable Long id, @RequestBody Member newMember) {
// TODO: member의 수정 정보와 url 상의 id 정보를 받아 member 정보를 수정한다.
Member member = members.stream()
.filter(it -> Objects.equals(it.getId(), null))
.filter(it -> Objects.equals(it.getId(), id))
.findFirst()
.orElseThrow(RuntimeException::new);

member.update(null);
member.update(newMember);
//return new ResponseEntity<>(null, HttpStatus.NO_CONTENT); 이건 왜 안되는지?
return null;
}

@DeleteMapping("/members/{id}")
public ResponseEntity<Void> delete() {
public ResponseEntity<Void> delete(@PathVariable Long id) {
// TODO: url 상의 id 정보를 받아 member를 삭제한다.
Member member = members.stream()
.filter(it -> Objects.equals(it.getId(), null))
.filter(it -> Objects.equals(it.getId(), id))
.findFirst()
.orElseThrow(RuntimeException::new);

members.remove(member);

return null;
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}