Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 {
}

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);
}
}